1 /* SSA operands management for trees.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "tree-pretty-print.h"
29 #include "gimple-pretty-print.h"
30 #include "tree-flow.h"
31 #include "tree-inline.h"
32 #include "tree-pass.h"
35 #include "langhooks.h"
36 #include "diagnostic-core.h"
39 /* This file contains the code required to manage the operands cache of the
40 SSA optimizer. For every stmt, we maintain an operand cache in the stmt
41 annotation. This cache contains operands that will be of interest to
42 optimizers and other passes wishing to manipulate the IL.
44 The operand type are broken up into REAL and VIRTUAL operands. The real
45 operands are represented as pointers into the stmt's operand tree. Thus
46 any manipulation of the real operands will be reflected in the actual tree.
47 Virtual operands are represented solely in the cache, although the base
48 variable for the SSA_NAME may, or may not occur in the stmt's tree.
49 Manipulation of the virtual operands will not be reflected in the stmt tree.
51 The routines in this file are concerned with creating this operand cache
54 The operand tree is the parsed by the various get_* routines which look
55 through the stmt tree for the occurrence of operands which may be of
56 interest, and calls are made to the append_* routines whenever one is
57 found. There are 4 of these routines, each representing one of the
58 4 types of operands. Defs, Uses, Virtual Uses, and Virtual May Defs.
60 The append_* routines check for duplication, and simply keep a list of
61 unique objects for each operand type in the build_* extendable vectors.
63 Once the stmt tree is completely parsed, the finalize_ssa_operands()
64 routine is called, which proceeds to perform the finalization routine
65 on each of the 4 operand vectors which have been built up.
67 If the stmt had a previous operand cache, the finalization routines
68 attempt to match up the new operands with the old ones. If it's a perfect
69 match, the old vector is simply reused. If it isn't a perfect match, then
70 a new vector is created and the new operands are placed there. For
71 virtual operands, if the previous cache had SSA_NAME version of a
72 variable, and that same variable occurs in the same operands cache, then
73 the new cache vector will also get the same SSA_NAME.
75 i.e., if a stmt had a VUSE of 'a_5', and 'a' occurs in the new
76 operand vector for VUSE, then the new vector will also be modified
77 such that it contains 'a_5' rather than 'a'. */
80 /* Flags to describe operand properties in helpers. */
82 /* By default, operands are loaded. */
85 /* Operand is the target of an assignment expression or a
86 call-clobbered variable. */
87 #define opf_def (1 << 0)
89 /* No virtual operands should be created in the expression. This is used
90 when traversing ADDR_EXPR nodes which have different semantics than
91 other expressions. Inside an ADDR_EXPR node, the only operands that we
92 need to consider are indices into arrays. For instance, &a.b[i] should
93 generate a USE of 'i' but it should not generate a VUSE for 'a' nor a
95 #define opf_no_vops (1 << 1)
97 /* Operand is an implicit reference. This is used to distinguish
98 explicit assignments in the form of MODIFY_EXPR from
99 clobbering sites like function calls or ASM_EXPRs. */
100 #define opf_implicit (1 << 2)
102 /* Operand is in a place where address-taken does not imply addressable. */
103 #define opf_non_addressable (1 << 3)
105 /* Operand is in a place where opf_non_addressable does not apply. */
106 #define opf_not_non_addressable (1 << 4)
108 /* Array for building all the def operands. */
109 static VEC(tree
,heap
) *build_defs
;
111 /* Array for building all the use operands. */
112 static VEC(tree
,heap
) *build_uses
;
114 /* The built VDEF operand. */
115 static tree build_vdef
;
117 /* The built VUSE operand. */
118 static tree build_vuse
;
120 /* Bitmap obstack for our datastructures that needs to survive across
121 compilations of multiple functions. */
122 static bitmap_obstack operands_bitmap_obstack
;
124 static void get_expr_operands (gimple
, tree
*, int);
126 /* Number of functions with initialized ssa_operands. */
127 static int n_initialized
= 0;
129 /* Return the DECL_UID of the base variable of T. */
131 static inline unsigned
132 get_name_decl (const_tree t
)
134 if (TREE_CODE (t
) != SSA_NAME
)
137 return DECL_UID (SSA_NAME_VAR (t
));
141 /* Return true if the SSA operands cache is active. */
144 ssa_operands_active (void)
146 /* This function may be invoked from contexts where CFUN is NULL
147 (IPA passes), return false for now. FIXME: operands may be
148 active in each individual function, maybe this function should
149 take CFUN as a parameter. */
153 return cfun
->gimple_df
&& gimple_ssa_operands (cfun
)->ops_active
;
157 /* Create the VOP variable, an artificial global variable to act as a
158 representative of all of the virtual operands FUD chain. */
161 create_vop_var (struct function
*fn
)
165 gcc_assert (fn
->gimple_df
->vop
== NULL_TREE
);
167 global_var
= build_decl (BUILTINS_LOCATION
, VAR_DECL
,
168 get_identifier (".MEM"),
170 DECL_ARTIFICIAL (global_var
) = 1;
171 TREE_READONLY (global_var
) = 0;
172 DECL_EXTERNAL (global_var
) = 1;
173 TREE_STATIC (global_var
) = 1;
174 TREE_USED (global_var
) = 1;
175 DECL_CONTEXT (global_var
) = NULL_TREE
;
176 TREE_THIS_VOLATILE (global_var
) = 0;
177 TREE_ADDRESSABLE (global_var
) = 0;
178 VAR_DECL_IS_VIRTUAL_OPERAND (global_var
) = 1;
180 create_var_ann (global_var
);
181 add_referenced_var_1 (global_var
, fn
);
182 fn
->gimple_df
->vop
= global_var
;
185 /* These are the sizes of the operand memory buffer in bytes which gets
186 allocated each time more operands space is required. The final value is
187 the amount that is allocated every time after that.
188 In 1k we can fit 25 use operands (or 63 def operands) on a host with
189 8 byte pointers, that would be 10 statements each with 1 def and 2
192 #define OP_SIZE_INIT 0
193 #define OP_SIZE_1 (1024 - sizeof (void *))
194 #define OP_SIZE_2 (1024 * 4 - sizeof (void *))
195 #define OP_SIZE_3 (1024 * 16 - sizeof (void *))
197 /* Initialize the operand cache routines. */
200 init_ssa_operands (struct function
*fn
)
202 if (!n_initialized
++)
204 build_defs
= VEC_alloc (tree
, heap
, 5);
205 build_uses
= VEC_alloc (tree
, heap
, 10);
206 build_vuse
= NULL_TREE
;
207 build_vdef
= NULL_TREE
;
208 bitmap_obstack_initialize (&operands_bitmap_obstack
);
211 gcc_assert (gimple_ssa_operands (fn
)->operand_memory
== NULL
);
212 gimple_ssa_operands (fn
)->operand_memory_index
213 = gimple_ssa_operands (fn
)->ssa_operand_mem_size
;
214 gimple_ssa_operands (fn
)->ops_active
= true;
215 gimple_ssa_operands (fn
)->ssa_operand_mem_size
= OP_SIZE_INIT
;
220 /* Dispose of anything required by the operand routines. */
223 fini_ssa_operands (void)
225 struct ssa_operand_memory_d
*ptr
;
227 if (!--n_initialized
)
229 VEC_free (tree
, heap
, build_defs
);
230 VEC_free (tree
, heap
, build_uses
);
231 build_vdef
= NULL_TREE
;
232 build_vuse
= NULL_TREE
;
235 gimple_ssa_operands (cfun
)->free_defs
= NULL
;
236 gimple_ssa_operands (cfun
)->free_uses
= NULL
;
238 while ((ptr
= gimple_ssa_operands (cfun
)->operand_memory
) != NULL
)
240 gimple_ssa_operands (cfun
)->operand_memory
241 = gimple_ssa_operands (cfun
)->operand_memory
->next
;
245 gimple_ssa_operands (cfun
)->ops_active
= false;
248 bitmap_obstack_release (&operands_bitmap_obstack
);
250 cfun
->gimple_df
->vop
= NULL_TREE
;
254 /* Return memory for an operand of size SIZE. */
257 ssa_operand_alloc (unsigned size
)
261 gcc_assert (size
== sizeof (struct use_optype_d
)
262 || size
== sizeof (struct def_optype_d
));
264 if (gimple_ssa_operands (cfun
)->operand_memory_index
+ size
265 >= gimple_ssa_operands (cfun
)->ssa_operand_mem_size
)
267 struct ssa_operand_memory_d
*ptr
;
269 switch (gimple_ssa_operands (cfun
)->ssa_operand_mem_size
)
272 gimple_ssa_operands (cfun
)->ssa_operand_mem_size
= OP_SIZE_1
;
275 gimple_ssa_operands (cfun
)->ssa_operand_mem_size
= OP_SIZE_2
;
279 gimple_ssa_operands (cfun
)->ssa_operand_mem_size
= OP_SIZE_3
;
286 ptr
= ggc_alloc_ssa_operand_memory_d (sizeof (void *)
287 + gimple_ssa_operands (cfun
)->ssa_operand_mem_size
);
289 ptr
->next
= gimple_ssa_operands (cfun
)->operand_memory
;
290 gimple_ssa_operands (cfun
)->operand_memory
= ptr
;
291 gimple_ssa_operands (cfun
)->operand_memory_index
= 0;
294 ptr
= &(gimple_ssa_operands (cfun
)->operand_memory
295 ->mem
[gimple_ssa_operands (cfun
)->operand_memory_index
]);
296 gimple_ssa_operands (cfun
)->operand_memory_index
+= size
;
301 /* Allocate a DEF operand. */
303 static inline struct def_optype_d
*
306 struct def_optype_d
*ret
;
307 if (gimple_ssa_operands (cfun
)->free_defs
)
309 ret
= gimple_ssa_operands (cfun
)->free_defs
;
310 gimple_ssa_operands (cfun
)->free_defs
311 = gimple_ssa_operands (cfun
)->free_defs
->next
;
314 ret
= (struct def_optype_d
*)
315 ssa_operand_alloc (sizeof (struct def_optype_d
));
320 /* Allocate a USE operand. */
322 static inline struct use_optype_d
*
325 struct use_optype_d
*ret
;
326 if (gimple_ssa_operands (cfun
)->free_uses
)
328 ret
= gimple_ssa_operands (cfun
)->free_uses
;
329 gimple_ssa_operands (cfun
)->free_uses
330 = gimple_ssa_operands (cfun
)->free_uses
->next
;
333 ret
= (struct use_optype_d
*)
334 ssa_operand_alloc (sizeof (struct use_optype_d
));
339 /* Adds OP to the list of defs after LAST. */
341 static inline def_optype_p
342 add_def_op (tree
*op
, def_optype_p last
)
344 def_optype_p new_def
;
346 new_def
= alloc_def ();
347 DEF_OP_PTR (new_def
) = op
;
348 last
->next
= new_def
;
349 new_def
->next
= NULL
;
354 /* Adds OP to the list of uses of statement STMT after LAST. */
356 static inline use_optype_p
357 add_use_op (gimple stmt
, tree
*op
, use_optype_p last
)
359 use_optype_p new_use
;
361 new_use
= alloc_use ();
362 USE_OP_PTR (new_use
)->use
= op
;
363 link_imm_use_stmt (USE_OP_PTR (new_use
), *op
, stmt
);
364 last
->next
= new_use
;
365 new_use
->next
= NULL
;
371 /* Takes elements from build_defs and turns them into def operands of STMT.
372 TODO -- Make build_defs VEC of tree *. */
375 finalize_ssa_defs (gimple stmt
)
378 struct def_optype_d new_list
;
379 def_optype_p old_ops
, last
;
380 unsigned int num
= VEC_length (tree
, build_defs
);
382 /* There should only be a single real definition per assignment. */
383 gcc_assert ((stmt
&& gimple_code (stmt
) != GIMPLE_ASSIGN
) || num
<= 1);
385 /* Pre-pend the vdef we may have built. */
386 if (build_vdef
!= NULL_TREE
)
388 tree oldvdef
= gimple_vdef (stmt
);
390 && TREE_CODE (oldvdef
) == SSA_NAME
)
391 oldvdef
= SSA_NAME_VAR (oldvdef
);
392 if (oldvdef
!= build_vdef
)
393 gimple_set_vdef (stmt
, build_vdef
);
394 VEC_safe_insert (tree
, heap
, build_defs
, 0, (tree
)gimple_vdef_ptr (stmt
));
398 new_list
.next
= NULL
;
401 old_ops
= gimple_def_ops (stmt
);
405 /* Clear and unlink a no longer necessary VDEF. */
406 if (build_vdef
== NULL_TREE
407 && gimple_vdef (stmt
) != NULL_TREE
)
409 if (TREE_CODE (gimple_vdef (stmt
)) == SSA_NAME
)
411 unlink_stmt_vdef (stmt
);
412 release_ssa_name (gimple_vdef (stmt
));
414 gimple_set_vdef (stmt
, NULL_TREE
);
417 /* If we have a non-SSA_NAME VDEF, mark it for renaming. */
418 if (gimple_vdef (stmt
)
419 && TREE_CODE (gimple_vdef (stmt
)) != SSA_NAME
)
420 mark_sym_for_renaming (gimple_vdef (stmt
));
422 /* Check for the common case of 1 def that hasn't changed. */
423 if (old_ops
&& old_ops
->next
== NULL
&& num
== 1
424 && (tree
*) VEC_index (tree
, build_defs
, 0) == DEF_OP_PTR (old_ops
))
427 /* If there is anything in the old list, free it. */
430 old_ops
->next
= gimple_ssa_operands (cfun
)->free_defs
;
431 gimple_ssa_operands (cfun
)->free_defs
= old_ops
;
434 /* If there is anything remaining in the build_defs list, simply emit it. */
435 for ( ; new_i
< num
; new_i
++)
436 last
= add_def_op ((tree
*) VEC_index (tree
, build_defs
, new_i
), last
);
438 /* Now set the stmt's operands. */
439 gimple_set_def_ops (stmt
, new_list
.next
);
443 /* Takes elements from build_uses and turns them into use operands of STMT.
444 TODO -- Make build_uses VEC of tree *. */
447 finalize_ssa_uses (gimple stmt
)
450 struct use_optype_d new_list
;
451 use_optype_p old_ops
, ptr
, last
;
453 /* Pre-pend the VUSE we may have built. */
454 if (build_vuse
!= NULL_TREE
)
456 tree oldvuse
= gimple_vuse (stmt
);
458 && TREE_CODE (oldvuse
) == SSA_NAME
)
459 oldvuse
= SSA_NAME_VAR (oldvuse
);
460 if (oldvuse
!= (build_vuse
!= NULL_TREE
461 ? build_vuse
: build_vdef
))
462 gimple_set_vuse (stmt
, NULL_TREE
);
463 VEC_safe_insert (tree
, heap
, build_uses
, 0, (tree
)gimple_vuse_ptr (stmt
));
466 new_list
.next
= NULL
;
469 old_ops
= gimple_use_ops (stmt
);
471 /* Clear a no longer necessary VUSE. */
472 if (build_vuse
== NULL_TREE
473 && gimple_vuse (stmt
) != NULL_TREE
)
474 gimple_set_vuse (stmt
, NULL_TREE
);
476 /* If there is anything in the old list, free it. */
479 for (ptr
= old_ops
; ptr
; ptr
= ptr
->next
)
480 delink_imm_use (USE_OP_PTR (ptr
));
481 old_ops
->next
= gimple_ssa_operands (cfun
)->free_uses
;
482 gimple_ssa_operands (cfun
)->free_uses
= old_ops
;
485 /* If we added a VUSE, make sure to set the operand if it is not already
486 present and mark it for renaming. */
487 if (build_vuse
!= NULL_TREE
488 && gimple_vuse (stmt
) == NULL_TREE
)
490 gimple_set_vuse (stmt
, gimple_vop (cfun
));
491 mark_sym_for_renaming (gimple_vop (cfun
));
494 /* Now create nodes for all the new nodes. */
495 for (new_i
= 0; new_i
< VEC_length (tree
, build_uses
); new_i
++)
496 last
= add_use_op (stmt
,
497 (tree
*) VEC_index (tree
, build_uses
, new_i
),
500 /* Now set the stmt's operands. */
501 gimple_set_use_ops (stmt
, new_list
.next
);
505 /* Clear the in_list bits and empty the build array for VDEFs and
509 cleanup_build_arrays (void)
511 build_vdef
= NULL_TREE
;
512 build_vuse
= NULL_TREE
;
513 VEC_truncate (tree
, build_defs
, 0);
514 VEC_truncate (tree
, build_uses
, 0);
518 /* Finalize all the build vectors, fill the new ones into INFO. */
521 finalize_ssa_stmt_operands (gimple stmt
)
523 finalize_ssa_defs (stmt
);
524 finalize_ssa_uses (stmt
);
525 cleanup_build_arrays ();
529 /* Start the process of building up operands vectors in INFO. */
532 start_ssa_stmt_operands (void)
534 gcc_assert (VEC_length (tree
, build_defs
) == 0);
535 gcc_assert (VEC_length (tree
, build_uses
) == 0);
536 gcc_assert (build_vuse
== NULL_TREE
);
537 gcc_assert (build_vdef
== NULL_TREE
);
541 /* Add DEF_P to the list of pointers to operands. */
544 append_def (tree
*def_p
)
546 VEC_safe_push (tree
, heap
, build_defs
, (tree
) def_p
);
550 /* Add USE_P to the list of pointers to operands. */
553 append_use (tree
*use_p
)
555 VEC_safe_push (tree
, heap
, build_uses
, (tree
) use_p
);
559 /* Add VAR to the set of variables that require a VDEF operator. */
562 append_vdef (tree var
)
567 gcc_assert ((build_vdef
== NULL_TREE
568 || build_vdef
== var
)
569 && (build_vuse
== NULL_TREE
570 || build_vuse
== var
));
577 /* Add VAR to the set of variables that require a VUSE operator. */
580 append_vuse (tree var
)
585 gcc_assert (build_vuse
== NULL_TREE
586 || build_vuse
== var
);
591 /* Add virtual operands for STMT. FLAGS is as in get_expr_operands. */
594 add_virtual_operand (gimple stmt ATTRIBUTE_UNUSED
, int flags
)
596 /* Add virtual operands to the stmt, unless the caller has specifically
597 requested not to do that (used when adding operands inside an
598 ADDR_EXPR expression). */
599 if (flags
& opf_no_vops
)
602 gcc_assert (!is_gimple_debug (stmt
));
605 append_vdef (gimple_vop (cfun
));
607 append_vuse (gimple_vop (cfun
));
611 /* Add *VAR_P to the appropriate operand array for statement STMT.
612 FLAGS is as in get_expr_operands. If *VAR_P is a GIMPLE register,
613 it will be added to the statement's real operands, otherwise it is
614 added to virtual operands. */
617 add_stmt_operand (tree
*var_p
, gimple stmt
, int flags
)
621 gcc_assert (SSA_VAR_P (*var_p
));
624 sym
= (TREE_CODE (var
) == SSA_NAME
? SSA_NAME_VAR (var
) : var
);
626 /* Mark statements with volatile operands. */
627 if (!(flags
& opf_no_vops
)
628 && TREE_THIS_VOLATILE (sym
))
629 gimple_set_has_volatile_ops (stmt
, true);
631 if (is_gimple_reg (sym
))
633 /* The variable is a GIMPLE register. Add it to real operands. */
640 add_virtual_operand (stmt
, flags
);
643 /* Mark the base address of REF as having its address taken.
644 REF may be a single variable whose address has been taken or any
645 other valid GIMPLE memory reference (structure reference, array,
649 mark_address_taken (tree ref
)
653 /* Note that it is *NOT OKAY* to use the target of a COMPONENT_REF
654 as the only thing we take the address of. If VAR is a structure,
655 taking the address of a field means that the whole structure may
656 be referenced using pointer arithmetic. See PR 21407 and the
657 ensuing mailing list discussion. */
658 var
= get_base_address (ref
);
662 TREE_ADDRESSABLE (var
) = 1;
663 else if (TREE_CODE (var
) == MEM_REF
664 && TREE_CODE (TREE_OPERAND (var
, 0)) == ADDR_EXPR
665 && DECL_P (TREE_OPERAND (TREE_OPERAND (var
, 0), 0)))
666 TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (var
, 0), 0)) = 1;
671 /* A subroutine of get_expr_operands to handle MEM_REF.
673 STMT is the statement being processed, EXPR is the MEM_REF
676 FLAGS is as in get_expr_operands.
678 RECURSE_ON_BASE should be set to true if we want to continue
679 calling get_expr_operands on the base pointer, and false if
680 something else will do it for us. */
683 get_indirect_ref_operands (gimple stmt
, tree expr
, int flags
,
684 bool recurse_on_base
)
686 tree
*pptr
= &TREE_OPERAND (expr
, 0);
688 if (!(flags
& opf_no_vops
)
689 && TREE_THIS_VOLATILE (expr
))
690 gimple_set_has_volatile_ops (stmt
, true);
693 add_virtual_operand (stmt
, flags
);
695 /* If requested, add a USE operand for the base pointer. */
697 get_expr_operands (stmt
, pptr
,
698 opf_non_addressable
| opf_use
699 | (flags
& (opf_no_vops
|opf_not_non_addressable
)));
703 /* A subroutine of get_expr_operands to handle TARGET_MEM_REF. */
706 get_tmr_operands (gimple stmt
, tree expr
, int flags
)
708 if (!(flags
& opf_no_vops
)
709 && TREE_THIS_VOLATILE (expr
))
710 gimple_set_has_volatile_ops (stmt
, true);
712 /* First record the real operands. */
713 get_expr_operands (stmt
, &TMR_BASE (expr
), opf_use
| (flags
& opf_no_vops
));
714 get_expr_operands (stmt
, &TMR_INDEX (expr
), opf_use
| (flags
& opf_no_vops
));
715 get_expr_operands (stmt
, &TMR_INDEX2 (expr
), opf_use
| (flags
& opf_no_vops
));
717 add_virtual_operand (stmt
, flags
);
721 /* If STMT is a call that may clobber globals and other symbols that
722 escape, add them to the VDEF/VUSE lists for it. */
725 maybe_add_call_vops (gimple stmt
)
727 int call_flags
= gimple_call_flags (stmt
);
729 /* If aliases have been computed already, add VDEF or VUSE
730 operands for all the symbols that have been found to be
732 if (!(call_flags
& ECF_NOVOPS
))
734 /* A 'pure' or a 'const' function never call-clobbers anything.
735 A 'noreturn' function might, but since we don't return anyway
736 there is no point in recording that. */
737 if (!(call_flags
& (ECF_PURE
| ECF_CONST
| ECF_NORETURN
)))
738 add_virtual_operand (stmt
, opf_def
);
739 else if (!(call_flags
& ECF_CONST
))
740 add_virtual_operand (stmt
, opf_use
);
745 /* Scan operands in the ASM_EXPR stmt referred to in INFO. */
748 get_asm_expr_operands (gimple stmt
)
751 const char **oconstraints
;
752 const char *constraint
;
753 bool allows_mem
, allows_reg
, is_inout
;
755 noutputs
= gimple_asm_noutputs (stmt
);
756 oconstraints
= (const char **) alloca ((noutputs
) * sizeof (const char *));
758 /* Gather all output operands. */
759 for (i
= 0; i
< gimple_asm_noutputs (stmt
); i
++)
761 tree link
= gimple_asm_output_op (stmt
, i
);
762 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
763 oconstraints
[i
] = constraint
;
764 parse_output_constraint (&constraint
, i
, 0, 0, &allows_mem
,
765 &allows_reg
, &is_inout
);
767 /* This should have been split in gimplify_asm_expr. */
768 gcc_assert (!allows_reg
|| !is_inout
);
770 /* Memory operands are addressable. Note that STMT needs the
771 address of this operand. */
772 if (!allows_reg
&& allows_mem
)
773 mark_address_taken (TREE_VALUE (link
));
775 get_expr_operands (stmt
, &TREE_VALUE (link
), opf_def
| opf_not_non_addressable
);
778 /* Gather all input operands. */
779 for (i
= 0; i
< gimple_asm_ninputs (stmt
); i
++)
781 tree link
= gimple_asm_input_op (stmt
, i
);
782 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
783 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0, oconstraints
,
784 &allows_mem
, &allows_reg
);
786 /* Memory operands are addressable. Note that STMT needs the
787 address of this operand. */
788 if (!allows_reg
&& allows_mem
)
789 mark_address_taken (TREE_VALUE (link
));
791 get_expr_operands (stmt
, &TREE_VALUE (link
), opf_not_non_addressable
);
794 /* Clobber all memory and addressable symbols for asm ("" : : : "memory"); */
795 if (gimple_asm_clobbers_memory_p (stmt
))
796 add_virtual_operand (stmt
, opf_def
);
800 /* Recursively scan the expression pointed to by EXPR_P in statement
801 STMT. FLAGS is one of the OPF_* constants modifying how to
802 interpret the operands found. */
805 get_expr_operands (gimple stmt
, tree
*expr_p
, int flags
)
808 enum tree_code_class codeclass
;
810 int uflags
= opf_use
;
815 if (is_gimple_debug (stmt
))
816 uflags
|= (flags
& opf_no_vops
);
818 code
= TREE_CODE (expr
);
819 codeclass
= TREE_CODE_CLASS (code
);
824 /* Taking the address of a variable does not represent a
825 reference to it, but the fact that the statement takes its
826 address will be of interest to some passes (e.g. alias
828 if ((!(flags
& opf_non_addressable
)
829 || (flags
& opf_not_non_addressable
))
830 && !is_gimple_debug (stmt
))
831 mark_address_taken (TREE_OPERAND (expr
, 0));
833 /* If the address is invariant, there may be no interesting
834 variable references inside. */
835 if (is_gimple_min_invariant (expr
))
838 /* Otherwise, there may be variables referenced inside but there
839 should be no VUSEs created, since the referenced objects are
840 not really accessed. The only operands that we should find
841 here are ARRAY_REF indices which will always be real operands
842 (GIMPLE does not allow non-registers as array indices). */
843 flags
|= opf_no_vops
;
844 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 0),
845 flags
| opf_not_non_addressable
);
849 add_stmt_operand (expr_p
, stmt
, flags
);
855 add_stmt_operand (expr_p
, stmt
, flags
);
858 case DEBUG_EXPR_DECL
:
859 gcc_assert (gimple_debug_bind_p (stmt
));
863 get_indirect_ref_operands (stmt
, expr
, flags
, true);
867 get_tmr_operands (stmt
, expr
, flags
);
871 case ARRAY_RANGE_REF
:
876 if (!(flags
& opf_no_vops
)
877 && TREE_THIS_VOLATILE (expr
))
878 gimple_set_has_volatile_ops (stmt
, true);
880 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 0), flags
);
882 if (code
== COMPONENT_REF
)
884 if (!(flags
& opf_no_vops
)
885 && TREE_THIS_VOLATILE (TREE_OPERAND (expr
, 1)))
886 gimple_set_has_volatile_ops (stmt
, true);
887 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 2), uflags
);
889 else if (code
== ARRAY_REF
|| code
== ARRAY_RANGE_REF
)
891 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 1), uflags
);
892 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 2), uflags
);
893 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 3), uflags
);
900 /* WITH_SIZE_EXPR is a pass-through reference to its first argument,
901 and an rvalue reference to its second argument. */
902 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 1), uflags
);
903 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 0), flags
);
909 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 0), uflags
);
910 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 1), uflags
);
911 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 2), uflags
);
916 /* General aggregate CONSTRUCTORs have been decomposed, but they
917 are still in use as the COMPLEX_EXPR equivalent for vectors. */
919 unsigned HOST_WIDE_INT idx
;
921 /* A volatile constructor is actually TREE_CLOBBER_P, transfer
922 the volatility to the statement, don't use TREE_CLOBBER_P for
923 mirroring the other uses of THIS_VOLATILE in this file. */
924 if (!(flags
& opf_no_vops
)
925 && TREE_THIS_VOLATILE (expr
))
926 gimple_set_has_volatile_ops (stmt
, true);
929 VEC_iterate (constructor_elt
, CONSTRUCTOR_ELTS (expr
), idx
, ce
);
931 get_expr_operands (stmt
, &ce
->value
, uflags
);
937 if (!(flags
& opf_no_vops
)
938 && TREE_THIS_VOLATILE (expr
))
939 gimple_set_has_volatile_ops (stmt
, true);
942 case VIEW_CONVERT_EXPR
:
944 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 0), flags
);
952 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 0), flags
);
953 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 1), flags
);
958 case REALIGN_LOAD_EXPR
:
959 case WIDEN_MULT_PLUS_EXPR
:
960 case WIDEN_MULT_MINUS_EXPR
:
963 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 0), flags
);
964 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 1), flags
);
965 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 2), flags
);
972 case CASE_LABEL_EXPR
:
973 /* Expressions that make no memory references. */
977 if (codeclass
== tcc_unary
)
979 if (codeclass
== tcc_binary
|| codeclass
== tcc_comparison
)
981 if (codeclass
== tcc_constant
|| codeclass
== tcc_type
)
985 /* If we get here, something has gone wrong. */
986 #ifdef ENABLE_CHECKING
987 fprintf (stderr
, "unhandled expression in get_expr_operands():\n");
989 fputs ("\n", stderr
);
995 /* Parse STMT looking for operands. When finished, the various
996 build_* operand vectors will have potential operands in them. */
999 parse_ssa_operands (gimple stmt
)
1001 enum gimple_code code
= gimple_code (stmt
);
1002 size_t i
, n
, start
= 0;
1007 get_asm_expr_operands (stmt
);
1010 case GIMPLE_TRANSACTION
:
1011 /* The start of a transaction is a memory barrier. */
1012 add_virtual_operand (stmt
, opf_def
| opf_use
);
1016 if (gimple_debug_bind_p (stmt
)
1017 && gimple_debug_bind_has_value_p (stmt
))
1018 get_expr_operands (stmt
, gimple_debug_bind_get_value_ptr (stmt
),
1019 opf_use
| opf_no_vops
);
1023 append_vuse (gimple_vop (cfun
));
1027 /* Add call-clobbered operands, if needed. */
1028 maybe_add_call_vops (stmt
);
1032 get_expr_operands (stmt
, gimple_op_ptr (stmt
, 0), opf_def
);
1038 n
= gimple_num_ops (stmt
);
1039 for (i
= start
; i
< n
; i
++)
1040 get_expr_operands (stmt
, gimple_op_ptr (stmt
, i
), opf_use
);
1046 /* Create an operands cache for STMT. */
1049 build_ssa_operands (gimple stmt
)
1051 /* Initially assume that the statement has no volatile operands. */
1052 gimple_set_has_volatile_ops (stmt
, false);
1054 start_ssa_stmt_operands ();
1055 parse_ssa_operands (stmt
);
1056 finalize_ssa_stmt_operands (stmt
);
1059 /* Verifies SSA statement operands. */
1062 verify_ssa_operands (gimple stmt
)
1064 use_operand_p use_p
;
1065 def_operand_p def_p
;
1069 bool volatile_p
= gimple_has_volatile_ops (stmt
);
1071 /* build_ssa_operands w/o finalizing them. */
1072 gimple_set_has_volatile_ops (stmt
, false);
1073 start_ssa_stmt_operands ();
1074 parse_ssa_operands (stmt
);
1076 /* Now verify the built operands are the same as present in STMT. */
1077 def
= gimple_vdef (stmt
);
1079 && TREE_CODE (def
) == SSA_NAME
)
1080 def
= SSA_NAME_VAR (def
);
1081 if (build_vdef
!= def
)
1083 error ("virtual definition of statement not up-to-date");
1086 if (gimple_vdef (stmt
)
1087 && ((def_p
= gimple_vdef_op (stmt
)) == NULL_DEF_OPERAND_P
1088 || DEF_FROM_PTR (def_p
) != gimple_vdef (stmt
)))
1090 error ("virtual def operand missing for stmt");
1094 use
= gimple_vuse (stmt
);
1096 && TREE_CODE (use
) == SSA_NAME
)
1097 use
= SSA_NAME_VAR (use
);
1098 if (build_vuse
!= use
)
1100 error ("virtual use of statement not up-to-date");
1103 if (gimple_vuse (stmt
)
1104 && ((use_p
= gimple_vuse_op (stmt
)) == NULL_USE_OPERAND_P
1105 || USE_FROM_PTR (use_p
) != gimple_vuse (stmt
)))
1107 error ("virtual use operand missing for stmt");
1111 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
1113 FOR_EACH_VEC_ELT (tree
, build_uses
, i
, use
)
1115 if (use_p
->use
== (tree
*)use
)
1117 VEC_replace (tree
, build_uses
, i
, NULL_TREE
);
1121 if (i
== VEC_length (tree
, build_uses
))
1123 error ("excess use operand for stmt");
1124 debug_generic_expr (USE_FROM_PTR (use_p
));
1128 FOR_EACH_VEC_ELT (tree
, build_uses
, i
, use
)
1129 if (use
!= NULL_TREE
)
1131 error ("use operand missing for stmt");
1132 debug_generic_expr (*(tree
*)use
);
1136 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, iter
, SSA_OP_DEF
)
1138 FOR_EACH_VEC_ELT (tree
, build_defs
, i
, def
)
1140 if (def_p
== (tree
*)def
)
1142 VEC_replace (tree
, build_defs
, i
, NULL_TREE
);
1146 if (i
== VEC_length (tree
, build_defs
))
1148 error ("excess def operand for stmt");
1149 debug_generic_expr (DEF_FROM_PTR (def_p
));
1153 FOR_EACH_VEC_ELT (tree
, build_defs
, i
, def
)
1154 if (def
!= NULL_TREE
)
1156 error ("def operand missing for stmt");
1157 debug_generic_expr (*(tree
*)def
);
1161 if (gimple_has_volatile_ops (stmt
) != volatile_p
)
1163 error ("stmt volatile flag not up-to-date");
1167 cleanup_build_arrays ();
1172 /* Releases the operands of STMT back to their freelists, and clears
1173 the stmt operand lists. */
1176 free_stmt_operands (gimple stmt
)
1178 def_optype_p defs
= gimple_def_ops (stmt
), last_def
;
1179 use_optype_p uses
= gimple_use_ops (stmt
), last_use
;
1183 for (last_def
= defs
; last_def
->next
; last_def
= last_def
->next
)
1185 last_def
->next
= gimple_ssa_operands (cfun
)->free_defs
;
1186 gimple_ssa_operands (cfun
)->free_defs
= defs
;
1187 gimple_set_def_ops (stmt
, NULL
);
1192 for (last_use
= uses
; last_use
->next
; last_use
= last_use
->next
)
1193 delink_imm_use (USE_OP_PTR (last_use
));
1194 delink_imm_use (USE_OP_PTR (last_use
));
1195 last_use
->next
= gimple_ssa_operands (cfun
)->free_uses
;
1196 gimple_ssa_operands (cfun
)->free_uses
= uses
;
1197 gimple_set_use_ops (stmt
, NULL
);
1200 if (gimple_has_mem_ops (stmt
))
1202 gimple_set_vuse (stmt
, NULL_TREE
);
1203 gimple_set_vdef (stmt
, NULL_TREE
);
1208 /* Get the operands of statement STMT. */
1211 update_stmt_operands (gimple stmt
)
1213 /* If update_stmt_operands is called before SSA is initialized, do
1215 if (!ssa_operands_active ())
1218 timevar_push (TV_TREE_OPS
);
1220 /* If the stmt is a noreturn call queue it to be processed by
1221 split_bbs_on_noreturn_calls during cfg cleanup. */
1222 if (is_gimple_call (stmt
)
1223 && gimple_call_noreturn_p (stmt
))
1224 VEC_safe_push (gimple
, gc
, MODIFIED_NORETURN_CALLS (cfun
), stmt
);
1226 gcc_assert (gimple_modified_p (stmt
));
1227 build_ssa_operands (stmt
);
1228 gimple_set_modified (stmt
, false);
1230 timevar_pop (TV_TREE_OPS
);
1234 /* Swap operands EXP0 and EXP1 in statement STMT. No attempt is done
1235 to test the validity of the swap operation. */
1238 swap_tree_operands (gimple stmt
, tree
*exp0
, tree
*exp1
)
1244 /* If the operand cache is active, attempt to preserve the relative
1245 positions of these two operands in their respective immediate use
1246 lists by adjusting their use pointer to point to the new
1247 operand position. */
1248 if (ssa_operands_active () && op0
!= op1
)
1250 use_optype_p use0
, use1
, ptr
;
1253 /* Find the 2 operands in the cache, if they are there. */
1254 for (ptr
= gimple_use_ops (stmt
); ptr
; ptr
= ptr
->next
)
1255 if (USE_OP_PTR (ptr
)->use
== exp0
)
1261 for (ptr
= gimple_use_ops (stmt
); ptr
; ptr
= ptr
->next
)
1262 if (USE_OP_PTR (ptr
)->use
== exp1
)
1268 /* And adjust their location to point to the new position of the
1271 USE_OP_PTR (use0
)->use
= exp1
;
1273 USE_OP_PTR (use1
)->use
= exp0
;
1276 /* Now swap the data. */
1282 /* Scan the immediate_use list for VAR making sure its linked properly.
1283 Return TRUE if there is a problem and emit an error message to F. */
1286 verify_imm_links (FILE *f
, tree var
)
1288 use_operand_p ptr
, prev
, list
;
1291 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
1293 list
= &(SSA_NAME_IMM_USE_NODE (var
));
1294 gcc_assert (list
->use
== NULL
);
1296 if (list
->prev
== NULL
)
1298 gcc_assert (list
->next
== NULL
);
1304 for (ptr
= list
->next
; ptr
!= list
; )
1306 if (prev
!= ptr
->prev
)
1309 if (ptr
->use
== NULL
)
1310 goto error
; /* 2 roots, or SAFE guard node. */
1311 else if (*(ptr
->use
) != var
)
1317 /* Avoid infinite loops. 50,000,000 uses probably indicates a
1319 if (count
++ > 50000000)
1323 /* Verify list in the other direction. */
1325 for (ptr
= list
->prev
; ptr
!= list
; )
1327 if (prev
!= ptr
->next
)
1341 if (ptr
->loc
.stmt
&& gimple_modified_p (ptr
->loc
.stmt
))
1343 fprintf (f
, " STMT MODIFIED. - <%p> ", (void *)ptr
->loc
.stmt
);
1344 print_gimple_stmt (f
, ptr
->loc
.stmt
, 0, TDF_SLIM
);
1346 fprintf (f
, " IMM ERROR : (use_p : tree - %p:%p)", (void *)ptr
,
1348 print_generic_expr (f
, USE_FROM_PTR (ptr
), TDF_SLIM
);
1354 /* Dump all the immediate uses to FILE. */
1357 dump_immediate_uses_for (FILE *file
, tree var
)
1359 imm_use_iterator iter
;
1360 use_operand_p use_p
;
1362 gcc_assert (var
&& TREE_CODE (var
) == SSA_NAME
);
1364 print_generic_expr (file
, var
, TDF_SLIM
);
1365 fprintf (file
, " : -->");
1366 if (has_zero_uses (var
))
1367 fprintf (file
, " no uses.\n");
1369 if (has_single_use (var
))
1370 fprintf (file
, " single use.\n");
1372 fprintf (file
, "%d uses.\n", num_imm_uses (var
));
1374 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
1376 if (use_p
->loc
.stmt
== NULL
&& use_p
->use
== NULL
)
1377 fprintf (file
, "***end of stmt iterator marker***\n");
1379 if (!is_gimple_reg (USE_FROM_PTR (use_p
)))
1380 print_gimple_stmt (file
, USE_STMT (use_p
), 0, TDF_VOPS
|TDF_MEMSYMS
);
1382 print_gimple_stmt (file
, USE_STMT (use_p
), 0, TDF_SLIM
);
1384 fprintf(file
, "\n");
1388 /* Dump all the immediate uses to FILE. */
1391 dump_immediate_uses (FILE *file
)
1396 fprintf (file
, "Immediate_uses: \n\n");
1397 for (x
= 1; x
< num_ssa_names
; x
++)
1402 dump_immediate_uses_for (file
, var
);
1407 /* Dump def-use edges on stderr. */
1410 debug_immediate_uses (void)
1412 dump_immediate_uses (stderr
);
1416 /* Dump def-use edges on stderr. */
1419 debug_immediate_uses_for (tree var
)
1421 dump_immediate_uses_for (stderr
, var
);
1425 /* Unlink STMTs virtual definition from the IL by propagating its use. */
1428 unlink_stmt_vdef (gimple stmt
)
1430 use_operand_p use_p
;
1431 imm_use_iterator iter
;
1433 tree vdef
= gimple_vdef (stmt
);
1434 tree vuse
= gimple_vuse (stmt
);
1437 || TREE_CODE (vdef
) != SSA_NAME
)
1440 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, vdef
)
1442 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
1443 SET_USE (use_p
, vuse
);
1446 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vdef
))
1447 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vuse
) = 1;