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);
91 /* Return a vector of contiguous memory of a specified size. */
93 static inline def_optype
94 allocate_def_optype (unsigned num)
98 size = sizeof (struct def_optype_d) + sizeof (tree *) * (num - 1);
99 def_ops = ggc_alloc (size);
100 def_ops->num_defs = num;
104 static inline use_optype
105 allocate_use_optype (unsigned num)
109 size = sizeof (struct use_optype_d) + sizeof (tree *) * (num - 1);
110 use_ops = ggc_alloc (size);
111 use_ops->num_uses = num;
115 static inline v_may_def_optype
116 allocate_v_may_def_optype (unsigned num)
118 v_may_def_optype v_may_def_ops;
120 size = sizeof (struct v_may_def_optype_d) + sizeof (tree) * ((num * 2) - 1);
121 v_may_def_ops = ggc_alloc (size);
122 v_may_def_ops->num_v_may_defs = num;
123 return v_may_def_ops;
126 static inline vuse_optype
127 allocate_vuse_optype (unsigned num)
129 vuse_optype vuse_ops;
131 size = sizeof (struct vuse_optype_d) + sizeof (tree) * (num - 1);
132 vuse_ops = ggc_alloc (size);
133 vuse_ops->num_vuses = num;
137 static inline v_must_def_optype
138 allocate_v_must_def_optype (unsigned num)
140 v_must_def_optype v_must_def_ops;
142 size = sizeof (struct v_must_def_optype_d) + sizeof (tree *) * (num - 1);
143 v_must_def_ops = ggc_alloc (size);
144 v_must_def_ops->num_v_must_defs = num;
145 return v_must_def_ops;
149 free_uses (use_optype *uses, bool dealloc)
160 free_defs (def_optype *defs, bool dealloc)
171 free_vuses (vuse_optype *vuses, bool dealloc)
182 free_v_may_defs (v_may_def_optype *v_may_defs, bool dealloc)
187 ggc_free (*v_may_defs);
193 free_v_must_defs (v_must_def_optype *v_must_defs, bool dealloc)
198 ggc_free (*v_must_defs);
204 remove_vuses (tree stmt)
208 ann = stmt_ann (stmt);
210 free_vuses (&(ann->vuse_ops), true);
214 remove_v_may_defs (tree stmt)
218 ann = stmt_ann (stmt);
220 free_v_may_defs (&(ann->v_may_def_ops), true);
224 remove_v_must_defs (tree stmt)
228 ann = stmt_ann (stmt);
230 free_v_must_defs (&(ann->v_must_def_ops), true);
234 init_ssa_operands (void)
236 VARRAY_TREE_PTR_INIT (build_defs, 5, "build defs");
237 VARRAY_TREE_PTR_INIT (build_uses, 10, "build uses");
238 VARRAY_TREE_INIT (build_v_may_defs, 10, "build v_may_defs");
239 VARRAY_TREE_INIT (build_vuses, 10, "build vuses");
240 VARRAY_TREE_INIT (build_v_must_defs, 10, "build v_must_defs");
244 fini_ssa_operands (void)
249 finalize_ssa_defs (tree stmt)
255 num = VARRAY_ACTIVE_SIZE (build_defs);
259 #ifdef ENABLE_CHECKING
260 /* There should only be a single real definition per assignment. */
261 if (TREE_CODE (stmt) == MODIFY_EXPR && num > 1)
265 def_ops = allocate_def_optype (num);
266 for (x = 0; x < num ; x++)
267 def_ops->defs[x].def = VARRAY_TREE_PTR (build_defs, x);
268 VARRAY_POP_ALL (build_defs);
270 ann = stmt_ann (stmt);
271 ann->def_ops = def_ops;
275 finalize_ssa_uses (tree stmt)
281 num = VARRAY_ACTIVE_SIZE (build_uses);
285 #ifdef ENABLE_CHECKING
288 /* If the pointer to the operand is the statement itself, something is
289 wrong. It means that we are pointing to a local variable (the
290 initial call to get_stmt_operands does not pass a pointer to a
292 for (x = 0; x < num; x++)
293 if (*(VARRAY_TREE_PTR (build_uses, x)) == stmt)
298 use_ops = allocate_use_optype (num);
299 for (x = 0; x < num ; x++)
300 use_ops->uses[x].use = VARRAY_TREE_PTR (build_uses, x);
301 VARRAY_POP_ALL (build_uses);
303 ann = stmt_ann (stmt);
304 ann->use_ops = use_ops;
308 finalize_ssa_v_may_defs (tree stmt)
311 v_may_def_optype v_may_def_ops;
314 num = VARRAY_ACTIVE_SIZE (build_v_may_defs);
318 #ifdef ENABLE_CHECKING
319 /* V_MAY_DEFs must be entered in pairs of result/uses. */
324 v_may_def_ops = allocate_v_may_def_optype (num / 2);
325 for (x = 0; x < num; x++)
326 v_may_def_ops->v_may_defs[x] = VARRAY_TREE (build_v_may_defs, x);
327 VARRAY_CLEAR (build_v_may_defs);
329 ann = stmt_ann (stmt);
330 ann->v_may_def_ops = v_may_def_ops;
334 finalize_ssa_vuses (tree stmt)
338 vuse_optype vuse_ops;
339 v_may_def_optype v_may_defs;
341 #ifdef ENABLE_CHECKING
342 if (VARRAY_ACTIVE_SIZE (build_v_may_defs) > 0)
344 fprintf (stderr, "Please finalize V_MAY_DEFs before finalize VUSES.\n");
349 num = VARRAY_ACTIVE_SIZE (build_vuses);
353 /* Remove superfluous VUSE operands. If the statement already has a
354 V_MAY_DEF operation for a variable 'a', then a VUSE for 'a' is not
355 needed because V_MAY_DEFs imply a VUSE of the variable. For instance,
356 suppose that variable 'a' is aliased:
359 # a_3 = V_MAY_DEF <a_2>
362 The VUSE <a_2> is superfluous because it is implied by the V_MAY_DEF
365 ann = stmt_ann (stmt);
366 v_may_defs = V_MAY_DEF_OPS (ann);
367 if (NUM_V_MAY_DEFS (v_may_defs) > 0)
370 for (i = 0; i < VARRAY_ACTIVE_SIZE (build_vuses); i++)
373 for (j = 0; j < NUM_V_MAY_DEFS (v_may_defs); j++)
375 tree vuse_var, v_may_def_var;
376 tree vuse = VARRAY_TREE (build_vuses, i);
377 tree v_may_def = V_MAY_DEF_OP (v_may_defs, j);
379 if (TREE_CODE (vuse) == SSA_NAME)
380 vuse_var = SSA_NAME_VAR (vuse);
384 if (TREE_CODE (v_may_def) == SSA_NAME)
385 v_may_def_var = SSA_NAME_VAR (v_may_def);
387 v_may_def_var = v_may_def;
389 if (vuse_var == v_may_def_var)
396 /* If we found a useless VUSE operand, remove it from the
397 operand array by replacing it with the last active element
398 in the operand array (unless the useless VUSE was the
399 last operand, in which case we simply remove it. */
402 if (i != VARRAY_ACTIVE_SIZE (build_vuses) - 1)
404 VARRAY_TREE (build_vuses, i)
405 = VARRAY_TREE (build_vuses,
406 VARRAY_ACTIVE_SIZE (build_vuses) - 1);
408 VARRAY_POP (build_vuses);
410 /* We want to rescan the element at this index, unless
411 this was the last element, in which case the loop
418 num = VARRAY_ACTIVE_SIZE (build_vuses);
419 /* We could have reduced the size to zero now, however. */
423 vuse_ops = allocate_vuse_optype (num);
424 for (x = 0; x < num; x++)
425 vuse_ops->vuses[x] = VARRAY_TREE (build_vuses, x);
426 VARRAY_CLEAR (build_vuses);
427 ann->vuse_ops = vuse_ops;
431 finalize_ssa_v_must_defs (tree stmt)
435 v_must_def_optype v_must_def_ops;
437 num = VARRAY_ACTIVE_SIZE (build_v_must_defs);
441 #ifdef ENABLE_CHECKING
442 /* There should only be a single V_MUST_DEF per assignment. */
443 if (TREE_CODE (stmt) == MODIFY_EXPR && num > 1)
447 v_must_def_ops = allocate_v_must_def_optype (num);
448 for (x = 0; x < num ; x++)
449 v_must_def_ops->v_must_defs[x] = VARRAY_TREE (build_v_must_defs, x);
450 VARRAY_POP_ALL (build_v_must_defs);
452 ann = stmt_ann (stmt);
453 ann->v_must_def_ops = v_must_def_ops;
457 finalize_ssa_stmt_operands (tree stmt)
459 #ifdef ENABLE_CHECKING
460 if (check_build_stmt == NULL)
464 finalize_ssa_defs (stmt);
465 finalize_ssa_uses (stmt);
466 finalize_ssa_v_must_defs (stmt);
467 finalize_ssa_v_may_defs (stmt);
468 finalize_ssa_vuses (stmt);
470 #ifdef ENABLE_CHECKING
471 check_build_stmt = NULL;
477 verify_start_operands (tree stmt ATTRIBUTE_UNUSED)
479 #ifdef ENABLE_CHECKING
480 if (VARRAY_ACTIVE_SIZE (build_defs) > 0
481 || VARRAY_ACTIVE_SIZE (build_uses) > 0
482 || VARRAY_ACTIVE_SIZE (build_vuses) > 0
483 || VARRAY_ACTIVE_SIZE (build_v_may_defs) > 0
484 || VARRAY_ACTIVE_SIZE (build_v_must_defs) > 0)
486 if (check_build_stmt != NULL)
488 check_build_stmt = stmt;
493 /* Add DEF_P to the list of pointers to operands defined by STMT. */
496 append_def (tree *def_p, tree stmt ATTRIBUTE_UNUSED)
498 #ifdef ENABLE_CHECKING
499 if (check_build_stmt != stmt)
502 VARRAY_PUSH_TREE_PTR (build_defs, def_p);
506 /* Add USE_P to the list of pointers to operands used by STMT. */
509 append_use (tree *use_p, tree stmt ATTRIBUTE_UNUSED)
511 #ifdef ENABLE_CHECKING
512 if (check_build_stmt != stmt)
515 VARRAY_PUSH_TREE_PTR (build_uses, use_p);
519 /* Add a new virtual def for variable VAR to statement STMT. If PREV_VOPS
520 is not NULL, the existing entries are preserved and no new entries are
521 added here. This is done to preserve the SSA numbering of virtual
525 append_v_may_def (tree var, tree stmt, voperands_t prev_vops)
531 #ifdef ENABLE_CHECKING
532 if (check_build_stmt != stmt)
536 ann = stmt_ann (stmt);
538 /* Don't allow duplicate entries. */
540 for (i = 0; i < VARRAY_ACTIVE_SIZE (build_v_may_defs); i += 2)
542 tree result = VARRAY_TREE (build_v_may_defs, i);
544 || (TREE_CODE (result) == SSA_NAME
545 && var == SSA_NAME_VAR (result)))
549 /* If the statement already had virtual definitions, see if any of the
550 existing V_MAY_DEFs matches VAR. If so, re-use it, otherwise add a new
551 V_MAY_DEF for VAR. */
555 for (i = 0; i < NUM_V_MAY_DEFS (prev_vops->v_may_def_ops); i++)
557 result = V_MAY_DEF_RESULT (prev_vops->v_may_def_ops, i);
559 || (TREE_CODE (result) == SSA_NAME
560 && SSA_NAME_VAR (result) == var))
562 source = V_MAY_DEF_OP (prev_vops->v_may_def_ops, i);
567 /* If no previous V_MAY_DEF operand was found for VAR, create one now. */
568 if (source == NULL_TREE)
574 VARRAY_PUSH_TREE (build_v_may_defs, result);
575 VARRAY_PUSH_TREE (build_v_may_defs, source);
579 /* Add VAR to the list of virtual uses for STMT. If PREV_VOPS
580 is not NULL, the existing entries are preserved and no new entries are
581 added here. This is done to preserve the SSA numbering of virtual
585 append_vuse (tree var, tree stmt, voperands_t prev_vops)
592 #ifdef ENABLE_CHECKING
593 if (check_build_stmt != stmt)
597 ann = stmt_ann (stmt);
599 /* Don't allow duplicate entries. */
600 for (i = 0; i < VARRAY_ACTIVE_SIZE (build_vuses); i++)
602 tree vuse_var = VARRAY_TREE (build_vuses, i);
604 || (TREE_CODE (vuse_var) == SSA_NAME
605 && var == SSA_NAME_VAR (vuse_var)))
609 /* If the statement already had virtual uses, see if any of the
610 existing VUSEs matches VAR. If so, re-use it, otherwise add a new
615 for (i = 0; i < NUM_VUSES (prev_vops->vuse_ops); i++)
617 vuse = VUSE_OP (prev_vops->vuse_ops, i);
619 || (TREE_CODE (vuse) == SSA_NAME
620 && SSA_NAME_VAR (vuse) == var))
627 /* If VAR existed already in PREV_VOPS, re-use it. */
631 VARRAY_PUSH_TREE (build_vuses, var);
634 /* Add VAR to the list of virtual must definitions for STMT. If PREV_VOPS
635 is not NULL, the existing entries are preserved and no new entries are
636 added here. This is done to preserve the SSA numbering of virtual
640 append_v_must_def (tree var, tree stmt, voperands_t prev_vops)
647 #ifdef ENABLE_CHECKING
648 if (check_build_stmt != stmt)
652 ann = stmt_ann (stmt);
654 /* Don't allow duplicate entries. */
655 for (i = 0; i < VARRAY_ACTIVE_SIZE (build_v_must_defs); i++)
657 tree v_must_def_var = VARRAY_TREE (build_v_must_defs, i);
658 if (var == v_must_def_var
659 || (TREE_CODE (v_must_def_var) == SSA_NAME
660 && var == SSA_NAME_VAR (v_must_def_var)))
664 /* If the statement already had virtual must defs, see if any of the
665 existing V_MUST_DEFs matches VAR. If so, re-use it, otherwise add a new
666 V_MUST_DEF for VAR. */
668 v_must_def = NULL_TREE;
670 for (i = 0; i < NUM_V_MUST_DEFS (prev_vops->v_must_def_ops); i++)
672 v_must_def = V_MUST_DEF_OP (prev_vops->v_must_def_ops, i);
673 if (v_must_def == var
674 || (TREE_CODE (v_must_def) == SSA_NAME
675 && SSA_NAME_VAR (v_must_def) == var))
682 /* If VAR existed already in PREV_VOPS, re-use it. */
686 VARRAY_PUSH_TREE (build_v_must_defs, var);
690 /* External entry point which by-passes the previous vops mechanism. */
692 add_vuse (tree var, tree stmt)
694 append_vuse (var, stmt, NULL);
698 /* Get the operands of statement STMT. Note that repeated calls to
699 get_stmt_operands for the same statement will do nothing until the
700 statement is marked modified by a call to modify_stmt(). */
703 get_stmt_operands (tree stmt)
707 struct voperands_d prev_vops;
709 #if defined ENABLE_CHECKING
710 /* The optimizers cannot handle statements that are nothing but a
711 _DECL. This indicates a bug in the gimplifier. */
712 if (SSA_VAR_P (stmt))
716 /* Ignore error statements. */
717 if (TREE_CODE (stmt) == ERROR_MARK)
720 ann = get_stmt_ann (stmt);
722 /* If the statement has not been modified, the operands are still valid. */
726 timevar_push (TV_TREE_OPS);
728 /* Initially assume that the statement has no volatile operands, nor
729 makes aliased loads or stores. */
730 ann->has_volatile_ops = false;
731 ann->makes_aliased_stores = false;
732 ann->makes_aliased_loads = false;
734 /* Remove any existing operands as they will be scanned again. */
735 free_defs (&(ann->def_ops), true);
736 free_uses (&(ann->use_ops), true);
738 /* Before removing existing virtual operands, save them in PREV_VOPS so
739 that we can re-use their SSA versions. */
740 prev_vops.v_may_def_ops = V_MAY_DEF_OPS (ann);
741 prev_vops.vuse_ops = VUSE_OPS (ann);
742 prev_vops.v_must_def_ops = V_MUST_DEF_OPS (ann);
744 /* Dont free the previous values to memory since we're still using them. */
745 free_v_may_defs (&(ann->v_may_def_ops), false);
746 free_vuses (&(ann->vuse_ops), false);
747 free_v_must_defs (&(ann->v_must_def_ops), false);
749 start_ssa_stmt_operands (stmt);
751 code = TREE_CODE (stmt);
755 get_expr_operands (stmt, &TREE_OPERAND (stmt, 1), opf_none, &prev_vops);
756 if (TREE_CODE (TREE_OPERAND (stmt, 0)) == ARRAY_REF
757 || TREE_CODE (TREE_OPERAND (stmt, 0)) == COMPONENT_REF
758 || TREE_CODE (TREE_OPERAND (stmt, 0)) == REALPART_EXPR
759 || TREE_CODE (TREE_OPERAND (stmt, 0)) == IMAGPART_EXPR
760 /* Use a V_MAY_DEF if the RHS might throw, as the LHS won't be
761 modified in that case. FIXME we should represent somehow
762 that it is killed on the fallthrough path. */
763 || tree_could_throw_p (TREE_OPERAND (stmt, 1)))
764 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), opf_is_def,
767 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0),
768 opf_is_def | opf_kill_def, &prev_vops);
772 get_expr_operands (stmt, &COND_EXPR_COND (stmt), opf_none, &prev_vops);
776 get_expr_operands (stmt, &SWITCH_COND (stmt), opf_none, &prev_vops);
781 int noutputs = list_length (ASM_OUTPUTS (stmt));
782 const char **oconstraints
783 = (const char **) alloca ((noutputs) * sizeof (const char *));
786 const char *constraint;
787 bool allows_mem, allows_reg, is_inout;
789 for (i=0, link = ASM_OUTPUTS (stmt); link;
790 ++i, link = TREE_CHAIN (link))
792 oconstraints[i] = constraint
793 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
794 parse_output_constraint (&constraint, i, 0, 0,
795 &allows_mem, &allows_reg, &is_inout);
796 if (allows_reg && is_inout)
797 /* This should have been split in gimplify_asm_expr. */
800 if (!allows_reg && allows_mem)
802 tree t = get_base_address (TREE_VALUE (link));
804 mark_call_clobbered (t);
807 get_expr_operands (stmt, &TREE_VALUE (link), opf_is_def,
811 for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
814 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
815 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
816 oconstraints, &allows_mem, &allows_reg);
818 if (!allows_reg && allows_mem)
820 tree t = get_base_address (TREE_VALUE (link));
822 mark_call_clobbered (t);
825 get_expr_operands (stmt, &TREE_VALUE (link), 0, &prev_vops);
828 /* Clobber memory for asm ("" : : : "memory"); */
829 for (link = ASM_CLOBBERS (stmt); link; link = TREE_CHAIN (link))
830 if (!strcmp (TREE_STRING_POINTER (TREE_VALUE (link)), "memory"))
831 add_call_clobber_ops (stmt, &prev_vops);
836 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), opf_none, &prev_vops);
840 get_expr_operands (stmt, &GOTO_DESTINATION (stmt), opf_none, &prev_vops);
844 get_expr_operands (stmt, &LABEL_EXPR_LABEL (stmt), opf_none, &prev_vops);
847 /* These nodes contain no variable references. */
849 case CASE_LABEL_EXPR:
851 case TRY_FINALLY_EXPR:
858 /* Notice that if get_expr_operands tries to use &STMT as the operand
859 pointer (which may only happen for USE operands), we will abort in
860 append_use. This default will handle statements like empty statements,
861 CALL_EXPRs or VA_ARG_EXPRs that may appear on the RHS of a statement
862 or as statements themselves. */
863 get_expr_operands (stmt, &stmt, opf_none, &prev_vops);
867 finalize_ssa_stmt_operands (stmt);
869 /* Now free the previous virtual ops to memory. */
870 free_v_may_defs (&(prev_vops.v_may_def_ops), true);
871 free_vuses (&(prev_vops.vuse_ops), true);
872 free_v_must_defs (&(prev_vops.v_must_def_ops), true);
874 /* Clear the modified bit for STMT. Subsequent calls to
875 get_stmt_operands for this statement will do nothing until the
876 statement is marked modified by a call to modify_stmt(). */
879 timevar_pop (TV_TREE_OPS);
883 /* Recursively scan the expression pointed by EXPR_P in statement STMT.
884 FLAGS is one of the OPF_* constants modifying how to interpret the
885 operands found. PREV_VOPS is as in append_v_may_def and append_vuse. */
888 get_expr_operands (tree stmt, tree *expr_p, int flags, voperands_t prev_vops)
894 if (expr == NULL || expr == error_mark_node)
897 code = TREE_CODE (expr);
898 class = TREE_CODE_CLASS (code);
900 /* Expressions that make no memory references. */
904 || code == FUNCTION_DECL
905 || code == EXC_PTR_EXPR
906 || code == FILTER_EXPR
907 || code == LABEL_DECL)
910 /* We could have the address of a component, array member, etc which
911 has interesting variable references. */
912 if (code == ADDR_EXPR)
914 enum tree_code subcode = TREE_CODE (TREE_OPERAND (expr, 0));
916 /* Taking the address of a variable does not represent a
917 reference to it, but the fact that STMT takes its address will be
918 of interest to some passes (e.g. alias resolution). */
919 add_stmt_operand (expr_p, stmt, 0, NULL);
921 /* If the address is invariant, there may be no interesting variable
922 references inside. */
923 if (is_gimple_min_invariant (expr))
926 /* There should be no VUSEs created, since the referenced objects are
927 not really accessed. The only operands that we should find here
928 are ARRAY_REF indices which will always be real operands (GIMPLE
929 does not allow non-registers as array indices). */
930 flags |= opf_no_vops;
932 /* Avoid recursion. */
934 class = TREE_CODE_CLASS (code);
935 expr_p = &TREE_OPERAND (expr, 0);
939 /* If we found a variable, add it to DEFS or USES depending on the
941 if (SSA_VAR_P (expr))
943 add_stmt_operand (expr_p, stmt, flags, prev_vops);
947 /* Pointer dereferences always represent a use of the base pointer. */
948 if (code == INDIRECT_REF)
950 tree *pptr = &TREE_OPERAND (expr, 0);
955 if (!aliases_computed_p)
957 /* If the pointer does not have a memory tag and aliases have not
958 been computed yet, mark the statement as having volatile
959 operands to prevent DOM from entering it in equivalence tables
960 and DCE from killing it. */
961 stmt_ann (stmt)->has_volatile_ops = true;
965 struct ptr_info_def *pi = NULL;
967 /* If we have computed aliasing already, check if PTR has
968 flow-sensitive points-to information. */
969 if (TREE_CODE (ptr) == SSA_NAME
970 && (pi = SSA_NAME_PTR_INFO (ptr)) != NULL
973 /* PTR has its own memory tag. Use it. */
974 add_stmt_operand (&pi->name_mem_tag, stmt, flags,
979 /* If PTR is not an SSA_NAME or it doesn't have a name
980 tag, use its type memory tag. */
983 /* If we are emitting debugging dumps, display a warning if
984 PTR is an SSA_NAME with no flow-sensitive alias
985 information. That means that we may need to compute
988 && TREE_CODE (ptr) == SSA_NAME
992 "NOTE: no flow-sensitive alias info for ");
993 print_generic_expr (dump_file, ptr, dump_flags);
994 fprintf (dump_file, " in ");
995 print_generic_stmt (dump_file, stmt, dump_flags);
998 if (TREE_CODE (ptr) == SSA_NAME)
999 ptr = SSA_NAME_VAR (ptr);
1000 ann = var_ann (ptr);
1001 add_stmt_operand (&ann->type_mem_tag, stmt, flags, prev_vops);
1006 /* If a constant is used as a pointer, we can't generate a real
1007 operand for it but we mark the statement volatile to prevent
1008 optimizations from messing things up. */
1009 else if (TREE_CODE (ptr) == INTEGER_CST)
1011 stmt_ann (stmt)->has_volatile_ops = true;
1015 /* Everything else *should* have been folded elsewhere, but users
1016 are smarter than we in finding ways to write invalid code. We
1017 cannot just abort here. If we were absolutely certain that we
1018 do handle all valid cases, then we could just do nothing here.
1019 That seems optimistic, so attempt to do something logical... */
1020 else if ((TREE_CODE (ptr) == PLUS_EXPR || TREE_CODE (ptr) == MINUS_EXPR)
1021 && TREE_CODE (TREE_OPERAND (ptr, 0)) == ADDR_EXPR
1022 && TREE_CODE (TREE_OPERAND (ptr, 1)) == INTEGER_CST)
1024 /* Make sure we know the object is addressable. */
1025 pptr = &TREE_OPERAND (ptr, 0);
1026 add_stmt_operand (pptr, stmt, 0, NULL);
1028 /* Mark the object itself with a VUSE. */
1029 pptr = &TREE_OPERAND (*pptr, 0);
1030 get_expr_operands (stmt, pptr, flags, prev_vops);
1034 /* Ok, this isn't even is_gimple_min_invariant. Something's broke. */
1038 /* Add a USE operand for the base pointer. */
1039 get_expr_operands (stmt, pptr, opf_none, prev_vops);
1043 /* Treat array references as references to the virtual variable
1044 representing the array. The virtual variable for an ARRAY_REF
1045 is the VAR_DECL for the array. */
1046 if (code == ARRAY_REF)
1048 /* Add the virtual variable for the ARRAY_REF to VDEFS or VUSES
1049 according to the value of IS_DEF. Recurse if the LHS of the
1050 ARRAY_REF node is not a regular variable. */
1051 if (SSA_VAR_P (TREE_OPERAND (expr, 0)))
1052 add_stmt_operand (expr_p, stmt, flags, prev_vops);
1054 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags, prev_vops);
1056 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none, prev_vops);
1060 /* Similarly to arrays, references to compound variables (complex types
1061 and structures/unions) are globbed.
1063 FIXME: This means that
1069 will not be constant propagated because the two partial
1070 definitions to 'a' will kill each other. Note that SRA may be
1071 able to fix this problem if 'a' can be scalarized. */
1072 if (code == IMAGPART_EXPR || code == REALPART_EXPR || code == COMPONENT_REF)
1074 /* If the LHS of the compound reference is not a regular variable,
1075 recurse to keep looking for more operands in the subexpression. */
1076 if (SSA_VAR_P (TREE_OPERAND (expr, 0)))
1077 add_stmt_operand (expr_p, stmt, flags, prev_vops);
1079 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags, prev_vops);
1084 /* Function calls. Add every argument to USES. If the callee is
1085 neither pure nor const, create a VDEF reference for GLOBAL_VAR
1086 (See find_vars_r). */
1087 if (code == CALL_EXPR)
1090 int call_flags = call_expr_flags (expr);
1092 /* Find uses in the called function. */
1093 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_none, prev_vops);
1095 for (op = TREE_OPERAND (expr, 1); op; op = TREE_CHAIN (op))
1096 get_expr_operands (stmt, &TREE_VALUE (op), opf_none, prev_vops);
1098 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none, prev_vops);
1100 if (bitmap_first_set_bit (call_clobbered_vars) >= 0)
1102 /* A 'pure' or a 'const' functions never call clobber anything.
1103 A 'noreturn' function might, but since we don't return anyway
1104 there is no point in recording that. */
1106 & (ECF_PURE | ECF_CONST | ECF_NORETURN)))
1107 add_call_clobber_ops (stmt, prev_vops);
1108 else if (!(call_flags & (ECF_CONST | ECF_NORETURN)))
1109 add_call_read_ops (stmt, prev_vops);
1111 else if (!aliases_computed_p)
1112 stmt_ann (stmt)->has_volatile_ops = true;
1118 if (code == TREE_LIST)
1122 for (op = expr; op; op = TREE_CHAIN (op))
1123 get_expr_operands (stmt, &TREE_VALUE (op), flags, prev_vops);
1129 if (code == MODIFY_EXPR)
1131 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none, prev_vops);
1132 if (TREE_CODE (TREE_OPERAND (expr, 0)) == ARRAY_REF
1133 || TREE_CODE (TREE_OPERAND (expr, 0)) == COMPONENT_REF
1134 || TREE_CODE (TREE_OPERAND (expr, 0)) == REALPART_EXPR
1135 || TREE_CODE (TREE_OPERAND (expr, 0)) == IMAGPART_EXPR)
1136 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_is_def,
1139 get_expr_operands (stmt, &TREE_OPERAND (expr, 0),
1140 opf_is_def | opf_kill_def, prev_vops);
1145 /* Mark VA_ARG_EXPR nodes as making volatile references. FIXME,
1146 this is needed because we currently do not gimplify VA_ARG_EXPR
1148 if (code == VA_ARG_EXPR)
1150 stmt_ann (stmt)->has_volatile_ops = true;
1154 /* Unary expressions. */
1156 || code == TRUTH_NOT_EXPR
1157 || code == BIT_FIELD_REF
1158 || code == CONSTRUCTOR)
1160 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags, prev_vops);
1164 /* Binary expressions. */
1167 || code == TRUTH_AND_EXPR
1168 || code == TRUTH_OR_EXPR
1169 || code == TRUTH_XOR_EXPR
1170 || code == COMPOUND_EXPR)
1172 tree op0 = TREE_OPERAND (expr, 0);
1173 tree op1 = TREE_OPERAND (expr, 1);
1175 /* If it would be profitable to swap the operands, then do so to
1176 canonicalize the statement, enabling better optimization.
1178 By placing canonicalization of such expressions here we
1179 transparently keep statements in canonical form, even
1180 when the statement is modified. */
1181 if (tree_swap_operands_p (op0, op1, false))
1183 /* For relationals we need to swap the operands and change
1190 TREE_SET_CODE (expr, swap_tree_comparison (code));
1191 TREE_OPERAND (expr, 0) = op1;
1192 TREE_OPERAND (expr, 1) = op0;
1195 /* For a commutative operator we can just swap the operands. */
1196 if (commutative_tree_code (code))
1198 TREE_OPERAND (expr, 0) = op1;
1199 TREE_OPERAND (expr, 1) = op0;
1203 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags, prev_vops);
1204 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags, prev_vops);
1208 /* If we get here, something has gone wrong. */
1209 fprintf (stderr, "unhandled expression in get_expr_operands():\n");
1211 fputs ("\n", stderr);
1216 /* Add *VAR_P to the appropriate operand array of STMT. FLAGS is as in
1217 get_expr_operands. If *VAR_P is a GIMPLE register, it will be added to
1218 the statement's real operands, otherwise it is added to virtual
1221 PREV_VOPS is used when adding virtual operands to statements that
1222 already had them (See append_v_may_def and append_vuse). */
1225 add_stmt_operand (tree *var_p, tree stmt, int flags, voperands_t prev_vops)
1235 s_ann = stmt_ann (stmt);
1237 /* If the operand is an ADDR_EXPR, add its operand to the list of
1238 variables that have had their address taken in this statement. */
1239 if (TREE_CODE (var) == ADDR_EXPR)
1241 note_addressable (TREE_OPERAND (var, 0), s_ann);
1245 /* If the original variable is not a scalar, it will be added to the list
1246 of virtual operands. In that case, use its base symbol as the virtual
1247 variable representing it. */
1248 is_real_op = is_gimple_reg (var);
1249 if (!is_real_op && !DECL_P (var))
1250 var = get_virtual_var (var);
1252 /* If VAR is not a variable that we care to optimize, do nothing. */
1253 if (var == NULL_TREE || !SSA_VAR_P (var))
1256 sym = (TREE_CODE (var) == SSA_NAME ? SSA_NAME_VAR (var) : var);
1257 v_ann = var_ann (sym);
1259 /* FIXME: We currently refuse to optimize variables that have hidden uses
1260 (variables used in VLA declarations, MD builtin calls and variables
1261 from the parent function in nested functions). This is because not
1262 all uses of these variables are exposed in the IL or the statements
1263 that reference them are not in GIMPLE form. If that's the case, mark
1264 the statement as having volatile operands and return. */
1265 if (v_ann->has_hidden_use)
1267 s_ann->has_volatile_ops = true;
1271 /* Don't expose volatile variables to the optimizers. */
1272 if (TREE_THIS_VOLATILE (sym))
1274 s_ann->has_volatile_ops = true;
1280 /* The variable is a GIMPLE register. Add it to real operands. */
1281 if (flags & opf_is_def)
1282 append_def (var_p, stmt);
1284 append_use (var_p, stmt);
1288 varray_type aliases;
1290 /* The variable is not a GIMPLE register. Add it (or its aliases) to
1291 virtual operands, unless the caller has specifically requested
1292 not to add virtual operands (used when adding operands inside an
1293 ADDR_EXPR expression). */
1294 if (flags & opf_no_vops)
1297 aliases = v_ann->may_aliases;
1299 /* If alias information hasn't been computed yet, then
1300 addressable variables will not be an alias tag nor will they
1301 have aliases. In this case, mark the statement as having
1302 volatile operands. */
1303 if (!aliases_computed_p && may_be_aliased (var))
1304 s_ann->has_volatile_ops = true;
1306 if (aliases == NULL)
1308 /* The variable is not aliased or it is an alias tag. */
1309 if (flags & opf_is_def)
1311 if (v_ann->is_alias_tag)
1313 /* Alias tagged vars get regular V_MAY_DEF */
1314 s_ann->makes_aliased_stores = 1;
1315 append_v_may_def (var, stmt, prev_vops);
1317 else if ((flags & opf_kill_def)
1318 && v_ann->mem_tag_kind == NOT_A_TAG)
1319 /* V_MUST_DEF for non-aliased non-GIMPLE register
1320 variable definitions. Avoid memory tags. */
1321 append_v_must_def (var, stmt, prev_vops);
1323 /* Call-clobbered variables & memory tags get
1325 append_v_may_def (var, stmt, prev_vops);
1329 append_vuse (var, stmt, prev_vops);
1330 if (v_ann->is_alias_tag)
1331 s_ann->makes_aliased_loads = 1;
1338 /* The variable is aliased. Add its aliases to the virtual
1340 if (VARRAY_ACTIVE_SIZE (aliases) == 0)
1343 if (flags & opf_is_def)
1345 /* If the variable is also an alias tag, add a virtual
1346 operand for it, otherwise we will miss representing
1347 references to the members of the variable's alias set.
1348 This fixes the bug in gcc.c-torture/execute/20020503-1.c. */
1349 if (v_ann->is_alias_tag)
1350 append_v_may_def (var, stmt, prev_vops);
1352 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
1353 append_v_may_def (VARRAY_TREE (aliases, i), stmt, prev_vops);
1355 s_ann->makes_aliased_stores = 1;
1359 if (v_ann->is_alias_tag)
1360 append_vuse (var, stmt, prev_vops);
1362 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
1363 append_vuse (VARRAY_TREE (aliases, i), stmt, prev_vops);
1365 s_ann->makes_aliased_loads = 1;
1371 /* Record that VAR had its address taken in the statement with annotations
1375 note_addressable (tree var, stmt_ann_t s_ann)
1377 var = get_base_address (var);
1378 if (var && SSA_VAR_P (var))
1380 if (s_ann->addresses_taken == NULL)
1381 s_ann->addresses_taken = BITMAP_GGC_ALLOC ();
1382 bitmap_set_bit (s_ann->addresses_taken, var_ann (var)->uid);
1387 /* Add clobbering definitions for .GLOBAL_VAR or for each of the call
1388 clobbered variables in the function. */
1391 add_call_clobber_ops (tree stmt, voperands_t prev_vops)
1393 /* Functions that are not const, pure or never return may clobber
1394 call-clobbered variables. */
1395 stmt_ann (stmt)->makes_clobbering_call = true;
1397 /* If we had created .GLOBAL_VAR earlier, use it. Otherwise, add
1398 a V_MAY_DEF operand for every call clobbered variable. See
1399 compute_may_aliases for the heuristic used to decide whether
1400 to create .GLOBAL_VAR or not. */
1402 add_stmt_operand (&global_var, stmt, opf_is_def, prev_vops);
1407 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i,
1409 tree var = referenced_var (i);
1411 /* If VAR is read-only, don't add a V_MAY_DEF, just a
1413 if (!TREE_READONLY (var))
1414 add_stmt_operand (&var, stmt, opf_is_def, prev_vops);
1416 add_stmt_operand (&var, stmt, opf_none, prev_vops);
1422 /* Add VUSE operands for .GLOBAL_VAR or all call clobbered variables in the
1426 add_call_read_ops (tree stmt, voperands_t prev_vops)
1428 /* Otherwise, if the function is not pure, it may reference memory. Add
1429 a VUSE for .GLOBAL_VAR if it has been created. Otherwise, add a VUSE
1430 for each call-clobbered variable. See add_referenced_var for the
1431 heuristic used to decide whether to create .GLOBAL_VAR. */
1433 add_stmt_operand (&global_var, stmt, opf_none, prev_vops);
1438 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i,
1440 tree var = referenced_var (i);
1441 add_stmt_operand (&var, stmt, opf_none, prev_vops);
1446 #include "gt-tree-ssa-operands.h"