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. */
41 #define opf_is_def (1 << 0)
43 /* No virtual operands should be created in the expression. This is used
44 when traversing ADDR_EXPR nodes which have different semantics than
45 other expressions. Inside an ADDR_EXPR node, the only operands that we
46 need to consider are indices into arrays. For instance, &a.b[i] should
47 generate a USE of 'i' but it should not generate a VUSE for 'a' nor a
49 #define opf_no_vops (1 << 1)
51 /* Array for building all the def operands. */
52 static GTY (()) varray_type build_defs
;
54 /* Array for building all the use operands. */
55 static GTY (()) varray_type build_uses
;
57 /* Array for building all the vdef operands. */
58 static GTY (()) varray_type build_vdefs
;
60 /* Array for building all the vuse operands. */
61 static GTY (()) varray_type build_vuses
;
63 #ifdef ENABLE_CHECKING
64 tree check_build_stmt
;
67 typedef struct voperands_d
73 static void note_addressable (tree
, stmt_ann_t
);
74 static void get_expr_operands (tree
, tree
*, int, voperands_t
);
75 static inline void append_def (tree
*, tree
);
76 static inline void append_use (tree
*, tree
);
77 static void append_vdef (tree
, tree
, voperands_t
);
78 static void add_call_clobber_ops (tree
, voperands_t
);
79 static void add_call_read_ops (tree
, voperands_t
);
80 static void add_stmt_operand (tree
*, tree
, int, voperands_t
);
83 struct freelist_d
GTY((chain_next ("%h.next")))
85 struct freelist_d
*next
;
89 static GTY ((length ("NUM_FREE"))) struct freelist_d optype_freelist
[NUM_FREE
] = { {0}, {0}, {0}, {0} };
93 check_optype_freelist (size_t num ATTRIBUTE_UNUSED
)
99 if (num
<= NUM_FREE
&& optype_freelist
[num
- 1].next
)
101 vec
= (void *)optype_freelist
[num
- 1].next
;
102 optype_freelist
[num
- 1].next
= optype_freelist
[num
- 1].next
->next
;
107 /* Return a vector of contiguous memory of a specified size. */
111 add_optype_freelist (void *vec ATTRIBUTE_UNUSED
, size_t size ATTRIBUTE_UNUSED
)
114 struct freelist_d
*ptr
;
115 #ifdef ENABLE_CHECKING
120 /* if its bigger than one of our lists, simply let it go and let GC
126 ptr
->next
= optype_freelist
[size
- 1].next
;;
127 optype_freelist
[size
- 1].next
= ptr
;
132 static inline def_optype
133 allocate_def_optype (unsigned num
)
137 size
= sizeof (struct def_optype_d
) + sizeof (tree
*) * (num
- 1);
138 def_ops
= check_optype_freelist (num
);
140 def_ops
= ggc_alloc (size
);
141 def_ops
->num_defs
= num
;
145 static inline use_optype
146 allocate_use_optype (unsigned num
)
150 size
= sizeof (struct use_optype_d
) + sizeof (tree
*) * (num
- 1);
151 use_ops
= check_optype_freelist (num
);
153 use_ops
= ggc_alloc (size
);
154 use_ops
->num_uses
= num
;
158 static inline vdef_optype
159 allocate_vdef_optype (unsigned num
)
161 vdef_optype vdef_ops
;
163 size
= sizeof (struct vdef_optype_d
) + sizeof (tree
) * ((num
* 2) - 1);
164 vdef_ops
= check_optype_freelist (num
* 2);
166 vdef_ops
= ggc_alloc (size
);
167 vdef_ops
->num_vdefs
= num
;
171 static inline vuse_optype
172 allocate_vuse_optype (unsigned num
)
174 vuse_optype vuse_ops
;
176 size
= sizeof (struct vuse_optype_d
) + sizeof (tree
) * (num
- 1);
177 vuse_ops
= check_optype_freelist (num
);
179 vuse_ops
= ggc_alloc (size
);
180 vuse_ops
->num_vuses
= num
;
185 free_uses (use_optype
*uses
, bool dealloc
)
190 add_optype_freelist (*uses
, (*uses
)->num_uses
);
196 free_defs (def_optype
*defs
, bool dealloc
)
201 add_optype_freelist (*defs
, (*defs
)->num_defs
);
207 free_vuses (vuse_optype
*vuses
, bool dealloc
)
212 add_optype_freelist (*vuses
, (*vuses
)->num_vuses
);
218 free_vdefs (vdef_optype
*vdefs
, bool dealloc
)
223 add_optype_freelist (*vdefs
, (*vdefs
)->num_vdefs
);
229 remove_vuses (tree stmt
)
233 ann
= stmt_ann (stmt
);
235 free_vuses (&(ann
->vuse_ops
), true);
239 remove_vdefs (tree stmt
)
243 ann
= stmt_ann (stmt
);
245 free_vdefs (&(ann
->vdef_ops
), true);
250 init_ssa_operands (void)
254 VARRAY_TREE_PTR_INIT (build_defs
, 5, "build defs");
255 VARRAY_TREE_PTR_INIT (build_uses
, 10, "build uses");
256 VARRAY_TREE_INIT (build_vdefs
, 10, "build vdefs");
257 VARRAY_TREE_INIT (build_vuses
, 10, "build vuses");
259 for (x
= 0; x
< NUM_FREE
; x
++)
260 optype_freelist
[x
].next
= NULL
;
264 fini_ssa_operands (void)
267 for (x
= 0; x
< NUM_FREE
; x
++)
268 optype_freelist
[x
].next
= NULL
;
272 finalize_ssa_defs (tree stmt
)
278 num
= VARRAY_ACTIVE_SIZE (build_defs
);
282 #ifdef ENABLE_CHECKING
283 /* There should only be a single real definition per assignment. */
284 if (TREE_CODE (stmt
) == MODIFY_EXPR
&& num
> 1)
288 def_ops
= allocate_def_optype (num
);
289 for (x
= 0; x
< num
; x
++)
290 def_ops
->defs
[x
] = VARRAY_TREE_PTR (build_defs
, x
);
291 VARRAY_POP_ALL (build_defs
);
293 ann
= stmt_ann (stmt
);
294 ann
->def_ops
= def_ops
;
298 finalize_ssa_uses (tree stmt
)
304 num
= VARRAY_ACTIVE_SIZE (build_uses
);
308 #ifdef ENABLE_CHECKING
311 /* If the pointer to the operand is the statement itself, something is
312 wrong. It means that we are pointing to a local variable (the
313 initial call to get_stmt_operands does not pass a pointer to a
315 for (x
= 0; x
< num
; x
++)
316 if (*(VARRAY_TREE_PTR (build_uses
, x
)) == stmt
)
321 use_ops
= allocate_use_optype (num
);
322 for (x
= 0; x
< num
; x
++)
323 use_ops
->uses
[x
] = VARRAY_TREE_PTR (build_uses
, x
);
324 VARRAY_POP_ALL (build_uses
);
326 ann
= stmt_ann (stmt
);
327 ann
->use_ops
= use_ops
;
331 finalize_ssa_vdefs (tree stmt
)
334 vdef_optype vdef_ops
;
337 num
= VARRAY_ACTIVE_SIZE (build_vdefs
);
341 #ifdef ENABLE_CHECKING
342 /* VDEFs must be entered in pairs of result/uses. */
347 vdef_ops
= allocate_vdef_optype (num
/ 2);
348 for (x
= 0; x
< num
; x
++)
349 vdef_ops
->vdefs
[x
] = VARRAY_TREE (build_vdefs
, x
);
350 VARRAY_CLEAR (build_vdefs
);
352 ann
= stmt_ann (stmt
);
353 ann
->vdef_ops
= vdef_ops
;
357 finalize_ssa_vuses (tree stmt
)
361 vuse_optype vuse_ops
;
364 #ifdef ENABLE_CHECKING
365 if (VARRAY_ACTIVE_SIZE (build_vdefs
) > 0)
367 fprintf (stderr
, "Please finalize VDEFs before finalize VUSES.\n");
372 num
= VARRAY_ACTIVE_SIZE (build_vuses
);
376 /* Remove superfluous VUSE operands. If the statement already has a
377 VDEF operation for a variable 'a', then a VUSE for 'a' is not
378 needed because VDEFs imply a VUSE of the variable. For instance,
379 suppose that variable 'a' is aliased:
385 The VUSE <a_2> is superfluous because it is implied by the VDEF
388 ann
= stmt_ann (stmt
);
389 vdefs
= VDEF_OPS (ann
);
390 if (NUM_VDEFS (vdefs
) > 0)
393 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (build_vuses
); i
++)
396 for (j
= 0; j
< NUM_VDEFS (vdefs
); j
++)
398 tree vuse_var
, vdef_var
;
399 tree vuse
= VARRAY_TREE (build_vuses
, i
);
400 tree vdef
= VDEF_OP (vdefs
, j
);
402 if (TREE_CODE (vuse
) == SSA_NAME
)
403 vuse_var
= SSA_NAME_VAR (vuse
);
407 if (TREE_CODE (vdef
) == SSA_NAME
)
408 vdef_var
= SSA_NAME_VAR (vdef
);
412 if (vuse_var
== vdef_var
)
419 /* If we found a useless VUSE operand, remove it from the
420 operand array by replacing it with the last active element
421 in the operand array (unless the useless VUSE was the
422 last operand, in which case we simply remove it. */
425 if (i
!= VARRAY_ACTIVE_SIZE (build_vuses
) - 1)
427 VARRAY_TREE (build_vuses
, i
)
428 = VARRAY_TREE (build_vuses
,
429 VARRAY_ACTIVE_SIZE (build_vuses
) - 1);
431 VARRAY_POP (build_vuses
);
433 /* We want to rescan the element at this index, unless
434 this was the last element, in which case the loop
441 num
= VARRAY_ACTIVE_SIZE (build_vuses
);
442 /* We could have reduced the size to zero now, however. */
446 vuse_ops
= allocate_vuse_optype (num
);
447 for (x
= 0; x
< num
; x
++)
448 vuse_ops
->vuses
[x
] = VARRAY_TREE (build_vuses
, x
);
449 VARRAY_CLEAR (build_vuses
);
450 ann
->vuse_ops
= vuse_ops
;
454 finalize_ssa_stmt_operands (tree stmt
)
456 #ifdef ENABLE_CHECKING
457 if (check_build_stmt
== NULL
)
461 finalize_ssa_defs (stmt
);
462 finalize_ssa_uses (stmt
);
463 finalize_ssa_vdefs (stmt
);
464 finalize_ssa_vuses (stmt
);
466 #ifdef ENABLE_CHECKING
467 check_build_stmt
= NULL
;
473 verify_start_operands (tree stmt ATTRIBUTE_UNUSED
)
475 #ifdef ENABLE_CHECKING
476 if (VARRAY_ACTIVE_SIZE (build_defs
) > 0
477 || VARRAY_ACTIVE_SIZE (build_uses
) > 0
478 || VARRAY_ACTIVE_SIZE (build_vuses
) > 0
479 || VARRAY_ACTIVE_SIZE (build_vdefs
) > 0)
481 if (check_build_stmt
!= NULL
)
483 check_build_stmt
= stmt
;
488 /* Add DEF_P to the list of pointers to operands defined by STMT. */
491 append_def (tree
*def_p
, tree stmt ATTRIBUTE_UNUSED
)
493 #ifdef ENABLE_CHECKING
494 if (check_build_stmt
!= stmt
)
497 VARRAY_PUSH_TREE_PTR (build_defs
, def_p
);
501 /* Add USE_P to the list of pointers to operands used by STMT. */
504 append_use (tree
*use_p
, tree stmt ATTRIBUTE_UNUSED
)
506 #ifdef ENABLE_CHECKING
507 if (check_build_stmt
!= stmt
)
510 VARRAY_PUSH_TREE_PTR (build_uses
, use_p
);
514 /* Add a new virtual def for variable VAR to statement STMT. If PREV_VOPS
515 is not NULL, the existing entries are preserved and no new entries are
516 added here. This is done to preserve the SSA numbering of virtual
520 append_vdef (tree var
, tree stmt
, voperands_t prev_vops
)
526 #ifdef ENABLE_CHECKING
527 if (check_build_stmt
!= stmt
)
531 ann
= stmt_ann (stmt
);
533 /* Don't allow duplicate entries. */
535 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (build_vdefs
); i
+= 2)
537 tree result
= VARRAY_TREE (build_vdefs
, i
);
539 || (TREE_CODE (result
) == SSA_NAME
540 && var
== SSA_NAME_VAR (result
)))
544 /* If the statement already had virtual definitions, see if any of the
545 existing VDEFs matches VAR. If so, re-use it, otherwise add a new
550 for (i
= 0; i
< NUM_VDEFS (prev_vops
->vdef_ops
); i
++)
552 result
= VDEF_RESULT (prev_vops
->vdef_ops
, i
);
554 || (TREE_CODE (result
) == SSA_NAME
555 && SSA_NAME_VAR (result
) == var
))
557 source
= VDEF_OP (prev_vops
->vdef_ops
, i
);
562 /* If no previous VDEF operand was found for VAR, create one now. */
563 if (source
== NULL_TREE
)
569 VARRAY_PUSH_TREE (build_vdefs
, result
);
570 VARRAY_PUSH_TREE (build_vdefs
, source
);
574 /* Add VAR to the list of virtual uses for STMT. If PREV_VOPS
575 is not NULL, the existing entries are preserved and no new entries are
576 added here. This is done to preserve the SSA numbering of virtual
580 append_vuse (tree var
, tree stmt
, voperands_t prev_vops
)
587 #ifdef ENABLE_CHECKING
588 if (check_build_stmt
!= stmt
)
592 ann
= stmt_ann (stmt
);
594 /* Don't allow duplicate entries. */
595 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (build_vuses
); i
++)
597 tree vuse_var
= VARRAY_TREE (build_vuses
, i
);
599 || (TREE_CODE (vuse_var
) == SSA_NAME
600 && var
== SSA_NAME_VAR (vuse_var
)))
604 /* If the statement already had virtual uses, see if any of the
605 existing VUSEs matches VAR. If so, re-use it, otherwise add a new
610 for (i
= 0; i
< NUM_VUSES (prev_vops
->vuse_ops
); i
++)
612 vuse
= VUSE_OP (prev_vops
->vuse_ops
, i
);
614 || (TREE_CODE (vuse
) == SSA_NAME
615 && SSA_NAME_VAR (vuse
) == var
))
622 /* If VAR existed already in PREV_VOPS, re-use it. */
626 VARRAY_PUSH_TREE (build_vuses
, var
);
630 /* External entry point which by-passes the previous vops mechanism. */
632 add_vuse (tree var
, tree stmt
)
634 append_vuse (var
, stmt
, NULL
);
638 /* Get the operands of statement STMT. Note that repeated calls to
639 get_stmt_operands for the same statement will do nothing until the
640 statement is marked modified by a call to modify_stmt(). */
643 get_stmt_operands (tree stmt
)
647 struct voperands_d prev_vops
;
649 #if defined ENABLE_CHECKING
650 /* The optimizers cannot handle statements that are nothing but a
651 _DECL. This indicates a bug in the gimplifier. */
652 if (SSA_VAR_P (stmt
))
656 /* Ignore error statements. */
657 if (TREE_CODE (stmt
) == ERROR_MARK
)
660 ann
= get_stmt_ann (stmt
);
662 /* If the statement has not been modified, the operands are still valid. */
666 timevar_push (TV_TREE_OPS
);
668 /* Initially assume that the statement has no volatile operands.
669 Statements marked with 'has_volatile_ops' are not processed by the
671 ann
->has_volatile_ops
= false;
673 /* Remove any existing operands as they will be scanned again. */
674 free_defs (&(ann
->def_ops
), true);
675 free_uses (&(ann
->use_ops
), true);
677 /* Before removing existing virtual operands, save them in PREV_VOPS so
678 that we can re-use their SSA versions. */
679 prev_vops
.vdef_ops
= VDEF_OPS (ann
);
680 prev_vops
.vuse_ops
= VUSE_OPS (ann
);
682 /* Dont free the previous values to memory since we're still using them. */
683 free_vdefs (&(ann
->vdef_ops
), false);
684 free_vuses (&(ann
->vuse_ops
), false);
686 start_ssa_stmt_operands (stmt
);
688 code
= TREE_CODE (stmt
);
692 get_expr_operands (stmt
, &TREE_OPERAND (stmt
, 1), opf_none
, &prev_vops
);
693 get_expr_operands (stmt
, &TREE_OPERAND (stmt
, 0), opf_is_def
, &prev_vops
);
697 get_expr_operands (stmt
, &COND_EXPR_COND (stmt
), opf_none
, &prev_vops
);
701 get_expr_operands (stmt
, &SWITCH_COND (stmt
), opf_none
, &prev_vops
);
706 int noutputs
= list_length (ASM_OUTPUTS (stmt
));
707 const char **oconstraints
708 = (const char **) alloca ((noutputs
) * sizeof (const char *));
711 const char *constraint
;
712 bool allows_mem
, allows_reg
, is_inout
;
714 for (i
=0, link
= ASM_OUTPUTS (stmt
); link
;
715 ++i
, link
= TREE_CHAIN (link
))
717 oconstraints
[i
] = constraint
718 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
719 parse_output_constraint (&constraint
, i
, 0, 0,
720 &allows_mem
, &allows_reg
, &is_inout
);
721 if (allows_reg
&& is_inout
)
722 /* This should have been split in gimplify_asm_expr. */
725 if (!allows_reg
&& allows_mem
)
727 tree t
= get_base_address (TREE_VALUE (link
));
729 mark_call_clobbered (t
);
732 get_expr_operands (stmt
, &TREE_VALUE (link
), opf_is_def
,
736 for (link
= ASM_INPUTS (stmt
); link
; link
= TREE_CHAIN (link
))
739 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
740 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0,
741 oconstraints
, &allows_mem
, &allows_reg
);
743 if (!allows_reg
&& allows_mem
)
745 tree t
= get_base_address (TREE_VALUE (link
));
747 mark_call_clobbered (t
);
750 get_expr_operands (stmt
, &TREE_VALUE (link
), 0, &prev_vops
);
753 /* Clobber memory for asm ("" : : : "memory"); */
754 for (link
= ASM_CLOBBERS (stmt
); link
; link
= TREE_CHAIN (link
))
755 if (!strcmp (TREE_STRING_POINTER (TREE_VALUE (link
)), "memory"))
756 add_call_clobber_ops (stmt
, &prev_vops
);
761 get_expr_operands (stmt
, &TREE_OPERAND (stmt
, 0), opf_none
, &prev_vops
);
765 get_expr_operands (stmt
, &GOTO_DESTINATION (stmt
), opf_none
, &prev_vops
);
769 get_expr_operands (stmt
, &LABEL_EXPR_LABEL (stmt
), opf_none
, &prev_vops
);
772 /* These nodes contain no variable references. */
774 case CASE_LABEL_EXPR
:
776 case TRY_FINALLY_EXPR
:
783 /* Notice that if get_expr_operands tries to use &STMT as the operand
784 pointer (which may only happen for USE operands), we will abort in
785 append_use. This default will handle statements like empty statements,
786 CALL_EXPRs or VA_ARG_EXPRs that may appear on the RHS of a statement
787 or as statements themselves. */
788 get_expr_operands (stmt
, &stmt
, opf_none
, &prev_vops
);
792 finalize_ssa_stmt_operands (stmt
);
794 /* Now free the previous virtual ops to memory. */
795 free_vdefs (&(prev_vops
.vdef_ops
), true);
796 free_vuses (&(prev_vops
.vuse_ops
), true);
798 /* Clear the modified bit for STMT. Subsequent calls to
799 get_stmt_operands for this statement will do nothing until the
800 statement is marked modified by a call to modify_stmt(). */
803 timevar_pop (TV_TREE_OPS
);
807 /* Recursively scan the expression pointed by EXPR_P in statement STMT.
808 FLAGS is one of the OPF_* constants modifying how to interpret the
809 operands found. PREV_VOPS is as in append_vdef and append_vuse. */
812 get_expr_operands (tree stmt
, tree
*expr_p
, int flags
, voperands_t prev_vops
)
818 if (expr
== NULL
|| expr
== error_mark_node
)
821 code
= TREE_CODE (expr
);
822 class = TREE_CODE_CLASS (code
);
824 /* Expressions that make no memory references. */
828 || code
== FUNCTION_DECL
829 || code
== EXC_PTR_EXPR
830 || code
== FILTER_EXPR
831 || code
== LABEL_DECL
)
834 /* We could have the address of a component, array member, etc which
835 has interesting variable references. */
836 if (code
== ADDR_EXPR
)
838 enum tree_code subcode
= TREE_CODE (TREE_OPERAND (expr
, 0));
840 /* Taking the address of a variable does not represent a
841 reference to it, but the fact that STMT takes its address will be
842 of interest to some passes (e.g. alias resolution). */
843 add_stmt_operand (expr_p
, stmt
, 0, NULL
);
845 /* If the address is invariant, there may be no interesting variable
846 references inside. */
847 if (is_gimple_min_invariant (expr
))
850 /* There should be no VUSEs created, since the referenced objects are
851 not really accessed. The only operands that we should find here
852 are ARRAY_REF indices which will always be real operands (GIMPLE
853 does not allow non-registers as array indices). */
854 flags
|= opf_no_vops
;
856 /* Avoid recursion. */
858 class = TREE_CODE_CLASS (code
);
859 expr_p
= &TREE_OPERAND (expr
, 0);
863 /* If we found a variable, add it to DEFS or USES depending on the
865 if (SSA_VAR_P (expr
))
867 add_stmt_operand (expr_p
, stmt
, flags
, prev_vops
);
871 /* Pointer dereferences always represent a use of the base pointer. */
872 if (code
== INDIRECT_REF
)
874 tree
*pptr
= &TREE_OPERAND (expr
, 0);
879 if (!aliases_computed_p
)
881 /* If the pointer does not have a memory tag and aliases have not
882 been computed yet, mark the statement as having volatile
883 operands to prevent DOM from entering it in equivalence tables
884 and DCE from killing it. */
885 stmt_ann (stmt
)->has_volatile_ops
= true;
889 ssa_name_ann_t ptr_ann
= NULL
;
891 /* If we have computed aliasing already, check if PTR has
892 flow-sensitive points-to information. */
893 if (TREE_CODE (ptr
) == SSA_NAME
894 && (ptr_ann
= ssa_name_ann (ptr
)) != NULL
895 && ptr_ann
->name_mem_tag
)
897 /* PTR has its own memory tag. Use it. */
898 add_stmt_operand (&ptr_ann
->name_mem_tag
, stmt
, flags
,
903 /* If PTR is not an SSA_NAME or it doesn't have a name
904 tag, use its type memory tag. */
907 /* If we are emitting debugging dumps, display a warning if
908 PTR is an SSA_NAME with no flow-sensitive alias
909 information. That means that we may need to compute
912 && TREE_CODE (ptr
) == SSA_NAME
916 "NOTE: no flow-sensitive alias info for ");
917 print_generic_expr (dump_file
, ptr
, dump_flags
);
918 fprintf (dump_file
, " in ");
919 print_generic_stmt (dump_file
, stmt
, dump_flags
);
922 if (TREE_CODE (ptr
) == SSA_NAME
)
923 ptr
= SSA_NAME_VAR (ptr
);
925 add_stmt_operand (&ann
->type_mem_tag
, stmt
, flags
, prev_vops
);
930 /* If a constant is used as a pointer, we can't generate a real
931 operand for it but we mark the statement volatile to prevent
932 optimizations from messing things up. */
933 else if (TREE_CODE (ptr
) == INTEGER_CST
)
935 stmt_ann (stmt
)->has_volatile_ops
= true;
939 /* Everything else *should* have been folded elsewhere, but users
940 are smarter than we in finding ways to write invalid code. We
941 cannot just abort here. If we were absolutely certain that we
942 do handle all valid cases, then we could just do nothing here.
943 That seems optimistic, so attempt to do something logical... */
944 else if ((TREE_CODE (ptr
) == PLUS_EXPR
|| TREE_CODE (ptr
) == MINUS_EXPR
)
945 && TREE_CODE (TREE_OPERAND (ptr
, 0)) == ADDR_EXPR
946 && TREE_CODE (TREE_OPERAND (ptr
, 1)) == INTEGER_CST
)
948 /* Make sure we know the object is addressable. */
949 pptr
= &TREE_OPERAND (ptr
, 0);
950 add_stmt_operand (pptr
, stmt
, 0, NULL
);
952 /* Mark the object itself with a VUSE. */
953 pptr
= &TREE_OPERAND (*pptr
, 0);
954 get_expr_operands (stmt
, pptr
, flags
, prev_vops
);
958 /* Ok, this isn't even is_gimple_min_invariant. Something's broke. */
962 /* Add a USE operand for the base pointer. */
963 get_expr_operands (stmt
, pptr
, opf_none
, prev_vops
);
967 /* Treat array references as references to the virtual variable
968 representing the array. The virtual variable for an ARRAY_REF
969 is the VAR_DECL for the array. */
970 if (code
== ARRAY_REF
)
972 /* Add the virtual variable for the ARRAY_REF to VDEFS or VUSES
973 according to the value of IS_DEF. Recurse if the LHS of the
974 ARRAY_REF node is not a regular variable. */
975 if (SSA_VAR_P (TREE_OPERAND (expr
, 0)))
976 add_stmt_operand (expr_p
, stmt
, flags
, prev_vops
);
978 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 0), flags
, prev_vops
);
980 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 1), opf_none
, prev_vops
);
984 /* Similarly to arrays, references to compound variables (complex types
985 and structures/unions) are globbed.
987 FIXME: This means that
993 will not be constant propagated because the two partial
994 definitions to 'a' will kill each other. Note that SRA may be
995 able to fix this problem if 'a' can be scalarized. */
996 if (code
== IMAGPART_EXPR
|| code
== REALPART_EXPR
|| code
== COMPONENT_REF
)
998 /* If the LHS of the compound reference is not a regular variable,
999 recurse to keep looking for more operands in the subexpression. */
1000 if (SSA_VAR_P (TREE_OPERAND (expr
, 0)))
1001 add_stmt_operand (expr_p
, stmt
, flags
, prev_vops
);
1003 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 0), flags
, prev_vops
);
1008 /* Function calls. Add every argument to USES. If the callee is
1009 neither pure nor const, create a VDEF reference for GLOBAL_VAR
1010 (See find_vars_r). */
1011 if (code
== CALL_EXPR
)
1014 int call_flags
= call_expr_flags (expr
);
1016 /* Find uses in the called function. */
1017 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 0), opf_none
, prev_vops
);
1019 for (op
= TREE_OPERAND (expr
, 1); op
; op
= TREE_CHAIN (op
))
1020 get_expr_operands (stmt
, &TREE_VALUE (op
), opf_none
, prev_vops
);
1022 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 2), opf_none
, prev_vops
);
1024 if (bitmap_first_set_bit (call_clobbered_vars
) >= 0)
1026 /* A 'pure' or a 'const' functions never call clobber anything.
1027 A 'noreturn' function might, but since we don't return anyway
1028 there is no point in recording that. */
1030 & (ECF_PURE
| ECF_CONST
| ECF_NORETURN
)))
1031 add_call_clobber_ops (stmt
, prev_vops
);
1032 else if (!(call_flags
& (ECF_CONST
| ECF_NORETURN
)))
1033 add_call_read_ops (stmt
, prev_vops
);
1035 else if (!aliases_computed_p
)
1036 stmt_ann (stmt
)->has_volatile_ops
= true;
1042 if (code
== TREE_LIST
)
1046 for (op
= expr
; op
; op
= TREE_CHAIN (op
))
1047 get_expr_operands (stmt
, &TREE_VALUE (op
), flags
, prev_vops
);
1053 if (code
== MODIFY_EXPR
)
1055 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 1), opf_none
, prev_vops
);
1056 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 0), opf_is_def
, prev_vops
);
1061 /* Mark VA_ARG_EXPR nodes as making volatile references. FIXME,
1062 this is needed because we currently do not gimplify VA_ARG_EXPR
1064 if (code
== VA_ARG_EXPR
)
1066 stmt_ann (stmt
)->has_volatile_ops
= true;
1070 /* Unary expressions. */
1072 || code
== TRUTH_NOT_EXPR
1073 || code
== BIT_FIELD_REF
1074 || code
== CONSTRUCTOR
)
1076 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 0), flags
, prev_vops
);
1080 /* Binary expressions. */
1083 || code
== TRUTH_AND_EXPR
1084 || code
== TRUTH_OR_EXPR
1085 || code
== TRUTH_XOR_EXPR
1086 || code
== COMPOUND_EXPR
)
1088 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 0), flags
, prev_vops
);
1089 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 1), flags
, prev_vops
);
1093 /* If we get here, something has gone wrong. */
1094 fprintf (stderr
, "unhandled expression in get_expr_operands():\n");
1096 fputs ("\n", stderr
);
1101 /* Add *VAR_P to the appropriate operand array of STMT. FLAGS is as in
1102 get_expr_operands. If *VAR_P is a GIMPLE register, it will be added to
1103 the statement's real operands, otherwise it is added to virtual
1106 PREV_VOPS is used when adding virtual operands to statements that
1107 already had them (See append_vdef and append_vuse). */
1110 add_stmt_operand (tree
*var_p
, tree stmt
, int flags
, voperands_t prev_vops
)
1120 s_ann
= stmt_ann (stmt
);
1122 /* If the operand is an ADDR_EXPR, add its operand to the list of
1123 variables that have had their address taken in this statement. */
1124 if (TREE_CODE (var
) == ADDR_EXPR
)
1126 note_addressable (TREE_OPERAND (var
, 0), s_ann
);
1130 /* If the original variable is not a scalar, it will be added to the list
1131 of virtual operands. In that case, use its base symbol as the virtual
1132 variable representing it. */
1133 is_real_op
= is_gimple_reg (var
);
1134 if (!is_real_op
&& !DECL_P (var
))
1135 var
= get_virtual_var (var
);
1137 /* If VAR is not a variable that we care to optimize, do nothing. */
1138 if (var
== NULL_TREE
|| !SSA_VAR_P (var
))
1141 sym
= (TREE_CODE (var
) == SSA_NAME
? SSA_NAME_VAR (var
) : var
);
1142 v_ann
= var_ann (sym
);
1144 /* FIXME: We currently refuse to optimize variables that have hidden uses
1145 (variables used in VLA declarations, MD builtin calls and variables
1146 from the parent function in nested functions). This is because not
1147 all uses of these variables are exposed in the IL or the statements
1148 that reference them are not in GIMPLE form. If that's the case, mark
1149 the statement as having volatile operands and return. */
1150 if (v_ann
->has_hidden_use
)
1152 s_ann
->has_volatile_ops
= true;
1156 /* Don't expose volatile variables to the optimizers. */
1157 if (TREE_THIS_VOLATILE (sym
))
1159 s_ann
->has_volatile_ops
= true;
1165 /* The variable is a GIMPLE register. Add it to real operands. */
1166 if (flags
& opf_is_def
)
1167 append_def (var_p
, stmt
);
1169 append_use (var_p
, stmt
);
1173 varray_type aliases
;
1175 /* The variable is not a GIMPLE register. Add it (or its aliases) to
1176 virtual operands, unless the caller has specifically requested
1177 not to add virtual operands (used when adding operands inside an
1178 ADDR_EXPR expression). */
1179 if (flags
& opf_no_vops
)
1182 aliases
= v_ann
->may_aliases
;
1184 /* If alias information hasn't been computed yet, then
1185 addressable variables will not be an alias tag nor will they
1186 have aliases. In this case, mark the statement as having
1187 volatile operands. */
1188 if (!aliases_computed_p
&& may_be_aliased (var
))
1189 s_ann
->has_volatile_ops
= true;
1191 if (aliases
== NULL
)
1193 /* The variable is not aliased or it is an alias tag. */
1194 if (flags
& opf_is_def
)
1196 append_vdef (var
, stmt
, prev_vops
);
1197 if (v_ann
->is_alias_tag
)
1198 s_ann
->makes_aliased_stores
= 1;
1202 append_vuse (var
, stmt
, prev_vops
);
1203 if (v_ann
->is_alias_tag
)
1204 s_ann
->makes_aliased_loads
= 1;
1211 /* The variable is aliased. Add its aliases to the virtual
1213 if (VARRAY_ACTIVE_SIZE (aliases
) == 0)
1216 if (flags
& opf_is_def
)
1218 /* If the variable is also an alias tag, add a virtual
1219 operand for it, otherwise we will miss representing
1220 references to the members of the variable's alias set.
1221 This fixes the bug in gcc.c-torture/execute/20020503-1.c. */
1222 if (v_ann
->is_alias_tag
)
1223 append_vdef (var
, stmt
, prev_vops
);
1225 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (aliases
); i
++)
1226 append_vdef (VARRAY_TREE (aliases
, i
), stmt
, prev_vops
);
1228 s_ann
->makes_aliased_stores
= 1;
1232 if (v_ann
->is_alias_tag
)
1233 append_vuse (var
, stmt
, prev_vops
);
1235 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (aliases
); i
++)
1236 append_vuse (VARRAY_TREE (aliases
, i
), stmt
, prev_vops
);
1238 s_ann
->makes_aliased_loads
= 1;
1244 /* Record that VAR had its address taken in the statement with annotations
1248 note_addressable (tree var
, stmt_ann_t s_ann
)
1250 var
= get_base_address (var
);
1251 if (var
&& SSA_VAR_P (var
))
1253 if (s_ann
->addresses_taken
== NULL
)
1254 s_ann
->addresses_taken
= BITMAP_GGC_ALLOC ();
1255 bitmap_set_bit (s_ann
->addresses_taken
, var_ann (var
)->uid
);
1260 /* Add clobbering definitions for .GLOBAL_VAR or for each of the call
1261 clobbered variables in the function. */
1264 add_call_clobber_ops (tree stmt
, voperands_t prev_vops
)
1266 /* Functions that are not const, pure or never return may clobber
1267 call-clobbered variables. */
1268 stmt_ann (stmt
)->makes_clobbering_call
= true;
1270 /* If we had created .GLOBAL_VAR earlier, use it. Otherwise, add a VDEF
1271 operand for every call clobbered variable. See compute_may_aliases for
1272 the heuristic used to decide whether to create .GLOBAL_VAR or not. */
1274 add_stmt_operand (&global_var
, stmt
, opf_is_def
, prev_vops
);
1279 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars
, 0, i
,
1281 tree var
= referenced_var (i
);
1283 /* If VAR is read-only, don't add a VDEF, just a VUSE operand. */
1284 if (!TREE_READONLY (var
))
1285 add_stmt_operand (&var
, stmt
, opf_is_def
, prev_vops
);
1287 add_stmt_operand (&var
, stmt
, opf_none
, prev_vops
);
1293 /* Add VUSE operands for .GLOBAL_VAR or all call clobbered variables in the
1297 add_call_read_ops (tree stmt
, voperands_t prev_vops
)
1299 /* Otherwise, if the function is not pure, it may reference memory. Add
1300 a VUSE for .GLOBAL_VAR if it has been created. Otherwise, add a VUSE
1301 for each call-clobbered variable. See add_referenced_var for the
1302 heuristic used to decide whether to create .GLOBAL_VAR. */
1304 add_stmt_operand (&global_var
, stmt
, opf_none
, prev_vops
);
1309 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars
, 0, i
,
1311 tree var
= referenced_var (i
);
1312 add_stmt_operand (&var
, stmt
, opf_none
, prev_vops
);
1317 #include "gt-tree-ssa-operands.h"