* inclhack.def (AAB_aix_fcntl): New fix.
[official-gcc.git] / gcc / tree-ssa-operands.c
blob421b35e70663f8c5c42d13fce831e8633c28f92f
1 /* SSA operands management for trees.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 2011, 2012
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "function.h"
29 #include "gimple-pretty-print.h"
30 #include "tree-flow.h"
31 #include "tree-inline.h"
32 #include "timevar.h"
33 #include "dumpfile.h"
34 #include "ggc.h"
35 #include "timevar.h"
36 #include "langhooks.h"
37 #include "diagnostic-core.h"
40 /* This file contains the code required to manage the operands cache of the
41 SSA optimizer. For every stmt, we maintain an operand cache in the stmt
42 annotation. This cache contains operands that will be of interest to
43 optimizers and other passes wishing to manipulate the IL.
45 The operand type are broken up into REAL and VIRTUAL operands. The real
46 operands are represented as pointers into the stmt's operand tree. Thus
47 any manipulation of the real operands will be reflected in the actual tree.
48 Virtual operands are represented solely in the cache, although the base
49 variable for the SSA_NAME may, or may not occur in the stmt's tree.
50 Manipulation of the virtual operands will not be reflected in the stmt tree.
52 The routines in this file are concerned with creating this operand cache
53 from a stmt tree.
55 The operand tree is the parsed by the various get_* routines which look
56 through the stmt tree for the occurrence of operands which may be of
57 interest, and calls are made to the append_* routines whenever one is
58 found. There are 4 of these routines, each representing one of the
59 4 types of operands. Defs, Uses, Virtual Uses, and Virtual May Defs.
61 The append_* routines check for duplication, and simply keep a list of
62 unique objects for each operand type in the build_* extendable vectors.
64 Once the stmt tree is completely parsed, the finalize_ssa_operands()
65 routine is called, which proceeds to perform the finalization routine
66 on each of the 4 operand vectors which have been built up.
68 If the stmt had a previous operand cache, the finalization routines
69 attempt to match up the new operands with the old ones. If it's a perfect
70 match, the old vector is simply reused. If it isn't a perfect match, then
71 a new vector is created and the new operands are placed there. For
72 virtual operands, if the previous cache had SSA_NAME version of a
73 variable, and that same variable occurs in the same operands cache, then
74 the new cache vector will also get the same SSA_NAME.
76 i.e., if a stmt had a VUSE of 'a_5', and 'a' occurs in the new
77 operand vector for VUSE, then the new vector will also be modified
78 such that it contains 'a_5' rather than 'a'. */
81 /* Flags to describe operand properties in helpers. */
83 /* By default, operands are loaded. */
84 #define opf_use 0
86 /* Operand is the target of an assignment expression or a
87 call-clobbered variable. */
88 #define opf_def (1 << 0)
90 /* No virtual operands should be created in the expression. This is used
91 when traversing ADDR_EXPR nodes which have different semantics than
92 other expressions. Inside an ADDR_EXPR node, the only operands that we
93 need to consider are indices into arrays. For instance, &a.b[i] should
94 generate a USE of 'i' but it should not generate a VUSE for 'a' nor a
95 VUSE for 'b'. */
96 #define opf_no_vops (1 << 1)
98 /* Operand is an implicit reference. This is used to distinguish
99 explicit assignments in the form of MODIFY_EXPR from
100 clobbering sites like function calls or ASM_EXPRs. */
101 #define opf_implicit (1 << 2)
103 /* Operand is in a place where address-taken does not imply addressable. */
104 #define opf_non_addressable (1 << 3)
106 /* Operand is in a place where opf_non_addressable does not apply. */
107 #define opf_not_non_addressable (1 << 4)
109 /* Array for building all the def operands. */
110 static VEC(tree,heap) *build_defs;
112 /* Array for building all the use operands. */
113 static VEC(tree,heap) *build_uses;
115 /* The built VDEF operand. */
116 static tree build_vdef;
118 /* The built VUSE operand. */
119 static tree build_vuse;
121 /* Bitmap obstack for our datastructures that needs to survive across
122 compilations of multiple functions. */
123 static bitmap_obstack operands_bitmap_obstack;
125 static void get_expr_operands (gimple, tree *, int);
127 /* Number of functions with initialized ssa_operands. */
128 static int n_initialized = 0;
131 /* Return true if the SSA operands cache is active. */
133 bool
134 ssa_operands_active (struct function *fun)
136 if (fun == NULL)
137 return false;
139 return fun->gimple_df && gimple_ssa_operands (fun)->ops_active;
143 /* Create the VOP variable, an artificial global variable to act as a
144 representative of all of the virtual operands FUD chain. */
146 static void
147 create_vop_var (struct function *fn)
149 tree global_var;
151 gcc_assert (fn->gimple_df->vop == NULL_TREE);
153 global_var = build_decl (BUILTINS_LOCATION, VAR_DECL,
154 get_identifier (".MEM"),
155 void_type_node);
156 DECL_ARTIFICIAL (global_var) = 1;
157 TREE_READONLY (global_var) = 0;
158 DECL_EXTERNAL (global_var) = 1;
159 TREE_STATIC (global_var) = 1;
160 TREE_USED (global_var) = 1;
161 DECL_CONTEXT (global_var) = NULL_TREE;
162 TREE_THIS_VOLATILE (global_var) = 0;
163 TREE_ADDRESSABLE (global_var) = 0;
164 VAR_DECL_IS_VIRTUAL_OPERAND (global_var) = 1;
166 fn->gimple_df->vop = global_var;
169 /* These are the sizes of the operand memory buffer in bytes which gets
170 allocated each time more operands space is required. The final value is
171 the amount that is allocated every time after that.
172 In 1k we can fit 25 use operands (or 63 def operands) on a host with
173 8 byte pointers, that would be 10 statements each with 1 def and 2
174 uses. */
176 #define OP_SIZE_INIT 0
177 #define OP_SIZE_1 (1024 - sizeof (void *))
178 #define OP_SIZE_2 (1024 * 4 - sizeof (void *))
179 #define OP_SIZE_3 (1024 * 16 - sizeof (void *))
181 /* Initialize the operand cache routines. */
183 void
184 init_ssa_operands (struct function *fn)
186 if (!n_initialized++)
188 build_defs = VEC_alloc (tree, heap, 5);
189 build_uses = VEC_alloc (tree, heap, 10);
190 build_vuse = NULL_TREE;
191 build_vdef = NULL_TREE;
192 bitmap_obstack_initialize (&operands_bitmap_obstack);
195 gcc_assert (gimple_ssa_operands (fn)->operand_memory == NULL);
196 gimple_ssa_operands (fn)->operand_memory_index
197 = gimple_ssa_operands (fn)->ssa_operand_mem_size;
198 gimple_ssa_operands (fn)->ops_active = true;
199 gimple_ssa_operands (fn)->ssa_operand_mem_size = OP_SIZE_INIT;
200 create_vop_var (fn);
204 /* Dispose of anything required by the operand routines. */
206 void
207 fini_ssa_operands (void)
209 struct ssa_operand_memory_d *ptr;
211 if (!--n_initialized)
213 VEC_free (tree, heap, build_defs);
214 VEC_free (tree, heap, build_uses);
215 build_vdef = NULL_TREE;
216 build_vuse = NULL_TREE;
219 gimple_ssa_operands (cfun)->free_defs = NULL;
220 gimple_ssa_operands (cfun)->free_uses = NULL;
222 while ((ptr = gimple_ssa_operands (cfun)->operand_memory) != NULL)
224 gimple_ssa_operands (cfun)->operand_memory
225 = gimple_ssa_operands (cfun)->operand_memory->next;
226 ggc_free (ptr);
229 gimple_ssa_operands (cfun)->ops_active = false;
231 if (!n_initialized)
232 bitmap_obstack_release (&operands_bitmap_obstack);
234 cfun->gimple_df->vop = NULL_TREE;
238 /* Return memory for an operand of size SIZE. */
240 static inline void *
241 ssa_operand_alloc (unsigned size)
243 char *ptr;
245 gcc_assert (size == sizeof (struct use_optype_d)
246 || size == sizeof (struct def_optype_d));
248 if (gimple_ssa_operands (cfun)->operand_memory_index + size
249 >= gimple_ssa_operands (cfun)->ssa_operand_mem_size)
251 struct ssa_operand_memory_d *ptr;
253 switch (gimple_ssa_operands (cfun)->ssa_operand_mem_size)
255 case OP_SIZE_INIT:
256 gimple_ssa_operands (cfun)->ssa_operand_mem_size = OP_SIZE_1;
257 break;
258 case OP_SIZE_1:
259 gimple_ssa_operands (cfun)->ssa_operand_mem_size = OP_SIZE_2;
260 break;
261 case OP_SIZE_2:
262 case OP_SIZE_3:
263 gimple_ssa_operands (cfun)->ssa_operand_mem_size = OP_SIZE_3;
264 break;
265 default:
266 gcc_unreachable ();
270 ptr = ggc_alloc_ssa_operand_memory_d (sizeof (void *)
271 + gimple_ssa_operands (cfun)->ssa_operand_mem_size);
273 ptr->next = gimple_ssa_operands (cfun)->operand_memory;
274 gimple_ssa_operands (cfun)->operand_memory = ptr;
275 gimple_ssa_operands (cfun)->operand_memory_index = 0;
278 ptr = &(gimple_ssa_operands (cfun)->operand_memory
279 ->mem[gimple_ssa_operands (cfun)->operand_memory_index]);
280 gimple_ssa_operands (cfun)->operand_memory_index += size;
281 return ptr;
285 /* Allocate a DEF operand. */
287 static inline struct def_optype_d *
288 alloc_def (void)
290 struct def_optype_d *ret;
291 if (gimple_ssa_operands (cfun)->free_defs)
293 ret = gimple_ssa_operands (cfun)->free_defs;
294 gimple_ssa_operands (cfun)->free_defs
295 = gimple_ssa_operands (cfun)->free_defs->next;
297 else
298 ret = (struct def_optype_d *)
299 ssa_operand_alloc (sizeof (struct def_optype_d));
300 return ret;
304 /* Allocate a USE operand. */
306 static inline struct use_optype_d *
307 alloc_use (void)
309 struct use_optype_d *ret;
310 if (gimple_ssa_operands (cfun)->free_uses)
312 ret = gimple_ssa_operands (cfun)->free_uses;
313 gimple_ssa_operands (cfun)->free_uses
314 = gimple_ssa_operands (cfun)->free_uses->next;
316 else
317 ret = (struct use_optype_d *)
318 ssa_operand_alloc (sizeof (struct use_optype_d));
319 return ret;
323 /* Adds OP to the list of defs after LAST. */
325 static inline def_optype_p
326 add_def_op (tree *op, def_optype_p last)
328 def_optype_p new_def;
330 new_def = alloc_def ();
331 DEF_OP_PTR (new_def) = op;
332 last->next = new_def;
333 new_def->next = NULL;
334 return new_def;
338 /* Adds OP to the list of uses of statement STMT after LAST. */
340 static inline use_optype_p
341 add_use_op (gimple stmt, tree *op, use_optype_p last)
343 use_optype_p new_use;
345 new_use = alloc_use ();
346 USE_OP_PTR (new_use)->use = op;
347 link_imm_use_stmt (USE_OP_PTR (new_use), *op, stmt);
348 last->next = new_use;
349 new_use->next = NULL;
350 return new_use;
355 /* Takes elements from build_defs and turns them into def operands of STMT.
356 TODO -- Make build_defs VEC of tree *. */
358 static inline void
359 finalize_ssa_defs (gimple stmt)
361 unsigned new_i;
362 struct def_optype_d new_list;
363 def_optype_p old_ops, last;
364 unsigned int num = VEC_length (tree, build_defs);
366 /* There should only be a single real definition per assignment. */
367 gcc_assert ((stmt && gimple_code (stmt) != GIMPLE_ASSIGN) || num <= 1);
369 /* Pre-pend the vdef we may have built. */
370 if (build_vdef != NULL_TREE)
372 tree oldvdef = gimple_vdef (stmt);
373 if (oldvdef
374 && TREE_CODE (oldvdef) == SSA_NAME)
375 oldvdef = SSA_NAME_VAR (oldvdef);
376 if (oldvdef != build_vdef)
377 gimple_set_vdef (stmt, build_vdef);
378 VEC_safe_insert (tree, heap, build_defs, 0, (tree)gimple_vdef_ptr (stmt));
379 ++num;
382 new_list.next = NULL;
383 last = &new_list;
385 old_ops = gimple_def_ops (stmt);
387 new_i = 0;
389 /* Clear and unlink a no longer necessary VDEF. */
390 if (build_vdef == NULL_TREE
391 && gimple_vdef (stmt) != NULL_TREE)
393 if (TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
395 unlink_stmt_vdef (stmt);
396 release_ssa_name (gimple_vdef (stmt));
398 gimple_set_vdef (stmt, NULL_TREE);
401 /* If we have a non-SSA_NAME VDEF, mark it for renaming. */
402 if (gimple_vdef (stmt)
403 && TREE_CODE (gimple_vdef (stmt)) != SSA_NAME)
405 cfun->gimple_df->rename_vops = 1;
406 cfun->gimple_df->ssa_renaming_needed = 1;
409 /* Check for the common case of 1 def that hasn't changed. */
410 if (old_ops && old_ops->next == NULL && num == 1
411 && (tree *) VEC_index (tree, build_defs, 0) == DEF_OP_PTR (old_ops))
412 return;
414 /* If there is anything in the old list, free it. */
415 if (old_ops)
417 old_ops->next = gimple_ssa_operands (cfun)->free_defs;
418 gimple_ssa_operands (cfun)->free_defs = old_ops;
421 /* If there is anything remaining in the build_defs list, simply emit it. */
422 for ( ; new_i < num; new_i++)
424 tree *op = (tree *) VEC_index (tree, build_defs, new_i);
425 if (DECL_P (*op))
426 cfun->gimple_df->ssa_renaming_needed = 1;
427 last = add_def_op (op, last);
430 /* Now set the stmt's operands. */
431 gimple_set_def_ops (stmt, new_list.next);
435 /* Takes elements from build_uses and turns them into use operands of STMT.
436 TODO -- Make build_uses VEC of tree *. */
438 static inline void
439 finalize_ssa_uses (gimple stmt)
441 unsigned new_i;
442 struct use_optype_d new_list;
443 use_optype_p old_ops, ptr, last;
445 /* Pre-pend the VUSE we may have built. */
446 if (build_vuse != NULL_TREE)
448 tree oldvuse = gimple_vuse (stmt);
449 if (oldvuse
450 && TREE_CODE (oldvuse) == SSA_NAME)
451 oldvuse = SSA_NAME_VAR (oldvuse);
452 if (oldvuse != (build_vuse != NULL_TREE
453 ? build_vuse : build_vdef))
454 gimple_set_vuse (stmt, NULL_TREE);
455 VEC_safe_insert (tree, heap, build_uses, 0, (tree)gimple_vuse_ptr (stmt));
458 new_list.next = NULL;
459 last = &new_list;
461 old_ops = gimple_use_ops (stmt);
463 /* Clear a no longer necessary VUSE. */
464 if (build_vuse == NULL_TREE
465 && gimple_vuse (stmt) != NULL_TREE)
466 gimple_set_vuse (stmt, NULL_TREE);
468 /* If there is anything in the old list, free it. */
469 if (old_ops)
471 for (ptr = old_ops; ptr; ptr = ptr->next)
472 delink_imm_use (USE_OP_PTR (ptr));
473 old_ops->next = gimple_ssa_operands (cfun)->free_uses;
474 gimple_ssa_operands (cfun)->free_uses = old_ops;
477 /* If we added a VUSE, make sure to set the operand if it is not already
478 present and mark it for renaming. */
479 if (build_vuse != NULL_TREE
480 && gimple_vuse (stmt) == NULL_TREE)
482 gimple_set_vuse (stmt, gimple_vop (cfun));
483 cfun->gimple_df->rename_vops = 1;
484 cfun->gimple_df->ssa_renaming_needed = 1;
487 /* Now create nodes for all the new nodes. */
488 for (new_i = 0; new_i < VEC_length (tree, build_uses); new_i++)
490 tree *op = (tree *) VEC_index (tree, build_uses, new_i);
491 if (DECL_P (*op))
492 cfun->gimple_df->ssa_renaming_needed = 1;
493 last = add_use_op (stmt, op, last);
496 /* Now set the stmt's operands. */
497 gimple_set_use_ops (stmt, new_list.next);
501 /* Clear the in_list bits and empty the build array for VDEFs and
502 VUSEs. */
504 static inline void
505 cleanup_build_arrays (void)
507 build_vdef = NULL_TREE;
508 build_vuse = NULL_TREE;
509 VEC_truncate (tree, build_defs, 0);
510 VEC_truncate (tree, build_uses, 0);
514 /* Finalize all the build vectors, fill the new ones into INFO. */
516 static inline void
517 finalize_ssa_stmt_operands (gimple stmt)
519 finalize_ssa_defs (stmt);
520 finalize_ssa_uses (stmt);
521 cleanup_build_arrays ();
525 /* Start the process of building up operands vectors in INFO. */
527 static inline void
528 start_ssa_stmt_operands (void)
530 gcc_assert (VEC_length (tree, build_defs) == 0);
531 gcc_assert (VEC_length (tree, build_uses) == 0);
532 gcc_assert (build_vuse == NULL_TREE);
533 gcc_assert (build_vdef == NULL_TREE);
537 /* Add DEF_P to the list of pointers to operands. */
539 static inline void
540 append_def (tree *def_p)
542 VEC_safe_push (tree, heap, build_defs, (tree) def_p);
546 /* Add USE_P to the list of pointers to operands. */
548 static inline void
549 append_use (tree *use_p)
551 VEC_safe_push (tree, heap, build_uses, (tree) use_p);
555 /* Add VAR to the set of variables that require a VDEF operator. */
557 static inline void
558 append_vdef (tree var)
560 if (!optimize)
561 return;
563 gcc_assert ((build_vdef == NULL_TREE
564 || build_vdef == var)
565 && (build_vuse == NULL_TREE
566 || build_vuse == var));
568 build_vdef = var;
569 build_vuse = var;
573 /* Add VAR to the set of variables that require a VUSE operator. */
575 static inline void
576 append_vuse (tree var)
578 if (!optimize)
579 return;
581 gcc_assert (build_vuse == NULL_TREE
582 || build_vuse == var);
584 build_vuse = var;
587 /* Add virtual operands for STMT. FLAGS is as in get_expr_operands. */
589 static void
590 add_virtual_operand (gimple stmt ATTRIBUTE_UNUSED, int flags)
592 /* Add virtual operands to the stmt, unless the caller has specifically
593 requested not to do that (used when adding operands inside an
594 ADDR_EXPR expression). */
595 if (flags & opf_no_vops)
596 return;
598 gcc_assert (!is_gimple_debug (stmt));
600 if (flags & opf_def)
601 append_vdef (gimple_vop (cfun));
602 else
603 append_vuse (gimple_vop (cfun));
607 /* Add *VAR_P to the appropriate operand array for statement STMT.
608 FLAGS is as in get_expr_operands. If *VAR_P is a GIMPLE register,
609 it will be added to the statement's real operands, otherwise it is
610 added to virtual operands. */
612 static void
613 add_stmt_operand (tree *var_p, gimple stmt, int flags)
615 tree var = *var_p;
617 gcc_assert (SSA_VAR_P (*var_p));
619 if (is_gimple_reg (var))
621 /* The variable is a GIMPLE register. Add it to real operands. */
622 if (flags & opf_def)
623 append_def (var_p);
624 else
625 append_use (var_p);
627 else
629 /* Mark statements with volatile operands. */
630 if (!(flags & opf_no_vops)
631 && TREE_THIS_VOLATILE (var))
632 gimple_set_has_volatile_ops (stmt, true);
634 /* The variable is a memory access. Add virtual operands. */
635 add_virtual_operand (stmt, flags);
639 /* Mark the base address of REF as having its address taken.
640 REF may be a single variable whose address has been taken or any
641 other valid GIMPLE memory reference (structure reference, array,
642 etc). */
644 static void
645 mark_address_taken (tree ref)
647 tree var;
649 /* Note that it is *NOT OKAY* to use the target of a COMPONENT_REF
650 as the only thing we take the address of. If VAR is a structure,
651 taking the address of a field means that the whole structure may
652 be referenced using pointer arithmetic. See PR 21407 and the
653 ensuing mailing list discussion. */
654 var = get_base_address (ref);
655 if (var)
657 if (DECL_P (var))
658 TREE_ADDRESSABLE (var) = 1;
659 else if (TREE_CODE (var) == MEM_REF
660 && TREE_CODE (TREE_OPERAND (var, 0)) == ADDR_EXPR
661 && DECL_P (TREE_OPERAND (TREE_OPERAND (var, 0), 0)))
662 TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (var, 0), 0)) = 1;
667 /* A subroutine of get_expr_operands to handle MEM_REF.
669 STMT is the statement being processed, EXPR is the MEM_REF
670 that got us here.
672 FLAGS is as in get_expr_operands.
674 RECURSE_ON_BASE should be set to true if we want to continue
675 calling get_expr_operands on the base pointer, and false if
676 something else will do it for us. */
678 static void
679 get_indirect_ref_operands (gimple stmt, tree expr, int flags,
680 bool recurse_on_base)
682 tree *pptr = &TREE_OPERAND (expr, 0);
684 if (!(flags & opf_no_vops)
685 && TREE_THIS_VOLATILE (expr))
686 gimple_set_has_volatile_ops (stmt, true);
688 /* Add the VOP. */
689 add_virtual_operand (stmt, flags);
691 /* If requested, add a USE operand for the base pointer. */
692 if (recurse_on_base)
693 get_expr_operands (stmt, pptr,
694 opf_non_addressable | opf_use
695 | (flags & (opf_no_vops|opf_not_non_addressable)));
699 /* A subroutine of get_expr_operands to handle TARGET_MEM_REF. */
701 static void
702 get_tmr_operands (gimple stmt, tree expr, int flags)
704 if (!(flags & opf_no_vops)
705 && TREE_THIS_VOLATILE (expr))
706 gimple_set_has_volatile_ops (stmt, true);
708 /* First record the real operands. */
709 get_expr_operands (stmt, &TMR_BASE (expr), opf_use | (flags & opf_no_vops));
710 get_expr_operands (stmt, &TMR_INDEX (expr), opf_use | (flags & opf_no_vops));
711 get_expr_operands (stmt, &TMR_INDEX2 (expr), opf_use | (flags & opf_no_vops));
713 add_virtual_operand (stmt, flags);
717 /* If STMT is a call that may clobber globals and other symbols that
718 escape, add them to the VDEF/VUSE lists for it. */
720 static void
721 maybe_add_call_vops (gimple stmt)
723 int call_flags = gimple_call_flags (stmt);
725 /* If aliases have been computed already, add VDEF or VUSE
726 operands for all the symbols that have been found to be
727 call-clobbered. */
728 if (!(call_flags & ECF_NOVOPS))
730 /* A 'pure' or a 'const' function never call-clobbers anything.
731 A 'noreturn' function might, but since we don't return anyway
732 there is no point in recording that. */
733 if (!(call_flags & (ECF_PURE | ECF_CONST | ECF_NORETURN)))
734 add_virtual_operand (stmt, opf_def);
735 else if (!(call_flags & ECF_CONST))
736 add_virtual_operand (stmt, opf_use);
741 /* Scan operands in the ASM_EXPR stmt referred to in INFO. */
743 static void
744 get_asm_expr_operands (gimple stmt)
746 size_t i, noutputs;
747 const char **oconstraints;
748 const char *constraint;
749 bool allows_mem, allows_reg, is_inout;
751 noutputs = gimple_asm_noutputs (stmt);
752 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
754 /* Gather all output operands. */
755 for (i = 0; i < gimple_asm_noutputs (stmt); i++)
757 tree link = gimple_asm_output_op (stmt, i);
758 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
759 oconstraints[i] = constraint;
760 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
761 &allows_reg, &is_inout);
763 /* This should have been split in gimplify_asm_expr. */
764 gcc_assert (!allows_reg || !is_inout);
766 /* Memory operands are addressable. Note that STMT needs the
767 address of this operand. */
768 if (!allows_reg && allows_mem)
769 mark_address_taken (TREE_VALUE (link));
771 get_expr_operands (stmt, &TREE_VALUE (link), opf_def | opf_not_non_addressable);
774 /* Gather all input operands. */
775 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
777 tree link = gimple_asm_input_op (stmt, i);
778 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
779 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
780 &allows_mem, &allows_reg);
782 /* Memory operands are addressable. Note that STMT needs the
783 address of this operand. */
784 if (!allows_reg && allows_mem)
785 mark_address_taken (TREE_VALUE (link));
787 get_expr_operands (stmt, &TREE_VALUE (link), opf_not_non_addressable);
790 /* Clobber all memory and addressable symbols for asm ("" : : : "memory"); */
791 if (gimple_asm_clobbers_memory_p (stmt))
792 add_virtual_operand (stmt, opf_def);
796 /* Recursively scan the expression pointed to by EXPR_P in statement
797 STMT. FLAGS is one of the OPF_* constants modifying how to
798 interpret the operands found. */
800 static void
801 get_expr_operands (gimple stmt, tree *expr_p, int flags)
803 enum tree_code code;
804 enum tree_code_class codeclass;
805 tree expr = *expr_p;
806 int uflags = opf_use;
808 if (expr == NULL)
809 return;
811 if (is_gimple_debug (stmt))
812 uflags |= (flags & opf_no_vops);
814 code = TREE_CODE (expr);
815 codeclass = TREE_CODE_CLASS (code);
817 switch (code)
819 case ADDR_EXPR:
820 /* Taking the address of a variable does not represent a
821 reference to it, but the fact that the statement takes its
822 address will be of interest to some passes (e.g. alias
823 resolution). */
824 if ((!(flags & opf_non_addressable)
825 || (flags & opf_not_non_addressable))
826 && !is_gimple_debug (stmt))
827 mark_address_taken (TREE_OPERAND (expr, 0));
829 /* If the address is invariant, there may be no interesting
830 variable references inside. */
831 if (is_gimple_min_invariant (expr))
832 return;
834 /* Otherwise, there may be variables referenced inside but there
835 should be no VUSEs created, since the referenced objects are
836 not really accessed. The only operands that we should find
837 here are ARRAY_REF indices which will always be real operands
838 (GIMPLE does not allow non-registers as array indices). */
839 flags |= opf_no_vops;
840 get_expr_operands (stmt, &TREE_OPERAND (expr, 0),
841 flags | opf_not_non_addressable);
842 return;
844 case SSA_NAME:
845 case VAR_DECL:
846 case PARM_DECL:
847 case RESULT_DECL:
848 add_stmt_operand (expr_p, stmt, flags);
849 return;
851 case DEBUG_EXPR_DECL:
852 gcc_assert (gimple_debug_bind_p (stmt));
853 return;
855 case MEM_REF:
856 get_indirect_ref_operands (stmt, expr, flags, true);
857 return;
859 case TARGET_MEM_REF:
860 get_tmr_operands (stmt, expr, flags);
861 return;
863 case ARRAY_REF:
864 case ARRAY_RANGE_REF:
865 case COMPONENT_REF:
866 case REALPART_EXPR:
867 case IMAGPART_EXPR:
869 if (!(flags & opf_no_vops)
870 && TREE_THIS_VOLATILE (expr))
871 gimple_set_has_volatile_ops (stmt, true);
873 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
875 if (code == COMPONENT_REF)
877 if (!(flags & opf_no_vops)
878 && TREE_THIS_VOLATILE (TREE_OPERAND (expr, 1)))
879 gimple_set_has_volatile_ops (stmt, true);
880 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), uflags);
882 else if (code == ARRAY_REF || code == ARRAY_RANGE_REF)
884 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), uflags);
885 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), uflags);
886 get_expr_operands (stmt, &TREE_OPERAND (expr, 3), uflags);
889 return;
892 case WITH_SIZE_EXPR:
893 /* WITH_SIZE_EXPR is a pass-through reference to its first argument,
894 and an rvalue reference to its second argument. */
895 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), uflags);
896 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
897 return;
899 case COND_EXPR:
900 case VEC_COND_EXPR:
901 case VEC_PERM_EXPR:
902 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), uflags);
903 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), uflags);
904 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), uflags);
905 return;
907 case CONSTRUCTOR:
909 /* General aggregate CONSTRUCTORs have been decomposed, but they
910 are still in use as the COMPLEX_EXPR equivalent for vectors. */
911 constructor_elt *ce;
912 unsigned HOST_WIDE_INT idx;
914 /* A volatile constructor is actually TREE_CLOBBER_P, transfer
915 the volatility to the statement, don't use TREE_CLOBBER_P for
916 mirroring the other uses of THIS_VOLATILE in this file. */
917 if (!(flags & opf_no_vops)
918 && TREE_THIS_VOLATILE (expr))
919 gimple_set_has_volatile_ops (stmt, true);
921 for (idx = 0;
922 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (expr), idx, ce);
923 idx++)
924 get_expr_operands (stmt, &ce->value, uflags);
926 return;
929 case BIT_FIELD_REF:
930 if (!(flags & opf_no_vops)
931 && TREE_THIS_VOLATILE (expr))
932 gimple_set_has_volatile_ops (stmt, true);
933 /* FALLTHRU */
935 case VIEW_CONVERT_EXPR:
936 do_unary:
937 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
938 return;
940 case COMPOUND_EXPR:
941 case OBJ_TYPE_REF:
942 case ASSERT_EXPR:
943 do_binary:
945 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
946 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
947 return;
950 case DOT_PROD_EXPR:
951 case REALIGN_LOAD_EXPR:
952 case WIDEN_MULT_PLUS_EXPR:
953 case WIDEN_MULT_MINUS_EXPR:
954 case FMA_EXPR:
956 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
957 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
958 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), flags);
959 return;
962 case FUNCTION_DECL:
963 case LABEL_DECL:
964 case CONST_DECL:
965 case CASE_LABEL_EXPR:
966 /* Expressions that make no memory references. */
967 return;
969 default:
970 if (codeclass == tcc_unary)
971 goto do_unary;
972 if (codeclass == tcc_binary || codeclass == tcc_comparison)
973 goto do_binary;
974 if (codeclass == tcc_constant || codeclass == tcc_type)
975 return;
978 /* If we get here, something has gone wrong. */
979 #ifdef ENABLE_CHECKING
980 fprintf (stderr, "unhandled expression in get_expr_operands():\n");
981 debug_tree (expr);
982 fputs ("\n", stderr);
983 #endif
984 gcc_unreachable ();
988 /* Parse STMT looking for operands. When finished, the various
989 build_* operand vectors will have potential operands in them. */
991 static void
992 parse_ssa_operands (gimple stmt)
994 enum gimple_code code = gimple_code (stmt);
995 size_t i, n, start = 0;
997 switch (code)
999 case GIMPLE_ASM:
1000 get_asm_expr_operands (stmt);
1001 break;
1003 case GIMPLE_TRANSACTION:
1004 /* The start of a transaction is a memory barrier. */
1005 add_virtual_operand (stmt, opf_def | opf_use);
1006 break;
1008 case GIMPLE_DEBUG:
1009 if (gimple_debug_bind_p (stmt)
1010 && gimple_debug_bind_has_value_p (stmt))
1011 get_expr_operands (stmt, gimple_debug_bind_get_value_ptr (stmt),
1012 opf_use | opf_no_vops);
1013 break;
1015 case GIMPLE_RETURN:
1016 append_vuse (gimple_vop (cfun));
1017 goto do_default;
1019 case GIMPLE_CALL:
1020 /* Add call-clobbered operands, if needed. */
1021 maybe_add_call_vops (stmt);
1022 /* FALLTHRU */
1024 case GIMPLE_ASSIGN:
1025 get_expr_operands (stmt, gimple_op_ptr (stmt, 0), opf_def);
1026 start = 1;
1027 /* FALLTHRU */
1029 default:
1030 do_default:
1031 n = gimple_num_ops (stmt);
1032 for (i = start; i < n; i++)
1033 get_expr_operands (stmt, gimple_op_ptr (stmt, i), opf_use);
1034 break;
1039 /* Create an operands cache for STMT. */
1041 static void
1042 build_ssa_operands (gimple stmt)
1044 /* Initially assume that the statement has no volatile operands. */
1045 gimple_set_has_volatile_ops (stmt, false);
1047 start_ssa_stmt_operands ();
1048 parse_ssa_operands (stmt);
1049 finalize_ssa_stmt_operands (stmt);
1052 /* Verifies SSA statement operands. */
1054 DEBUG_FUNCTION bool
1055 verify_ssa_operands (gimple stmt)
1057 use_operand_p use_p;
1058 def_operand_p def_p;
1059 ssa_op_iter iter;
1060 unsigned i;
1061 tree use, def;
1062 bool volatile_p = gimple_has_volatile_ops (stmt);
1064 /* build_ssa_operands w/o finalizing them. */
1065 gimple_set_has_volatile_ops (stmt, false);
1066 start_ssa_stmt_operands ();
1067 parse_ssa_operands (stmt);
1069 /* Now verify the built operands are the same as present in STMT. */
1070 def = gimple_vdef (stmt);
1071 if (def
1072 && TREE_CODE (def) == SSA_NAME)
1073 def = SSA_NAME_VAR (def);
1074 if (build_vdef != def)
1076 error ("virtual definition of statement not up-to-date");
1077 return true;
1079 if (gimple_vdef (stmt)
1080 && ((def_p = gimple_vdef_op (stmt)) == NULL_DEF_OPERAND_P
1081 || DEF_FROM_PTR (def_p) != gimple_vdef (stmt)))
1083 error ("virtual def operand missing for stmt");
1084 return true;
1087 use = gimple_vuse (stmt);
1088 if (use
1089 && TREE_CODE (use) == SSA_NAME)
1090 use = SSA_NAME_VAR (use);
1091 if (build_vuse != use)
1093 error ("virtual use of statement not up-to-date");
1094 return true;
1096 if (gimple_vuse (stmt)
1097 && ((use_p = gimple_vuse_op (stmt)) == NULL_USE_OPERAND_P
1098 || USE_FROM_PTR (use_p) != gimple_vuse (stmt)))
1100 error ("virtual use operand missing for stmt");
1101 return true;
1104 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1106 FOR_EACH_VEC_ELT (tree, build_uses, i, use)
1108 if (use_p->use == (tree *)use)
1110 VEC_replace (tree, build_uses, i, NULL_TREE);
1111 break;
1114 if (i == VEC_length (tree, build_uses))
1116 error ("excess use operand for stmt");
1117 debug_generic_expr (USE_FROM_PTR (use_p));
1118 return true;
1121 FOR_EACH_VEC_ELT (tree, build_uses, i, use)
1122 if (use != NULL_TREE)
1124 error ("use operand missing for stmt");
1125 debug_generic_expr (*(tree *)use);
1126 return true;
1129 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_DEF)
1131 FOR_EACH_VEC_ELT (tree, build_defs, i, def)
1133 if (def_p == (tree *)def)
1135 VEC_replace (tree, build_defs, i, NULL_TREE);
1136 break;
1139 if (i == VEC_length (tree, build_defs))
1141 error ("excess def operand for stmt");
1142 debug_generic_expr (DEF_FROM_PTR (def_p));
1143 return true;
1146 FOR_EACH_VEC_ELT (tree, build_defs, i, def)
1147 if (def != NULL_TREE)
1149 error ("def operand missing for stmt");
1150 debug_generic_expr (*(tree *)def);
1151 return true;
1154 if (gimple_has_volatile_ops (stmt) != volatile_p)
1156 error ("stmt volatile flag not up-to-date");
1157 return true;
1160 cleanup_build_arrays ();
1161 return false;
1165 /* Releases the operands of STMT back to their freelists, and clears
1166 the stmt operand lists. */
1168 void
1169 free_stmt_operands (gimple stmt)
1171 def_optype_p defs = gimple_def_ops (stmt), last_def;
1172 use_optype_p uses = gimple_use_ops (stmt), last_use;
1174 if (defs)
1176 for (last_def = defs; last_def->next; last_def = last_def->next)
1177 continue;
1178 last_def->next = gimple_ssa_operands (cfun)->free_defs;
1179 gimple_ssa_operands (cfun)->free_defs = defs;
1180 gimple_set_def_ops (stmt, NULL);
1183 if (uses)
1185 for (last_use = uses; last_use->next; last_use = last_use->next)
1186 delink_imm_use (USE_OP_PTR (last_use));
1187 delink_imm_use (USE_OP_PTR (last_use));
1188 last_use->next = gimple_ssa_operands (cfun)->free_uses;
1189 gimple_ssa_operands (cfun)->free_uses = uses;
1190 gimple_set_use_ops (stmt, NULL);
1193 if (gimple_has_mem_ops (stmt))
1195 gimple_set_vuse (stmt, NULL_TREE);
1196 gimple_set_vdef (stmt, NULL_TREE);
1201 /* Get the operands of statement STMT. */
1203 void
1204 update_stmt_operands (gimple stmt)
1206 /* If update_stmt_operands is called before SSA is initialized, do
1207 nothing. */
1208 if (!ssa_operands_active (cfun))
1209 return;
1211 timevar_push (TV_TREE_OPS);
1213 /* If the stmt is a noreturn call queue it to be processed by
1214 split_bbs_on_noreturn_calls during cfg cleanup. */
1215 if (is_gimple_call (stmt)
1216 && gimple_call_noreturn_p (stmt))
1217 VEC_safe_push (gimple, gc, MODIFIED_NORETURN_CALLS (cfun), stmt);
1219 gcc_assert (gimple_modified_p (stmt));
1220 build_ssa_operands (stmt);
1221 gimple_set_modified (stmt, false);
1223 timevar_pop (TV_TREE_OPS);
1227 /* Swap operands EXP0 and EXP1 in statement STMT. No attempt is done
1228 to test the validity of the swap operation. */
1230 void
1231 swap_tree_operands (gimple stmt, tree *exp0, tree *exp1)
1233 tree op0, op1;
1234 op0 = *exp0;
1235 op1 = *exp1;
1237 /* If the operand cache is active, attempt to preserve the relative
1238 positions of these two operands in their respective immediate use
1239 lists by adjusting their use pointer to point to the new
1240 operand position. */
1241 if (ssa_operands_active (cfun) && op0 != op1)
1243 use_optype_p use0, use1, ptr;
1244 use0 = use1 = NULL;
1246 /* Find the 2 operands in the cache, if they are there. */
1247 for (ptr = gimple_use_ops (stmt); ptr; ptr = ptr->next)
1248 if (USE_OP_PTR (ptr)->use == exp0)
1250 use0 = ptr;
1251 break;
1254 for (ptr = gimple_use_ops (stmt); ptr; ptr = ptr->next)
1255 if (USE_OP_PTR (ptr)->use == exp1)
1257 use1 = ptr;
1258 break;
1261 /* And adjust their location to point to the new position of the
1262 operand. */
1263 if (use0)
1264 USE_OP_PTR (use0)->use = exp1;
1265 if (use1)
1266 USE_OP_PTR (use1)->use = exp0;
1269 /* Now swap the data. */
1270 *exp0 = op1;
1271 *exp1 = op0;
1275 /* Scan the immediate_use list for VAR making sure its linked properly.
1276 Return TRUE if there is a problem and emit an error message to F. */
1278 DEBUG_FUNCTION bool
1279 verify_imm_links (FILE *f, tree var)
1281 use_operand_p ptr, prev, list;
1282 int count;
1284 gcc_assert (TREE_CODE (var) == SSA_NAME);
1286 list = &(SSA_NAME_IMM_USE_NODE (var));
1287 gcc_assert (list->use == NULL);
1289 if (list->prev == NULL)
1291 gcc_assert (list->next == NULL);
1292 return false;
1295 prev = list;
1296 count = 0;
1297 for (ptr = list->next; ptr != list; )
1299 if (prev != ptr->prev)
1300 goto error;
1302 if (ptr->use == NULL)
1303 goto error; /* 2 roots, or SAFE guard node. */
1304 else if (*(ptr->use) != var)
1305 goto error;
1307 prev = ptr;
1308 ptr = ptr->next;
1310 /* Avoid infinite loops. 50,000,000 uses probably indicates a
1311 problem. */
1312 if (count++ > 50000000)
1313 goto error;
1316 /* Verify list in the other direction. */
1317 prev = list;
1318 for (ptr = list->prev; ptr != list; )
1320 if (prev != ptr->next)
1321 goto error;
1322 prev = ptr;
1323 ptr = ptr->prev;
1324 if (count-- < 0)
1325 goto error;
1328 if (count != 0)
1329 goto error;
1331 return false;
1333 error:
1334 if (ptr->loc.stmt && gimple_modified_p (ptr->loc.stmt))
1336 fprintf (f, " STMT MODIFIED. - <%p> ", (void *)ptr->loc.stmt);
1337 print_gimple_stmt (f, ptr->loc.stmt, 0, TDF_SLIM);
1339 fprintf (f, " IMM ERROR : (use_p : tree - %p:%p)", (void *)ptr,
1340 (void *)ptr->use);
1341 print_generic_expr (f, USE_FROM_PTR (ptr), TDF_SLIM);
1342 fprintf(f, "\n");
1343 return true;
1347 /* Dump all the immediate uses to FILE. */
1349 void
1350 dump_immediate_uses_for (FILE *file, tree var)
1352 imm_use_iterator iter;
1353 use_operand_p use_p;
1355 gcc_assert (var && TREE_CODE (var) == SSA_NAME);
1357 print_generic_expr (file, var, TDF_SLIM);
1358 fprintf (file, " : -->");
1359 if (has_zero_uses (var))
1360 fprintf (file, " no uses.\n");
1361 else
1362 if (has_single_use (var))
1363 fprintf (file, " single use.\n");
1364 else
1365 fprintf (file, "%d uses.\n", num_imm_uses (var));
1367 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
1369 if (use_p->loc.stmt == NULL && use_p->use == NULL)
1370 fprintf (file, "***end of stmt iterator marker***\n");
1371 else
1372 if (!is_gimple_reg (USE_FROM_PTR (use_p)))
1373 print_gimple_stmt (file, USE_STMT (use_p), 0, TDF_VOPS|TDF_MEMSYMS);
1374 else
1375 print_gimple_stmt (file, USE_STMT (use_p), 0, TDF_SLIM);
1377 fprintf(file, "\n");
1381 /* Dump all the immediate uses to FILE. */
1383 void
1384 dump_immediate_uses (FILE *file)
1386 tree var;
1387 unsigned int x;
1389 fprintf (file, "Immediate_uses: \n\n");
1390 for (x = 1; x < num_ssa_names; x++)
1392 var = ssa_name(x);
1393 if (!var)
1394 continue;
1395 dump_immediate_uses_for (file, var);
1400 /* Dump def-use edges on stderr. */
1402 DEBUG_FUNCTION void
1403 debug_immediate_uses (void)
1405 dump_immediate_uses (stderr);
1409 /* Dump def-use edges on stderr. */
1411 DEBUG_FUNCTION void
1412 debug_immediate_uses_for (tree var)
1414 dump_immediate_uses_for (stderr, var);
1418 /* Return true if OP, an SSA name or a DECL is a virtual operand. */
1420 bool
1421 virtual_operand_p (tree op)
1423 if (TREE_CODE (op) == SSA_NAME)
1425 op = SSA_NAME_VAR (op);
1426 if (!op)
1427 return false;
1430 if (TREE_CODE (op) == VAR_DECL)
1431 return VAR_DECL_IS_VIRTUAL_OPERAND (op);
1433 return false;
1436 /* Unlink STMTs virtual definition from the IL by propagating its use. */
1438 void
1439 unlink_stmt_vdef (gimple stmt)
1441 use_operand_p use_p;
1442 imm_use_iterator iter;
1443 gimple use_stmt;
1444 tree vdef = gimple_vdef (stmt);
1445 tree vuse = gimple_vuse (stmt);
1447 if (!vdef
1448 || TREE_CODE (vdef) != SSA_NAME)
1449 return;
1451 FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
1453 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1454 SET_USE (use_p, vuse);
1457 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vdef))
1458 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vuse) = 1;