* decl.c (cp_finish_decl): Check for invalid multiple initializers
[official-gcc.git] / gcc / tree-ssa-operands.c
blobd9b3146cf4f0d0717e644dc9708c9e91d0c3a68a
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)
10 any later version.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "function.h"
28 #include "gimple-pretty-print.h"
29 #include "tree-flow.h"
30 #include "tree-inline.h"
31 #include "timevar.h"
32 #include "dumpfile.h"
33 #include "ggc.h"
34 #include "timevar.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
52 from a stmt tree.
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. */
83 #define opf_use 0
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
94 VUSE for 'b'. */
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;
130 /* Return true if the SSA operands cache is active. */
132 bool
133 ssa_operands_active (struct function *fun)
135 if (fun == NULL)
136 return false;
138 return fun->gimple_df && gimple_ssa_operands (fun)->ops_active;
142 /* Create the VOP variable, an artificial global variable to act as a
143 representative of all of the virtual operands FUD chain. */
145 static void
146 create_vop_var (struct function *fn)
148 tree global_var;
150 gcc_assert (fn->gimple_df->vop == NULL_TREE);
152 global_var = build_decl (BUILTINS_LOCATION, VAR_DECL,
153 get_identifier (".MEM"),
154 void_type_node);
155 DECL_ARTIFICIAL (global_var) = 1;
156 TREE_READONLY (global_var) = 0;
157 DECL_EXTERNAL (global_var) = 1;
158 TREE_STATIC (global_var) = 1;
159 TREE_USED (global_var) = 1;
160 DECL_CONTEXT (global_var) = NULL_TREE;
161 TREE_THIS_VOLATILE (global_var) = 0;
162 TREE_ADDRESSABLE (global_var) = 0;
163 VAR_DECL_IS_VIRTUAL_OPERAND (global_var) = 1;
165 fn->gimple_df->vop = global_var;
168 /* These are the sizes of the operand memory buffer in bytes which gets
169 allocated each time more operands space is required. The final value is
170 the amount that is allocated every time after that.
171 In 1k we can fit 25 use operands (or 63 def operands) on a host with
172 8 byte pointers, that would be 10 statements each with 1 def and 2
173 uses. */
175 #define OP_SIZE_INIT 0
176 #define OP_SIZE_1 (1024 - sizeof (void *))
177 #define OP_SIZE_2 (1024 * 4 - sizeof (void *))
178 #define OP_SIZE_3 (1024 * 16 - sizeof (void *))
180 /* Initialize the operand cache routines. */
182 void
183 init_ssa_operands (struct function *fn)
185 if (!n_initialized++)
187 build_defs = VEC_alloc (tree, heap, 5);
188 build_uses = VEC_alloc (tree, heap, 10);
189 build_vuse = NULL_TREE;
190 build_vdef = NULL_TREE;
191 bitmap_obstack_initialize (&operands_bitmap_obstack);
194 gcc_assert (gimple_ssa_operands (fn)->operand_memory == NULL);
195 gimple_ssa_operands (fn)->operand_memory_index
196 = gimple_ssa_operands (fn)->ssa_operand_mem_size;
197 gimple_ssa_operands (fn)->ops_active = true;
198 gimple_ssa_operands (fn)->ssa_operand_mem_size = OP_SIZE_INIT;
199 create_vop_var (fn);
203 /* Dispose of anything required by the operand routines. */
205 void
206 fini_ssa_operands (void)
208 struct ssa_operand_memory_d *ptr;
210 if (!--n_initialized)
212 VEC_free (tree, heap, build_defs);
213 VEC_free (tree, heap, build_uses);
214 build_vdef = NULL_TREE;
215 build_vuse = NULL_TREE;
218 gimple_ssa_operands (cfun)->free_defs = NULL;
219 gimple_ssa_operands (cfun)->free_uses = NULL;
221 while ((ptr = gimple_ssa_operands (cfun)->operand_memory) != NULL)
223 gimple_ssa_operands (cfun)->operand_memory
224 = gimple_ssa_operands (cfun)->operand_memory->next;
225 ggc_free (ptr);
228 gimple_ssa_operands (cfun)->ops_active = false;
230 if (!n_initialized)
231 bitmap_obstack_release (&operands_bitmap_obstack);
233 cfun->gimple_df->vop = NULL_TREE;
237 /* Return memory for an operand of size SIZE. */
239 static inline void *
240 ssa_operand_alloc (unsigned size)
242 char *ptr;
244 gcc_assert (size == sizeof (struct use_optype_d)
245 || size == sizeof (struct def_optype_d));
247 if (gimple_ssa_operands (cfun)->operand_memory_index + size
248 >= gimple_ssa_operands (cfun)->ssa_operand_mem_size)
250 struct ssa_operand_memory_d *ptr;
252 switch (gimple_ssa_operands (cfun)->ssa_operand_mem_size)
254 case OP_SIZE_INIT:
255 gimple_ssa_operands (cfun)->ssa_operand_mem_size = OP_SIZE_1;
256 break;
257 case OP_SIZE_1:
258 gimple_ssa_operands (cfun)->ssa_operand_mem_size = OP_SIZE_2;
259 break;
260 case OP_SIZE_2:
261 case OP_SIZE_3:
262 gimple_ssa_operands (cfun)->ssa_operand_mem_size = OP_SIZE_3;
263 break;
264 default:
265 gcc_unreachable ();
269 ptr = ggc_alloc_ssa_operand_memory_d (sizeof (void *)
270 + gimple_ssa_operands (cfun)->ssa_operand_mem_size);
272 ptr->next = gimple_ssa_operands (cfun)->operand_memory;
273 gimple_ssa_operands (cfun)->operand_memory = ptr;
274 gimple_ssa_operands (cfun)->operand_memory_index = 0;
277 ptr = &(gimple_ssa_operands (cfun)->operand_memory
278 ->mem[gimple_ssa_operands (cfun)->operand_memory_index]);
279 gimple_ssa_operands (cfun)->operand_memory_index += size;
280 return ptr;
284 /* Allocate a DEF operand. */
286 static inline struct def_optype_d *
287 alloc_def (void)
289 struct def_optype_d *ret;
290 if (gimple_ssa_operands (cfun)->free_defs)
292 ret = gimple_ssa_operands (cfun)->free_defs;
293 gimple_ssa_operands (cfun)->free_defs
294 = gimple_ssa_operands (cfun)->free_defs->next;
296 else
297 ret = (struct def_optype_d *)
298 ssa_operand_alloc (sizeof (struct def_optype_d));
299 return ret;
303 /* Allocate a USE operand. */
305 static inline struct use_optype_d *
306 alloc_use (void)
308 struct use_optype_d *ret;
309 if (gimple_ssa_operands (cfun)->free_uses)
311 ret = gimple_ssa_operands (cfun)->free_uses;
312 gimple_ssa_operands (cfun)->free_uses
313 = gimple_ssa_operands (cfun)->free_uses->next;
315 else
316 ret = (struct use_optype_d *)
317 ssa_operand_alloc (sizeof (struct use_optype_d));
318 return ret;
322 /* Adds OP to the list of defs after LAST. */
324 static inline def_optype_p
325 add_def_op (tree *op, def_optype_p last)
327 def_optype_p new_def;
329 new_def = alloc_def ();
330 DEF_OP_PTR (new_def) = op;
331 last->next = new_def;
332 new_def->next = NULL;
333 return new_def;
337 /* Adds OP to the list of uses of statement STMT after LAST. */
339 static inline use_optype_p
340 add_use_op (gimple stmt, tree *op, use_optype_p last)
342 use_optype_p new_use;
344 new_use = alloc_use ();
345 USE_OP_PTR (new_use)->use = op;
346 link_imm_use_stmt (USE_OP_PTR (new_use), *op, stmt);
347 last->next = new_use;
348 new_use->next = NULL;
349 return new_use;
354 /* Takes elements from build_defs and turns them into def operands of STMT.
355 TODO -- Make build_defs VEC of tree *. */
357 static inline void
358 finalize_ssa_defs (gimple stmt)
360 unsigned new_i;
361 struct def_optype_d new_list;
362 def_optype_p old_ops, last;
363 unsigned int num = VEC_length (tree, build_defs);
365 /* There should only be a single real definition per assignment. */
366 gcc_assert ((stmt && gimple_code (stmt) != GIMPLE_ASSIGN) || num <= 1);
368 /* Pre-pend the vdef we may have built. */
369 if (build_vdef != NULL_TREE)
371 tree oldvdef = gimple_vdef (stmt);
372 if (oldvdef
373 && TREE_CODE (oldvdef) == SSA_NAME)
374 oldvdef = SSA_NAME_VAR (oldvdef);
375 if (oldvdef != build_vdef)
376 gimple_set_vdef (stmt, build_vdef);
377 VEC_safe_insert (tree, heap, build_defs, 0, (tree)gimple_vdef_ptr (stmt));
378 ++num;
381 new_list.next = NULL;
382 last = &new_list;
384 old_ops = gimple_def_ops (stmt);
386 new_i = 0;
388 /* Clear and unlink a no longer necessary VDEF. */
389 if (build_vdef == NULL_TREE
390 && gimple_vdef (stmt) != NULL_TREE)
392 if (TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
394 unlink_stmt_vdef (stmt);
395 release_ssa_name (gimple_vdef (stmt));
397 gimple_set_vdef (stmt, NULL_TREE);
400 /* If we have a non-SSA_NAME VDEF, mark it for renaming. */
401 if (gimple_vdef (stmt)
402 && TREE_CODE (gimple_vdef (stmt)) != SSA_NAME)
404 cfun->gimple_df->rename_vops = 1;
405 cfun->gimple_df->ssa_renaming_needed = 1;
408 /* Check for the common case of 1 def that hasn't changed. */
409 if (old_ops && old_ops->next == NULL && num == 1
410 && (tree *) VEC_index (tree, build_defs, 0) == DEF_OP_PTR (old_ops))
411 return;
413 /* If there is anything in the old list, free it. */
414 if (old_ops)
416 old_ops->next = gimple_ssa_operands (cfun)->free_defs;
417 gimple_ssa_operands (cfun)->free_defs = old_ops;
420 /* If there is anything remaining in the build_defs list, simply emit it. */
421 for ( ; new_i < num; new_i++)
423 tree *op = (tree *) VEC_index (tree, build_defs, new_i);
424 if (DECL_P (*op))
425 cfun->gimple_df->ssa_renaming_needed = 1;
426 last = add_def_op (op, last);
429 /* Now set the stmt's operands. */
430 gimple_set_def_ops (stmt, new_list.next);
434 /* Takes elements from build_uses and turns them into use operands of STMT.
435 TODO -- Make build_uses VEC of tree *. */
437 static inline void
438 finalize_ssa_uses (gimple stmt)
440 unsigned new_i;
441 struct use_optype_d new_list;
442 use_optype_p old_ops, ptr, last;
444 /* Pre-pend the VUSE we may have built. */
445 if (build_vuse != NULL_TREE)
447 tree oldvuse = gimple_vuse (stmt);
448 if (oldvuse
449 && TREE_CODE (oldvuse) == SSA_NAME)
450 oldvuse = SSA_NAME_VAR (oldvuse);
451 if (oldvuse != (build_vuse != NULL_TREE
452 ? build_vuse : build_vdef))
453 gimple_set_vuse (stmt, NULL_TREE);
454 VEC_safe_insert (tree, heap, build_uses, 0, (tree)gimple_vuse_ptr (stmt));
457 new_list.next = NULL;
458 last = &new_list;
460 old_ops = gimple_use_ops (stmt);
462 /* Clear a no longer necessary VUSE. */
463 if (build_vuse == NULL_TREE
464 && gimple_vuse (stmt) != NULL_TREE)
465 gimple_set_vuse (stmt, NULL_TREE);
467 /* If there is anything in the old list, free it. */
468 if (old_ops)
470 for (ptr = old_ops; ptr; ptr = ptr->next)
471 delink_imm_use (USE_OP_PTR (ptr));
472 old_ops->next = gimple_ssa_operands (cfun)->free_uses;
473 gimple_ssa_operands (cfun)->free_uses = old_ops;
476 /* If we added a VUSE, make sure to set the operand if it is not already
477 present and mark it for renaming. */
478 if (build_vuse != NULL_TREE
479 && gimple_vuse (stmt) == NULL_TREE)
481 gimple_set_vuse (stmt, gimple_vop (cfun));
482 cfun->gimple_df->rename_vops = 1;
483 cfun->gimple_df->ssa_renaming_needed = 1;
486 /* Now create nodes for all the new nodes. */
487 for (new_i = 0; new_i < VEC_length (tree, build_uses); new_i++)
489 tree *op = (tree *) VEC_index (tree, build_uses, new_i);
490 if (DECL_P (*op))
491 cfun->gimple_df->ssa_renaming_needed = 1;
492 last = add_use_op (stmt, op, last);
495 /* Now set the stmt's operands. */
496 gimple_set_use_ops (stmt, new_list.next);
500 /* Clear the in_list bits and empty the build array for VDEFs and
501 VUSEs. */
503 static inline void
504 cleanup_build_arrays (void)
506 build_vdef = NULL_TREE;
507 build_vuse = NULL_TREE;
508 VEC_truncate (tree, build_defs, 0);
509 VEC_truncate (tree, build_uses, 0);
513 /* Finalize all the build vectors, fill the new ones into INFO. */
515 static inline void
516 finalize_ssa_stmt_operands (gimple stmt)
518 finalize_ssa_defs (stmt);
519 finalize_ssa_uses (stmt);
520 cleanup_build_arrays ();
524 /* Start the process of building up operands vectors in INFO. */
526 static inline void
527 start_ssa_stmt_operands (void)
529 gcc_assert (VEC_length (tree, build_defs) == 0);
530 gcc_assert (VEC_length (tree, build_uses) == 0);
531 gcc_assert (build_vuse == NULL_TREE);
532 gcc_assert (build_vdef == NULL_TREE);
536 /* Add DEF_P to the list of pointers to operands. */
538 static inline void
539 append_def (tree *def_p)
541 VEC_safe_push (tree, heap, build_defs, (tree) def_p);
545 /* Add USE_P to the list of pointers to operands. */
547 static inline void
548 append_use (tree *use_p)
550 VEC_safe_push (tree, heap, build_uses, (tree) use_p);
554 /* Add VAR to the set of variables that require a VDEF operator. */
556 static inline void
557 append_vdef (tree var)
559 if (!optimize)
560 return;
562 gcc_assert ((build_vdef == NULL_TREE
563 || build_vdef == var)
564 && (build_vuse == NULL_TREE
565 || build_vuse == var));
567 build_vdef = var;
568 build_vuse = var;
572 /* Add VAR to the set of variables that require a VUSE operator. */
574 static inline void
575 append_vuse (tree var)
577 if (!optimize)
578 return;
580 gcc_assert (build_vuse == NULL_TREE
581 || build_vuse == var);
583 build_vuse = var;
586 /* Add virtual operands for STMT. FLAGS is as in get_expr_operands. */
588 static void
589 add_virtual_operand (gimple stmt ATTRIBUTE_UNUSED, int flags)
591 /* Add virtual operands to the stmt, unless the caller has specifically
592 requested not to do that (used when adding operands inside an
593 ADDR_EXPR expression). */
594 if (flags & opf_no_vops)
595 return;
597 gcc_assert (!is_gimple_debug (stmt));
599 if (flags & opf_def)
600 append_vdef (gimple_vop (cfun));
601 else
602 append_vuse (gimple_vop (cfun));
606 /* Add *VAR_P to the appropriate operand array for statement STMT.
607 FLAGS is as in get_expr_operands. If *VAR_P is a GIMPLE register,
608 it will be added to the statement's real operands, otherwise it is
609 added to virtual operands. */
611 static void
612 add_stmt_operand (tree *var_p, gimple stmt, int flags)
614 tree var = *var_p;
616 gcc_assert (SSA_VAR_P (*var_p));
618 if (is_gimple_reg (var))
620 /* The variable is a GIMPLE register. Add it to real operands. */
621 if (flags & opf_def)
622 append_def (var_p);
623 else
624 append_use (var_p);
626 else
628 /* Mark statements with volatile operands. */
629 if (!(flags & opf_no_vops)
630 && TREE_THIS_VOLATILE (var))
631 gimple_set_has_volatile_ops (stmt, true);
633 /* The variable is a memory access. Add virtual operands. */
634 add_virtual_operand (stmt, flags);
638 /* Mark the base address of REF as having its address taken.
639 REF may be a single variable whose address has been taken or any
640 other valid GIMPLE memory reference (structure reference, array,
641 etc). */
643 static void
644 mark_address_taken (tree ref)
646 tree var;
648 /* Note that it is *NOT OKAY* to use the target of a COMPONENT_REF
649 as the only thing we take the address of. If VAR is a structure,
650 taking the address of a field means that the whole structure may
651 be referenced using pointer arithmetic. See PR 21407 and the
652 ensuing mailing list discussion. */
653 var = get_base_address (ref);
654 if (var)
656 if (DECL_P (var))
657 TREE_ADDRESSABLE (var) = 1;
658 else if (TREE_CODE (var) == MEM_REF
659 && TREE_CODE (TREE_OPERAND (var, 0)) == ADDR_EXPR
660 && DECL_P (TREE_OPERAND (TREE_OPERAND (var, 0), 0)))
661 TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (var, 0), 0)) = 1;
666 /* A subroutine of get_expr_operands to handle MEM_REF.
668 STMT is the statement being processed, EXPR is the MEM_REF
669 that got us here.
671 FLAGS is as in get_expr_operands.
673 RECURSE_ON_BASE should be set to true if we want to continue
674 calling get_expr_operands on the base pointer, and false if
675 something else will do it for us. */
677 static void
678 get_indirect_ref_operands (gimple stmt, tree expr, int flags,
679 bool recurse_on_base)
681 tree *pptr = &TREE_OPERAND (expr, 0);
683 if (!(flags & opf_no_vops)
684 && TREE_THIS_VOLATILE (expr))
685 gimple_set_has_volatile_ops (stmt, true);
687 /* Add the VOP. */
688 add_virtual_operand (stmt, flags);
690 /* If requested, add a USE operand for the base pointer. */
691 if (recurse_on_base)
692 get_expr_operands (stmt, pptr,
693 opf_non_addressable | opf_use
694 | (flags & (opf_no_vops|opf_not_non_addressable)));
698 /* A subroutine of get_expr_operands to handle TARGET_MEM_REF. */
700 static void
701 get_tmr_operands (gimple stmt, tree expr, int flags)
703 if (!(flags & opf_no_vops)
704 && TREE_THIS_VOLATILE (expr))
705 gimple_set_has_volatile_ops (stmt, true);
707 /* First record the real operands. */
708 get_expr_operands (stmt, &TMR_BASE (expr), opf_use | (flags & opf_no_vops));
709 get_expr_operands (stmt, &TMR_INDEX (expr), opf_use | (flags & opf_no_vops));
710 get_expr_operands (stmt, &TMR_INDEX2 (expr), opf_use | (flags & opf_no_vops));
712 add_virtual_operand (stmt, flags);
716 /* If STMT is a call that may clobber globals and other symbols that
717 escape, add them to the VDEF/VUSE lists for it. */
719 static void
720 maybe_add_call_vops (gimple stmt)
722 int call_flags = gimple_call_flags (stmt);
724 /* If aliases have been computed already, add VDEF or VUSE
725 operands for all the symbols that have been found to be
726 call-clobbered. */
727 if (!(call_flags & ECF_NOVOPS))
729 /* A 'pure' or a 'const' function never call-clobbers anything.
730 A 'noreturn' function might, but since we don't return anyway
731 there is no point in recording that. */
732 if (!(call_flags & (ECF_PURE | ECF_CONST | ECF_NORETURN)))
733 add_virtual_operand (stmt, opf_def);
734 else if (!(call_flags & ECF_CONST))
735 add_virtual_operand (stmt, opf_use);
740 /* Scan operands in the ASM_EXPR stmt referred to in INFO. */
742 static void
743 get_asm_expr_operands (gimple stmt)
745 size_t i, noutputs;
746 const char **oconstraints;
747 const char *constraint;
748 bool allows_mem, allows_reg, is_inout;
750 noutputs = gimple_asm_noutputs (stmt);
751 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
753 /* Gather all output operands. */
754 for (i = 0; i < gimple_asm_noutputs (stmt); i++)
756 tree link = gimple_asm_output_op (stmt, i);
757 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
758 oconstraints[i] = constraint;
759 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
760 &allows_reg, &is_inout);
762 /* This should have been split in gimplify_asm_expr. */
763 gcc_assert (!allows_reg || !is_inout);
765 /* Memory operands are addressable. Note that STMT needs the
766 address of this operand. */
767 if (!allows_reg && allows_mem)
768 mark_address_taken (TREE_VALUE (link));
770 get_expr_operands (stmt, &TREE_VALUE (link), opf_def | opf_not_non_addressable);
773 /* Gather all input operands. */
774 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
776 tree link = gimple_asm_input_op (stmt, i);
777 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
778 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
779 &allows_mem, &allows_reg);
781 /* Memory operands are addressable. Note that STMT needs the
782 address of this operand. */
783 if (!allows_reg && allows_mem)
784 mark_address_taken (TREE_VALUE (link));
786 get_expr_operands (stmt, &TREE_VALUE (link), opf_not_non_addressable);
789 /* Clobber all memory and addressable symbols for asm ("" : : : "memory"); */
790 if (gimple_asm_clobbers_memory_p (stmt))
791 add_virtual_operand (stmt, opf_def);
795 /* Recursively scan the expression pointed to by EXPR_P in statement
796 STMT. FLAGS is one of the OPF_* constants modifying how to
797 interpret the operands found. */
799 static void
800 get_expr_operands (gimple stmt, tree *expr_p, int flags)
802 enum tree_code code;
803 enum tree_code_class codeclass;
804 tree expr = *expr_p;
805 int uflags = opf_use;
807 if (expr == NULL)
808 return;
810 if (is_gimple_debug (stmt))
811 uflags |= (flags & opf_no_vops);
813 code = TREE_CODE (expr);
814 codeclass = TREE_CODE_CLASS (code);
816 switch (code)
818 case ADDR_EXPR:
819 /* Taking the address of a variable does not represent a
820 reference to it, but the fact that the statement takes its
821 address will be of interest to some passes (e.g. alias
822 resolution). */
823 if ((!(flags & opf_non_addressable)
824 || (flags & opf_not_non_addressable))
825 && !is_gimple_debug (stmt))
826 mark_address_taken (TREE_OPERAND (expr, 0));
828 /* If the address is invariant, there may be no interesting
829 variable references inside. */
830 if (is_gimple_min_invariant (expr))
831 return;
833 /* Otherwise, there may be variables referenced inside but there
834 should be no VUSEs created, since the referenced objects are
835 not really accessed. The only operands that we should find
836 here are ARRAY_REF indices which will always be real operands
837 (GIMPLE does not allow non-registers as array indices). */
838 flags |= opf_no_vops;
839 get_expr_operands (stmt, &TREE_OPERAND (expr, 0),
840 flags | opf_not_non_addressable);
841 return;
843 case SSA_NAME:
844 add_stmt_operand (expr_p, stmt, flags);
845 return;
847 case VAR_DECL:
848 case PARM_DECL:
849 case RESULT_DECL:
850 add_stmt_operand (expr_p, stmt, flags);
851 return;
853 case DEBUG_EXPR_DECL:
854 gcc_assert (gimple_debug_bind_p (stmt));
855 return;
857 case MEM_REF:
858 get_indirect_ref_operands (stmt, expr, flags, true);
859 return;
861 case TARGET_MEM_REF:
862 get_tmr_operands (stmt, expr, flags);
863 return;
865 case ARRAY_REF:
866 case ARRAY_RANGE_REF:
867 case COMPONENT_REF:
868 case REALPART_EXPR:
869 case IMAGPART_EXPR:
871 if (!(flags & opf_no_vops)
872 && TREE_THIS_VOLATILE (expr))
873 gimple_set_has_volatile_ops (stmt, true);
875 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
877 if (code == COMPONENT_REF)
879 if (!(flags & opf_no_vops)
880 && TREE_THIS_VOLATILE (TREE_OPERAND (expr, 1)))
881 gimple_set_has_volatile_ops (stmt, true);
882 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), uflags);
884 else if (code == ARRAY_REF || code == ARRAY_RANGE_REF)
886 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), uflags);
887 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), uflags);
888 get_expr_operands (stmt, &TREE_OPERAND (expr, 3), uflags);
891 return;
894 case WITH_SIZE_EXPR:
895 /* WITH_SIZE_EXPR is a pass-through reference to its first argument,
896 and an rvalue reference to its second argument. */
897 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), uflags);
898 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
899 return;
901 case COND_EXPR:
902 case VEC_COND_EXPR:
903 case VEC_PERM_EXPR:
904 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), uflags);
905 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), uflags);
906 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), uflags);
907 return;
909 case CONSTRUCTOR:
911 /* General aggregate CONSTRUCTORs have been decomposed, but they
912 are still in use as the COMPLEX_EXPR equivalent for vectors. */
913 constructor_elt *ce;
914 unsigned HOST_WIDE_INT idx;
916 /* A volatile constructor is actually TREE_CLOBBER_P, transfer
917 the volatility to the statement, don't use TREE_CLOBBER_P for
918 mirroring the other uses of THIS_VOLATILE in this file. */
919 if (!(flags & opf_no_vops)
920 && TREE_THIS_VOLATILE (expr))
921 gimple_set_has_volatile_ops (stmt, true);
923 for (idx = 0;
924 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (expr), idx, ce);
925 idx++)
926 get_expr_operands (stmt, &ce->value, uflags);
928 return;
931 case BIT_FIELD_REF:
932 if (!(flags & opf_no_vops)
933 && TREE_THIS_VOLATILE (expr))
934 gimple_set_has_volatile_ops (stmt, true);
935 /* FALLTHRU */
937 case VIEW_CONVERT_EXPR:
938 do_unary:
939 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
940 return;
942 case COMPOUND_EXPR:
943 case OBJ_TYPE_REF:
944 case ASSERT_EXPR:
945 do_binary:
947 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
948 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
949 return;
952 case DOT_PROD_EXPR:
953 case REALIGN_LOAD_EXPR:
954 case WIDEN_MULT_PLUS_EXPR:
955 case WIDEN_MULT_MINUS_EXPR:
956 case FMA_EXPR:
958 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
959 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
960 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), flags);
961 return;
964 case FUNCTION_DECL:
965 case LABEL_DECL:
966 case CONST_DECL:
967 case CASE_LABEL_EXPR:
968 /* Expressions that make no memory references. */
969 return;
971 default:
972 if (codeclass == tcc_unary)
973 goto do_unary;
974 if (codeclass == tcc_binary || codeclass == tcc_comparison)
975 goto do_binary;
976 if (codeclass == tcc_constant || codeclass == tcc_type)
977 return;
980 /* If we get here, something has gone wrong. */
981 #ifdef ENABLE_CHECKING
982 fprintf (stderr, "unhandled expression in get_expr_operands():\n");
983 debug_tree (expr);
984 fputs ("\n", stderr);
985 #endif
986 gcc_unreachable ();
990 /* Parse STMT looking for operands. When finished, the various
991 build_* operand vectors will have potential operands in them. */
993 static void
994 parse_ssa_operands (gimple stmt)
996 enum gimple_code code = gimple_code (stmt);
997 size_t i, n, start = 0;
999 switch (code)
1001 case GIMPLE_ASM:
1002 get_asm_expr_operands (stmt);
1003 break;
1005 case GIMPLE_TRANSACTION:
1006 /* The start of a transaction is a memory barrier. */
1007 add_virtual_operand (stmt, opf_def | opf_use);
1008 break;
1010 case GIMPLE_DEBUG:
1011 if (gimple_debug_bind_p (stmt)
1012 && gimple_debug_bind_has_value_p (stmt))
1013 get_expr_operands (stmt, gimple_debug_bind_get_value_ptr (stmt),
1014 opf_use | opf_no_vops);
1015 break;
1017 case GIMPLE_RETURN:
1018 append_vuse (gimple_vop (cfun));
1019 goto do_default;
1021 case GIMPLE_CALL:
1022 /* Add call-clobbered operands, if needed. */
1023 maybe_add_call_vops (stmt);
1024 /* FALLTHRU */
1026 case GIMPLE_ASSIGN:
1027 get_expr_operands (stmt, gimple_op_ptr (stmt, 0), opf_def);
1028 start = 1;
1029 /* FALLTHRU */
1031 default:
1032 do_default:
1033 n = gimple_num_ops (stmt);
1034 for (i = start; i < n; i++)
1035 get_expr_operands (stmt, gimple_op_ptr (stmt, i), opf_use);
1036 break;
1041 /* Create an operands cache for STMT. */
1043 static void
1044 build_ssa_operands (gimple stmt)
1046 /* Initially assume that the statement has no volatile operands. */
1047 gimple_set_has_volatile_ops (stmt, false);
1049 start_ssa_stmt_operands ();
1050 parse_ssa_operands (stmt);
1051 finalize_ssa_stmt_operands (stmt);
1054 /* Verifies SSA statement operands. */
1056 DEBUG_FUNCTION bool
1057 verify_ssa_operands (gimple stmt)
1059 use_operand_p use_p;
1060 def_operand_p def_p;
1061 ssa_op_iter iter;
1062 unsigned i;
1063 tree use, def;
1064 bool volatile_p = gimple_has_volatile_ops (stmt);
1066 /* build_ssa_operands w/o finalizing them. */
1067 gimple_set_has_volatile_ops (stmt, false);
1068 start_ssa_stmt_operands ();
1069 parse_ssa_operands (stmt);
1071 /* Now verify the built operands are the same as present in STMT. */
1072 def = gimple_vdef (stmt);
1073 if (def
1074 && TREE_CODE (def) == SSA_NAME)
1075 def = SSA_NAME_VAR (def);
1076 if (build_vdef != def)
1078 error ("virtual definition of statement not up-to-date");
1079 return true;
1081 if (gimple_vdef (stmt)
1082 && ((def_p = gimple_vdef_op (stmt)) == NULL_DEF_OPERAND_P
1083 || DEF_FROM_PTR (def_p) != gimple_vdef (stmt)))
1085 error ("virtual def operand missing for stmt");
1086 return true;
1089 use = gimple_vuse (stmt);
1090 if (use
1091 && TREE_CODE (use) == SSA_NAME)
1092 use = SSA_NAME_VAR (use);
1093 if (build_vuse != use)
1095 error ("virtual use of statement not up-to-date");
1096 return true;
1098 if (gimple_vuse (stmt)
1099 && ((use_p = gimple_vuse_op (stmt)) == NULL_USE_OPERAND_P
1100 || USE_FROM_PTR (use_p) != gimple_vuse (stmt)))
1102 error ("virtual use operand missing for stmt");
1103 return true;
1106 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1108 FOR_EACH_VEC_ELT (tree, build_uses, i, use)
1110 if (use_p->use == (tree *)use)
1112 VEC_replace (tree, build_uses, i, NULL_TREE);
1113 break;
1116 if (i == VEC_length (tree, build_uses))
1118 error ("excess use operand for stmt");
1119 debug_generic_expr (USE_FROM_PTR (use_p));
1120 return true;
1123 FOR_EACH_VEC_ELT (tree, build_uses, i, use)
1124 if (use != NULL_TREE)
1126 error ("use operand missing for stmt");
1127 debug_generic_expr (*(tree *)use);
1128 return true;
1131 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_DEF)
1133 FOR_EACH_VEC_ELT (tree, build_defs, i, def)
1135 if (def_p == (tree *)def)
1137 VEC_replace (tree, build_defs, i, NULL_TREE);
1138 break;
1141 if (i == VEC_length (tree, build_defs))
1143 error ("excess def operand for stmt");
1144 debug_generic_expr (DEF_FROM_PTR (def_p));
1145 return true;
1148 FOR_EACH_VEC_ELT (tree, build_defs, i, def)
1149 if (def != NULL_TREE)
1151 error ("def operand missing for stmt");
1152 debug_generic_expr (*(tree *)def);
1153 return true;
1156 if (gimple_has_volatile_ops (stmt) != volatile_p)
1158 error ("stmt volatile flag not up-to-date");
1159 return true;
1162 cleanup_build_arrays ();
1163 return false;
1167 /* Releases the operands of STMT back to their freelists, and clears
1168 the stmt operand lists. */
1170 void
1171 free_stmt_operands (gimple stmt)
1173 def_optype_p defs = gimple_def_ops (stmt), last_def;
1174 use_optype_p uses = gimple_use_ops (stmt), last_use;
1176 if (defs)
1178 for (last_def = defs; last_def->next; last_def = last_def->next)
1179 continue;
1180 last_def->next = gimple_ssa_operands (cfun)->free_defs;
1181 gimple_ssa_operands (cfun)->free_defs = defs;
1182 gimple_set_def_ops (stmt, NULL);
1185 if (uses)
1187 for (last_use = uses; last_use->next; last_use = last_use->next)
1188 delink_imm_use (USE_OP_PTR (last_use));
1189 delink_imm_use (USE_OP_PTR (last_use));
1190 last_use->next = gimple_ssa_operands (cfun)->free_uses;
1191 gimple_ssa_operands (cfun)->free_uses = uses;
1192 gimple_set_use_ops (stmt, NULL);
1195 if (gimple_has_mem_ops (stmt))
1197 gimple_set_vuse (stmt, NULL_TREE);
1198 gimple_set_vdef (stmt, NULL_TREE);
1203 /* Get the operands of statement STMT. */
1205 void
1206 update_stmt_operands (gimple stmt)
1208 /* If update_stmt_operands is called before SSA is initialized, do
1209 nothing. */
1210 if (!ssa_operands_active (cfun))
1211 return;
1213 timevar_push (TV_TREE_OPS);
1215 /* If the stmt is a noreturn call queue it to be processed by
1216 split_bbs_on_noreturn_calls during cfg cleanup. */
1217 if (is_gimple_call (stmt)
1218 && gimple_call_noreturn_p (stmt))
1219 VEC_safe_push (gimple, gc, MODIFIED_NORETURN_CALLS (cfun), stmt);
1221 gcc_assert (gimple_modified_p (stmt));
1222 build_ssa_operands (stmt);
1223 gimple_set_modified (stmt, false);
1225 timevar_pop (TV_TREE_OPS);
1229 /* Swap operands EXP0 and EXP1 in statement STMT. No attempt is done
1230 to test the validity of the swap operation. */
1232 void
1233 swap_tree_operands (gimple stmt, tree *exp0, tree *exp1)
1235 tree op0, op1;
1236 op0 = *exp0;
1237 op1 = *exp1;
1239 /* If the operand cache is active, attempt to preserve the relative
1240 positions of these two operands in their respective immediate use
1241 lists by adjusting their use pointer to point to the new
1242 operand position. */
1243 if (ssa_operands_active (cfun) && op0 != op1)
1245 use_optype_p use0, use1, ptr;
1246 use0 = use1 = NULL;
1248 /* Find the 2 operands in the cache, if they are there. */
1249 for (ptr = gimple_use_ops (stmt); ptr; ptr = ptr->next)
1250 if (USE_OP_PTR (ptr)->use == exp0)
1252 use0 = ptr;
1253 break;
1256 for (ptr = gimple_use_ops (stmt); ptr; ptr = ptr->next)
1257 if (USE_OP_PTR (ptr)->use == exp1)
1259 use1 = ptr;
1260 break;
1263 /* And adjust their location to point to the new position of the
1264 operand. */
1265 if (use0)
1266 USE_OP_PTR (use0)->use = exp1;
1267 if (use1)
1268 USE_OP_PTR (use1)->use = exp0;
1271 /* Now swap the data. */
1272 *exp0 = op1;
1273 *exp1 = op0;
1277 /* Scan the immediate_use list for VAR making sure its linked properly.
1278 Return TRUE if there is a problem and emit an error message to F. */
1280 DEBUG_FUNCTION bool
1281 verify_imm_links (FILE *f, tree var)
1283 use_operand_p ptr, prev, list;
1284 int count;
1286 gcc_assert (TREE_CODE (var) == SSA_NAME);
1288 list = &(SSA_NAME_IMM_USE_NODE (var));
1289 gcc_assert (list->use == NULL);
1291 if (list->prev == NULL)
1293 gcc_assert (list->next == NULL);
1294 return false;
1297 prev = list;
1298 count = 0;
1299 for (ptr = list->next; ptr != list; )
1301 if (prev != ptr->prev)
1302 goto error;
1304 if (ptr->use == NULL)
1305 goto error; /* 2 roots, or SAFE guard node. */
1306 else if (*(ptr->use) != var)
1307 goto error;
1309 prev = ptr;
1310 ptr = ptr->next;
1312 /* Avoid infinite loops. 50,000,000 uses probably indicates a
1313 problem. */
1314 if (count++ > 50000000)
1315 goto error;
1318 /* Verify list in the other direction. */
1319 prev = list;
1320 for (ptr = list->prev; ptr != list; )
1322 if (prev != ptr->next)
1323 goto error;
1324 prev = ptr;
1325 ptr = ptr->prev;
1326 if (count-- < 0)
1327 goto error;
1330 if (count != 0)
1331 goto error;
1333 return false;
1335 error:
1336 if (ptr->loc.stmt && gimple_modified_p (ptr->loc.stmt))
1338 fprintf (f, " STMT MODIFIED. - <%p> ", (void *)ptr->loc.stmt);
1339 print_gimple_stmt (f, ptr->loc.stmt, 0, TDF_SLIM);
1341 fprintf (f, " IMM ERROR : (use_p : tree - %p:%p)", (void *)ptr,
1342 (void *)ptr->use);
1343 print_generic_expr (f, USE_FROM_PTR (ptr), TDF_SLIM);
1344 fprintf(f, "\n");
1345 return true;
1349 /* Dump all the immediate uses to FILE. */
1351 void
1352 dump_immediate_uses_for (FILE *file, tree var)
1354 imm_use_iterator iter;
1355 use_operand_p use_p;
1357 gcc_assert (var && TREE_CODE (var) == SSA_NAME);
1359 print_generic_expr (file, var, TDF_SLIM);
1360 fprintf (file, " : -->");
1361 if (has_zero_uses (var))
1362 fprintf (file, " no uses.\n");
1363 else
1364 if (has_single_use (var))
1365 fprintf (file, " single use.\n");
1366 else
1367 fprintf (file, "%d uses.\n", num_imm_uses (var));
1369 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
1371 if (use_p->loc.stmt == NULL && use_p->use == NULL)
1372 fprintf (file, "***end of stmt iterator marker***\n");
1373 else
1374 if (!is_gimple_reg (USE_FROM_PTR (use_p)))
1375 print_gimple_stmt (file, USE_STMT (use_p), 0, TDF_VOPS|TDF_MEMSYMS);
1376 else
1377 print_gimple_stmt (file, USE_STMT (use_p), 0, TDF_SLIM);
1379 fprintf(file, "\n");
1383 /* Dump all the immediate uses to FILE. */
1385 void
1386 dump_immediate_uses (FILE *file)
1388 tree var;
1389 unsigned int x;
1391 fprintf (file, "Immediate_uses: \n\n");
1392 for (x = 1; x < num_ssa_names; x++)
1394 var = ssa_name(x);
1395 if (!var)
1396 continue;
1397 dump_immediate_uses_for (file, var);
1402 /* Dump def-use edges on stderr. */
1404 DEBUG_FUNCTION void
1405 debug_immediate_uses (void)
1407 dump_immediate_uses (stderr);
1411 /* Dump def-use edges on stderr. */
1413 DEBUG_FUNCTION void
1414 debug_immediate_uses_for (tree var)
1416 dump_immediate_uses_for (stderr, var);
1420 /* Return true if OP, an SSA name or a DECL is a virtual operand. */
1422 bool
1423 virtual_operand_p (tree op)
1425 if (TREE_CODE (op) == SSA_NAME)
1427 op = SSA_NAME_VAR (op);
1428 if (!op)
1429 return false;
1432 if (TREE_CODE (op) == VAR_DECL)
1433 return VAR_DECL_IS_VIRTUAL_OPERAND (op);
1435 return false;
1438 /* Unlink STMTs virtual definition from the IL by propagating its use. */
1440 void
1441 unlink_stmt_vdef (gimple stmt)
1443 use_operand_p use_p;
1444 imm_use_iterator iter;
1445 gimple use_stmt;
1446 tree vdef = gimple_vdef (stmt);
1447 tree vuse = gimple_vuse (stmt);
1449 if (!vdef
1450 || TREE_CODE (vdef) != SSA_NAME)
1451 return;
1453 FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
1455 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1456 SET_USE (use_p, vuse);
1459 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vdef))
1460 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vuse) = 1;