PR bootstrap/63496
[official-gcc.git] / gcc / tree-ssa-operands.c
bloba7ccf04bea94760a24e772e248cc7a97cfccd328
1 /* SSA operands management for trees.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "stmt.h"
26 #include "print-tree.h"
27 #include "flags.h"
28 #include "function.h"
29 #include "gimple-pretty-print.h"
30 #include "bitmap.h"
31 #include "basic-block.h"
32 #include "tree-ssa-alias.h"
33 #include "internal-fn.h"
34 #include "gimple-expr.h"
35 #include "is-a.h"
36 #include "gimple.h"
37 #include "gimple-ssa.h"
38 #include "tree-phinodes.h"
39 #include "ssa-iterators.h"
40 #include "stringpool.h"
41 #include "tree-ssanames.h"
42 #include "tree-inline.h"
43 #include "timevar.h"
44 #include "dumpfile.h"
45 #include "timevar.h"
46 #include "langhooks.h"
47 #include "diagnostic-core.h"
50 /* This file contains the code required to manage the operands cache of the
51 SSA optimizer. For every stmt, we maintain an operand cache in the stmt
52 annotation. This cache contains operands that will be of interest to
53 optimizers and other passes wishing to manipulate the IL.
55 The operand type are broken up into REAL and VIRTUAL operands. The real
56 operands are represented as pointers into the stmt's operand tree. Thus
57 any manipulation of the real operands will be reflected in the actual tree.
58 Virtual operands are represented solely in the cache, although the base
59 variable for the SSA_NAME may, or may not occur in the stmt's tree.
60 Manipulation of the virtual operands will not be reflected in the stmt tree.
62 The routines in this file are concerned with creating this operand cache
63 from a stmt tree.
65 The operand tree is the parsed by the various get_* routines which look
66 through the stmt tree for the occurrence of operands which may be of
67 interest, and calls are made to the append_* routines whenever one is
68 found. There are 4 of these routines, each representing one of the
69 4 types of operands. Defs, Uses, Virtual Uses, and Virtual May Defs.
71 The append_* routines check for duplication, and simply keep a list of
72 unique objects for each operand type in the build_* extendable vectors.
74 Once the stmt tree is completely parsed, the finalize_ssa_operands()
75 routine is called, which proceeds to perform the finalization routine
76 on each of the 4 operand vectors which have been built up.
78 If the stmt had a previous operand cache, the finalization routines
79 attempt to match up the new operands with the old ones. If it's a perfect
80 match, the old vector is simply reused. If it isn't a perfect match, then
81 a new vector is created and the new operands are placed there. For
82 virtual operands, if the previous cache had SSA_NAME version of a
83 variable, and that same variable occurs in the same operands cache, then
84 the new cache vector will also get the same SSA_NAME.
86 i.e., if a stmt had a VUSE of 'a_5', and 'a' occurs in the new
87 operand vector for VUSE, then the new vector will also be modified
88 such that it contains 'a_5' rather than 'a'. */
91 /* Flags to describe operand properties in helpers. */
93 /* By default, operands are loaded. */
94 #define opf_use 0
96 /* Operand is the target of an assignment expression or a
97 call-clobbered variable. */
98 #define opf_def (1 << 0)
100 /* No virtual operands should be created in the expression. This is used
101 when traversing ADDR_EXPR nodes which have different semantics than
102 other expressions. Inside an ADDR_EXPR node, the only operands that we
103 need to consider are indices into arrays. For instance, &a.b[i] should
104 generate a USE of 'i' but it should not generate a VUSE for 'a' nor a
105 VUSE for 'b'. */
106 #define opf_no_vops (1 << 1)
108 /* Operand is in a place where address-taken does not imply addressable. */
109 #define opf_non_addressable (1 << 3)
111 /* Operand is in a place where opf_non_addressable does not apply. */
112 #define opf_not_non_addressable (1 << 4)
114 /* Operand is having its address taken. */
115 #define opf_address_taken (1 << 5)
117 /* Array for building all the use operands. */
118 static vec<tree> build_uses;
120 /* The built VDEF operand. */
121 static tree build_vdef;
123 /* The built VUSE operand. */
124 static tree build_vuse;
126 /* Bitmap obstack for our datastructures that needs to survive across
127 compilations of multiple functions. */
128 static bitmap_obstack operands_bitmap_obstack;
130 static void get_expr_operands (struct function *, gimple, tree *, int);
132 /* Number of functions with initialized ssa_operands. */
133 static int n_initialized = 0;
135 /* Accessor to tree-ssa-operands.c caches. */
136 static inline struct ssa_operands *
137 gimple_ssa_operands (const struct function *fun)
139 return &fun->gimple_df->ssa_operands;
143 /* Return true if the SSA operands cache is active. */
145 bool
146 ssa_operands_active (struct function *fun)
148 if (fun == NULL)
149 return false;
151 return fun->gimple_df && gimple_ssa_operands (fun)->ops_active;
155 /* Create the VOP variable, an artificial global variable to act as a
156 representative of all of the virtual operands FUD chain. */
158 static void
159 create_vop_var (struct function *fn)
161 tree global_var;
163 gcc_assert (fn->gimple_df->vop == NULL_TREE);
165 global_var = build_decl (BUILTINS_LOCATION, VAR_DECL,
166 get_identifier (".MEM"),
167 void_type_node);
168 DECL_ARTIFICIAL (global_var) = 1;
169 DECL_IGNORED_P (global_var) = 1;
170 TREE_READONLY (global_var) = 0;
171 DECL_EXTERNAL (global_var) = 1;
172 TREE_STATIC (global_var) = 1;
173 TREE_USED (global_var) = 1;
174 DECL_CONTEXT (global_var) = NULL_TREE;
175 TREE_THIS_VOLATILE (global_var) = 0;
176 TREE_ADDRESSABLE (global_var) = 0;
177 VAR_DECL_IS_VIRTUAL_OPERAND (global_var) = 1;
179 fn->gimple_df->vop = global_var;
182 /* These are the sizes of the operand memory buffer in bytes which gets
183 allocated each time more operands space is required. The final value is
184 the amount that is allocated every time after that.
185 In 1k we can fit 25 use operands (or 63 def operands) on a host with
186 8 byte pointers, that would be 10 statements each with 1 def and 2
187 uses. */
189 #define OP_SIZE_INIT 0
190 #define OP_SIZE_1 (1024 - sizeof (void *))
191 #define OP_SIZE_2 (1024 * 4 - sizeof (void *))
192 #define OP_SIZE_3 (1024 * 16 - sizeof (void *))
194 /* Initialize the operand cache routines. */
196 void
197 init_ssa_operands (struct function *fn)
199 if (!n_initialized++)
201 build_uses.create (10);
202 build_vuse = NULL_TREE;
203 build_vdef = NULL_TREE;
204 bitmap_obstack_initialize (&operands_bitmap_obstack);
207 gcc_assert (gimple_ssa_operands (fn)->operand_memory == NULL);
208 gimple_ssa_operands (fn)->operand_memory_index
209 = gimple_ssa_operands (fn)->ssa_operand_mem_size;
210 gimple_ssa_operands (fn)->ops_active = true;
211 gimple_ssa_operands (fn)->ssa_operand_mem_size = OP_SIZE_INIT;
212 create_vop_var (fn);
216 /* Dispose of anything required by the operand routines. */
218 void
219 fini_ssa_operands (struct function *fn)
221 struct ssa_operand_memory_d *ptr;
223 if (!--n_initialized)
225 build_uses.release ();
226 build_vdef = NULL_TREE;
227 build_vuse = NULL_TREE;
230 gimple_ssa_operands (fn)->free_uses = NULL;
232 while ((ptr = gimple_ssa_operands (fn)->operand_memory) != NULL)
234 gimple_ssa_operands (fn)->operand_memory
235 = gimple_ssa_operands (fn)->operand_memory->next;
236 ggc_free (ptr);
239 gimple_ssa_operands (fn)->ops_active = false;
241 if (!n_initialized)
242 bitmap_obstack_release (&operands_bitmap_obstack);
244 fn->gimple_df->vop = NULL_TREE;
248 /* Return memory for an operand of size SIZE. */
250 static inline void *
251 ssa_operand_alloc (struct function *fn, unsigned size)
253 char *ptr;
255 gcc_assert (size == sizeof (struct use_optype_d));
257 if (gimple_ssa_operands (fn)->operand_memory_index + size
258 >= gimple_ssa_operands (fn)->ssa_operand_mem_size)
260 struct ssa_operand_memory_d *ptr;
262 switch (gimple_ssa_operands (fn)->ssa_operand_mem_size)
264 case OP_SIZE_INIT:
265 gimple_ssa_operands (fn)->ssa_operand_mem_size = OP_SIZE_1;
266 break;
267 case OP_SIZE_1:
268 gimple_ssa_operands (fn)->ssa_operand_mem_size = OP_SIZE_2;
269 break;
270 case OP_SIZE_2:
271 case OP_SIZE_3:
272 gimple_ssa_operands (fn)->ssa_operand_mem_size = OP_SIZE_3;
273 break;
274 default:
275 gcc_unreachable ();
279 ptr = (ssa_operand_memory_d *) ggc_internal_alloc
280 (sizeof (void *) + gimple_ssa_operands (fn)->ssa_operand_mem_size);
282 ptr->next = gimple_ssa_operands (fn)->operand_memory;
283 gimple_ssa_operands (fn)->operand_memory = ptr;
284 gimple_ssa_operands (fn)->operand_memory_index = 0;
287 ptr = &(gimple_ssa_operands (fn)->operand_memory
288 ->mem[gimple_ssa_operands (fn)->operand_memory_index]);
289 gimple_ssa_operands (fn)->operand_memory_index += size;
290 return ptr;
294 /* Allocate a USE operand. */
296 static inline struct use_optype_d *
297 alloc_use (struct function *fn)
299 struct use_optype_d *ret;
300 if (gimple_ssa_operands (fn)->free_uses)
302 ret = gimple_ssa_operands (fn)->free_uses;
303 gimple_ssa_operands (fn)->free_uses
304 = gimple_ssa_operands (fn)->free_uses->next;
306 else
307 ret = (struct use_optype_d *)
308 ssa_operand_alloc (fn, sizeof (struct use_optype_d));
309 return ret;
313 /* Adds OP to the list of uses of statement STMT after LAST. */
315 static inline use_optype_p
316 add_use_op (struct function *fn, gimple stmt, tree *op, use_optype_p last)
318 use_optype_p new_use;
320 new_use = alloc_use (fn);
321 USE_OP_PTR (new_use)->use = op;
322 link_imm_use_stmt (USE_OP_PTR (new_use), *op, stmt);
323 last->next = new_use;
324 new_use->next = NULL;
325 return new_use;
330 /* Takes elements from build_defs and turns them into def operands of STMT.
331 TODO -- Make build_defs vec of tree *. */
333 static inline void
334 finalize_ssa_defs (struct function *fn, gimple stmt)
336 /* Pre-pend the vdef we may have built. */
337 if (build_vdef != NULL_TREE)
339 tree oldvdef = gimple_vdef (stmt);
340 if (oldvdef
341 && TREE_CODE (oldvdef) == SSA_NAME)
342 oldvdef = SSA_NAME_VAR (oldvdef);
343 if (oldvdef != build_vdef)
344 gimple_set_vdef (stmt, build_vdef);
347 /* Clear and unlink a no longer necessary VDEF. */
348 if (build_vdef == NULL_TREE
349 && gimple_vdef (stmt) != NULL_TREE)
351 if (TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
353 unlink_stmt_vdef (stmt);
354 release_ssa_name_fn (fn, gimple_vdef (stmt));
356 gimple_set_vdef (stmt, NULL_TREE);
359 /* If we have a non-SSA_NAME VDEF, mark it for renaming. */
360 if (gimple_vdef (stmt)
361 && TREE_CODE (gimple_vdef (stmt)) != SSA_NAME)
363 fn->gimple_df->rename_vops = 1;
364 fn->gimple_df->ssa_renaming_needed = 1;
369 /* Takes elements from build_uses and turns them into use operands of STMT.
370 TODO -- Make build_uses vec of tree *. */
372 static inline void
373 finalize_ssa_uses (struct function *fn, gimple stmt)
375 unsigned new_i;
376 struct use_optype_d new_list;
377 use_optype_p old_ops, ptr, last;
379 /* Pre-pend the VUSE we may have built. */
380 if (build_vuse != NULL_TREE)
382 tree oldvuse = gimple_vuse (stmt);
383 if (oldvuse
384 && TREE_CODE (oldvuse) == SSA_NAME)
385 oldvuse = SSA_NAME_VAR (oldvuse);
386 if (oldvuse != (build_vuse != NULL_TREE
387 ? build_vuse : build_vdef))
388 gimple_set_vuse (stmt, NULL_TREE);
389 build_uses.safe_insert (0, (tree)gimple_vuse_ptr (stmt));
392 new_list.next = NULL;
393 last = &new_list;
395 old_ops = gimple_use_ops (stmt);
397 /* Clear a no longer necessary VUSE. */
398 if (build_vuse == NULL_TREE
399 && gimple_vuse (stmt) != NULL_TREE)
400 gimple_set_vuse (stmt, NULL_TREE);
402 /* If there is anything in the old list, free it. */
403 if (old_ops)
405 for (ptr = old_ops; ptr; ptr = ptr->next)
406 delink_imm_use (USE_OP_PTR (ptr));
407 old_ops->next = gimple_ssa_operands (fn)->free_uses;
408 gimple_ssa_operands (fn)->free_uses = old_ops;
411 /* If we added a VUSE, make sure to set the operand if it is not already
412 present and mark it for renaming. */
413 if (build_vuse != NULL_TREE
414 && gimple_vuse (stmt) == NULL_TREE)
416 gimple_set_vuse (stmt, gimple_vop (fn));
417 fn->gimple_df->rename_vops = 1;
418 fn->gimple_df->ssa_renaming_needed = 1;
421 /* Now create nodes for all the new nodes. */
422 for (new_i = 0; new_i < build_uses.length (); new_i++)
424 tree *op = (tree *) build_uses[new_i];
425 last = add_use_op (fn, stmt, op, last);
428 /* Now set the stmt's operands. */
429 gimple_set_use_ops (stmt, new_list.next);
433 /* Clear the in_list bits and empty the build array for VDEFs and
434 VUSEs. */
436 static inline void
437 cleanup_build_arrays (void)
439 build_vdef = NULL_TREE;
440 build_vuse = NULL_TREE;
441 build_uses.truncate (0);
445 /* Finalize all the build vectors, fill the new ones into INFO. */
447 static inline void
448 finalize_ssa_stmt_operands (struct function *fn, gimple stmt)
450 finalize_ssa_defs (fn, stmt);
451 finalize_ssa_uses (fn, stmt);
452 cleanup_build_arrays ();
456 /* Start the process of building up operands vectors in INFO. */
458 static inline void
459 start_ssa_stmt_operands (void)
461 gcc_assert (build_uses.length () == 0);
462 gcc_assert (build_vuse == NULL_TREE);
463 gcc_assert (build_vdef == NULL_TREE);
467 /* Add USE_P to the list of pointers to operands. */
469 static inline void
470 append_use (tree *use_p)
472 build_uses.safe_push ((tree) use_p);
476 /* Add VAR to the set of variables that require a VDEF operator. */
478 static inline void
479 append_vdef (tree var)
481 gcc_assert ((build_vdef == NULL_TREE
482 || build_vdef == var)
483 && (build_vuse == NULL_TREE
484 || build_vuse == var));
486 build_vdef = var;
487 build_vuse = var;
491 /* Add VAR to the set of variables that require a VUSE operator. */
493 static inline void
494 append_vuse (tree var)
496 gcc_assert (build_vuse == NULL_TREE
497 || build_vuse == var);
499 build_vuse = var;
502 /* Add virtual operands for STMT. FLAGS is as in get_expr_operands. */
504 static void
505 add_virtual_operand (struct function *fn,
506 gimple stmt ATTRIBUTE_UNUSED, int flags)
508 /* Add virtual operands to the stmt, unless the caller has specifically
509 requested not to do that (used when adding operands inside an
510 ADDR_EXPR expression). */
511 if (flags & opf_no_vops)
512 return;
514 gcc_assert (!is_gimple_debug (stmt));
516 if (flags & opf_def)
517 append_vdef (gimple_vop (fn));
518 else
519 append_vuse (gimple_vop (fn));
523 /* Add *VAR_P to the appropriate operand array for statement STMT.
524 FLAGS is as in get_expr_operands. If *VAR_P is a GIMPLE register,
525 it will be added to the statement's real operands, otherwise it is
526 added to virtual operands. */
528 static void
529 add_stmt_operand (struct function *fn, tree *var_p, gimple stmt, int flags)
531 tree var = *var_p;
533 gcc_assert (SSA_VAR_P (*var_p));
535 if (is_gimple_reg (var))
537 /* The variable is a GIMPLE register. Add it to real operands. */
538 if (flags & opf_def)
540 else
541 append_use (var_p);
542 if (DECL_P (*var_p))
543 fn->gimple_df->ssa_renaming_needed = 1;
545 else
547 /* Mark statements with volatile operands. */
548 if (!(flags & opf_no_vops)
549 && TREE_THIS_VOLATILE (var))
550 gimple_set_has_volatile_ops (stmt, true);
552 /* The variable is a memory access. Add virtual operands. */
553 add_virtual_operand (fn, stmt, flags);
557 /* Mark the base address of REF as having its address taken.
558 REF may be a single variable whose address has been taken or any
559 other valid GIMPLE memory reference (structure reference, array,
560 etc). */
562 static void
563 mark_address_taken (tree ref)
565 tree var;
567 /* Note that it is *NOT OKAY* to use the target of a COMPONENT_REF
568 as the only thing we take the address of. If VAR is a structure,
569 taking the address of a field means that the whole structure may
570 be referenced using pointer arithmetic. See PR 21407 and the
571 ensuing mailing list discussion. */
572 var = get_base_address (ref);
573 if (var)
575 if (DECL_P (var))
576 TREE_ADDRESSABLE (var) = 1;
577 else if (TREE_CODE (var) == MEM_REF
578 && TREE_CODE (TREE_OPERAND (var, 0)) == ADDR_EXPR
579 && DECL_P (TREE_OPERAND (TREE_OPERAND (var, 0), 0)))
580 TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (var, 0), 0)) = 1;
585 /* A subroutine of get_expr_operands to handle MEM_REF.
587 STMT is the statement being processed, EXPR is the MEM_REF
588 that got us here.
590 FLAGS is as in get_expr_operands. */
592 static void
593 get_mem_ref_operands (struct function *fn,
594 gimple stmt, tree expr, int flags)
596 tree *pptr = &TREE_OPERAND (expr, 0);
598 if (!(flags & opf_no_vops)
599 && TREE_THIS_VOLATILE (expr))
600 gimple_set_has_volatile_ops (stmt, true);
602 /* Add the VOP. */
603 add_virtual_operand (fn, stmt, flags);
605 /* If requested, add a USE operand for the base pointer. */
606 get_expr_operands (fn, stmt, pptr,
607 opf_non_addressable | opf_use
608 | (flags & (opf_no_vops|opf_not_non_addressable)));
612 /* A subroutine of get_expr_operands to handle TARGET_MEM_REF. */
614 static void
615 get_tmr_operands (struct function *fn, gimple stmt, tree expr, int flags)
617 if (!(flags & opf_no_vops)
618 && TREE_THIS_VOLATILE (expr))
619 gimple_set_has_volatile_ops (stmt, true);
621 /* First record the real operands. */
622 get_expr_operands (fn, stmt,
623 &TMR_BASE (expr), opf_use | (flags & opf_no_vops));
624 get_expr_operands (fn, stmt,
625 &TMR_INDEX (expr), opf_use | (flags & opf_no_vops));
626 get_expr_operands (fn, stmt,
627 &TMR_INDEX2 (expr), opf_use | (flags & opf_no_vops));
629 add_virtual_operand (fn, stmt, flags);
633 /* If STMT is a call that may clobber globals and other symbols that
634 escape, add them to the VDEF/VUSE lists for it. */
636 static void
637 maybe_add_call_vops (struct function *fn, gimple stmt)
639 int call_flags = gimple_call_flags (stmt);
641 /* If aliases have been computed already, add VDEF or VUSE
642 operands for all the symbols that have been found to be
643 call-clobbered. */
644 if (!(call_flags & ECF_NOVOPS))
646 /* A 'pure' or a 'const' function never call-clobbers anything. */
647 if (!(call_flags & (ECF_PURE | ECF_CONST)))
648 add_virtual_operand (fn, stmt, opf_def);
649 else if (!(call_flags & ECF_CONST))
650 add_virtual_operand (fn, stmt, opf_use);
655 /* Scan operands in the ASM_EXPR stmt referred to in INFO. */
657 static void
658 get_asm_stmt_operands (struct function *fn, gimple stmt)
660 size_t i, noutputs;
661 const char **oconstraints;
662 const char *constraint;
663 bool allows_mem, allows_reg, is_inout;
665 noutputs = gimple_asm_noutputs (stmt);
666 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
668 /* Gather all output operands. */
669 for (i = 0; i < gimple_asm_noutputs (stmt); i++)
671 tree link = gimple_asm_output_op (stmt, i);
672 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
673 oconstraints[i] = constraint;
674 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
675 &allows_reg, &is_inout);
677 /* This should have been split in gimplify_asm_expr. */
678 gcc_assert (!allows_reg || !is_inout);
680 /* Memory operands are addressable. Note that STMT needs the
681 address of this operand. */
682 if (!allows_reg && allows_mem)
683 mark_address_taken (TREE_VALUE (link));
685 get_expr_operands (fn, stmt,
686 &TREE_VALUE (link), opf_def | opf_not_non_addressable);
689 /* Gather all input operands. */
690 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
692 tree link = gimple_asm_input_op (stmt, i);
693 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
694 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
695 &allows_mem, &allows_reg);
697 /* Memory operands are addressable. Note that STMT needs the
698 address of this operand. */
699 if (!allows_reg && allows_mem)
700 mark_address_taken (TREE_VALUE (link));
702 get_expr_operands (fn, stmt, &TREE_VALUE (link), opf_not_non_addressable);
705 /* Clobber all memory and addressable symbols for asm ("" : : : "memory"); */
706 if (gimple_asm_clobbers_memory_p (stmt))
707 add_virtual_operand (fn, stmt, opf_def);
711 /* Recursively scan the expression pointed to by EXPR_P in statement
712 STMT. FLAGS is one of the OPF_* constants modifying how to
713 interpret the operands found. */
715 static void
716 get_expr_operands (struct function *fn, gimple stmt, tree *expr_p, int flags)
718 enum tree_code code;
719 enum tree_code_class codeclass;
720 tree expr = *expr_p;
721 int uflags = opf_use;
723 if (expr == NULL)
724 return;
726 if (is_gimple_debug (stmt))
727 uflags |= (flags & opf_no_vops);
729 code = TREE_CODE (expr);
730 codeclass = TREE_CODE_CLASS (code);
732 switch (code)
734 case ADDR_EXPR:
735 /* Taking the address of a variable does not represent a
736 reference to it, but the fact that the statement takes its
737 address will be of interest to some passes (e.g. alias
738 resolution). */
739 if ((!(flags & opf_non_addressable)
740 || (flags & opf_not_non_addressable))
741 && !is_gimple_debug (stmt))
742 mark_address_taken (TREE_OPERAND (expr, 0));
744 /* Otherwise, there may be variables referenced inside but there
745 should be no VUSEs created, since the referenced objects are
746 not really accessed. The only operands that we should find
747 here are ARRAY_REF indices which will always be real operands
748 (GIMPLE does not allow non-registers as array indices). */
749 flags |= opf_no_vops;
750 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 0),
751 flags | opf_not_non_addressable | opf_address_taken);
752 return;
754 case SSA_NAME:
755 case VAR_DECL:
756 case PARM_DECL:
757 case RESULT_DECL:
758 if (!(flags & opf_address_taken))
759 add_stmt_operand (fn, expr_p, stmt, flags);
760 return;
762 case DEBUG_EXPR_DECL:
763 gcc_assert (gimple_debug_bind_p (stmt));
764 return;
766 case MEM_REF:
767 get_mem_ref_operands (fn, stmt, expr, flags);
768 return;
770 case TARGET_MEM_REF:
771 get_tmr_operands (fn, stmt, expr, flags);
772 return;
774 case ARRAY_REF:
775 case ARRAY_RANGE_REF:
776 case COMPONENT_REF:
777 case REALPART_EXPR:
778 case IMAGPART_EXPR:
780 if (!(flags & opf_no_vops)
781 && TREE_THIS_VOLATILE (expr))
782 gimple_set_has_volatile_ops (stmt, true);
784 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 0), flags);
786 if (code == COMPONENT_REF)
788 if (!(flags & opf_no_vops)
789 && TREE_THIS_VOLATILE (TREE_OPERAND (expr, 1)))
790 gimple_set_has_volatile_ops (stmt, true);
791 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 2), uflags);
793 else if (code == ARRAY_REF || code == ARRAY_RANGE_REF)
795 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 1), uflags);
796 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 2), uflags);
797 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 3), uflags);
800 return;
803 case WITH_SIZE_EXPR:
804 /* WITH_SIZE_EXPR is a pass-through reference to its first argument,
805 and an rvalue reference to its second argument. */
806 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 1), uflags);
807 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 0), flags);
808 return;
810 case COND_EXPR:
811 case VEC_COND_EXPR:
812 case VEC_PERM_EXPR:
813 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 0), uflags);
814 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 1), uflags);
815 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 2), uflags);
816 return;
818 case CONSTRUCTOR:
820 /* General aggregate CONSTRUCTORs have been decomposed, but they
821 are still in use as the COMPLEX_EXPR equivalent for vectors. */
822 constructor_elt *ce;
823 unsigned HOST_WIDE_INT idx;
825 /* A volatile constructor is actually TREE_CLOBBER_P, transfer
826 the volatility to the statement, don't use TREE_CLOBBER_P for
827 mirroring the other uses of THIS_VOLATILE in this file. */
828 if (!(flags & opf_no_vops)
829 && TREE_THIS_VOLATILE (expr))
830 gimple_set_has_volatile_ops (stmt, true);
832 for (idx = 0;
833 vec_safe_iterate (CONSTRUCTOR_ELTS (expr), idx, &ce);
834 idx++)
835 get_expr_operands (fn, stmt, &ce->value, uflags);
837 return;
840 case BIT_FIELD_REF:
841 if (!(flags & opf_no_vops)
842 && TREE_THIS_VOLATILE (expr))
843 gimple_set_has_volatile_ops (stmt, true);
844 /* FALLTHRU */
846 case VIEW_CONVERT_EXPR:
847 do_unary:
848 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 0), flags);
849 return;
851 case COMPOUND_EXPR:
852 case OBJ_TYPE_REF:
853 case ASSERT_EXPR:
854 do_binary:
856 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 0), flags);
857 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 1), flags);
858 return;
861 case DOT_PROD_EXPR:
862 case SAD_EXPR:
863 case REALIGN_LOAD_EXPR:
864 case WIDEN_MULT_PLUS_EXPR:
865 case WIDEN_MULT_MINUS_EXPR:
866 case FMA_EXPR:
868 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 0), flags);
869 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 1), flags);
870 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 2), flags);
871 return;
874 case FUNCTION_DECL:
875 case LABEL_DECL:
876 case CONST_DECL:
877 case CASE_LABEL_EXPR:
878 /* Expressions that make no memory references. */
879 return;
881 default:
882 if (codeclass == tcc_unary)
883 goto do_unary;
884 if (codeclass == tcc_binary || codeclass == tcc_comparison)
885 goto do_binary;
886 if (codeclass == tcc_constant || codeclass == tcc_type)
887 return;
890 /* If we get here, something has gone wrong. */
891 #ifdef ENABLE_CHECKING
892 fprintf (stderr, "unhandled expression in get_expr_operands():\n");
893 debug_tree (expr);
894 fputs ("\n", stderr);
895 #endif
896 gcc_unreachable ();
900 /* Parse STMT looking for operands. When finished, the various
901 build_* operand vectors will have potential operands in them. */
903 static void
904 parse_ssa_operands (struct function *fn, gimple stmt)
906 enum gimple_code code = gimple_code (stmt);
907 size_t i, n, start = 0;
909 switch (code)
911 case GIMPLE_ASM:
912 get_asm_stmt_operands (fn, stmt);
913 break;
915 case GIMPLE_TRANSACTION:
916 /* The start of a transaction is a memory barrier. */
917 add_virtual_operand (fn, stmt, opf_def | opf_use);
918 break;
920 case GIMPLE_DEBUG:
921 if (gimple_debug_bind_p (stmt)
922 && gimple_debug_bind_has_value_p (stmt))
923 get_expr_operands (fn, stmt, gimple_debug_bind_get_value_ptr (stmt),
924 opf_use | opf_no_vops);
925 break;
927 case GIMPLE_RETURN:
928 append_vuse (gimple_vop (fn));
929 goto do_default;
931 case GIMPLE_CALL:
932 /* Add call-clobbered operands, if needed. */
933 maybe_add_call_vops (fn, stmt);
934 /* FALLTHRU */
936 case GIMPLE_ASSIGN:
937 get_expr_operands (fn, stmt, gimple_op_ptr (stmt, 0), opf_def);
938 start = 1;
939 /* FALLTHRU */
941 default:
942 do_default:
943 n = gimple_num_ops (stmt);
944 for (i = start; i < n; i++)
945 get_expr_operands (fn, stmt, gimple_op_ptr (stmt, i), opf_use);
946 break;
951 /* Create an operands cache for STMT. */
953 static void
954 build_ssa_operands (struct function *fn, gimple stmt)
956 /* Initially assume that the statement has no volatile operands. */
957 gimple_set_has_volatile_ops (stmt, false);
959 start_ssa_stmt_operands ();
960 parse_ssa_operands (fn, stmt);
961 finalize_ssa_stmt_operands (fn, stmt);
964 /* Verifies SSA statement operands. */
966 DEBUG_FUNCTION bool
967 verify_ssa_operands (struct function *fn, gimple stmt)
969 use_operand_p use_p;
970 def_operand_p def_p;
971 ssa_op_iter iter;
972 unsigned i;
973 tree use, def;
974 bool volatile_p = gimple_has_volatile_ops (stmt);
976 /* build_ssa_operands w/o finalizing them. */
977 gimple_set_has_volatile_ops (stmt, false);
978 start_ssa_stmt_operands ();
979 parse_ssa_operands (fn, stmt);
981 /* Now verify the built operands are the same as present in STMT. */
982 def = gimple_vdef (stmt);
983 if (def
984 && TREE_CODE (def) == SSA_NAME)
985 def = SSA_NAME_VAR (def);
986 if (build_vdef != def)
988 error ("virtual definition of statement not up-to-date");
989 return true;
991 if (gimple_vdef (stmt)
992 && ((def_p = gimple_vdef_op (stmt)) == NULL_DEF_OPERAND_P
993 || DEF_FROM_PTR (def_p) != gimple_vdef (stmt)))
995 error ("virtual def operand missing for stmt");
996 return true;
999 use = gimple_vuse (stmt);
1000 if (use
1001 && TREE_CODE (use) == SSA_NAME)
1002 use = SSA_NAME_VAR (use);
1003 if (build_vuse != use)
1005 error ("virtual use of statement not up-to-date");
1006 return true;
1008 if (gimple_vuse (stmt)
1009 && ((use_p = gimple_vuse_op (stmt)) == NULL_USE_OPERAND_P
1010 || USE_FROM_PTR (use_p) != gimple_vuse (stmt)))
1012 error ("virtual use operand missing for stmt");
1013 return true;
1016 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1018 FOR_EACH_VEC_ELT (build_uses, i, use)
1020 if (use_p->use == (tree *)use)
1022 build_uses[i] = NULL_TREE;
1023 break;
1026 if (i == build_uses.length ())
1028 error ("excess use operand for stmt");
1029 debug_generic_expr (USE_FROM_PTR (use_p));
1030 return true;
1033 FOR_EACH_VEC_ELT (build_uses, i, use)
1034 if (use != NULL_TREE)
1036 error ("use operand missing for stmt");
1037 debug_generic_expr (*(tree *)use);
1038 return true;
1041 if (gimple_has_volatile_ops (stmt) != volatile_p)
1043 error ("stmt volatile flag not up-to-date");
1044 return true;
1047 cleanup_build_arrays ();
1048 return false;
1052 /* Releases the operands of STMT back to their freelists, and clears
1053 the stmt operand lists. */
1055 void
1056 free_stmt_operands (struct function *fn, gimple stmt)
1058 use_optype_p uses = gimple_use_ops (stmt), last_use;
1060 if (uses)
1062 for (last_use = uses; last_use->next; last_use = last_use->next)
1063 delink_imm_use (USE_OP_PTR (last_use));
1064 delink_imm_use (USE_OP_PTR (last_use));
1065 last_use->next = gimple_ssa_operands (fn)->free_uses;
1066 gimple_ssa_operands (fn)->free_uses = uses;
1067 gimple_set_use_ops (stmt, NULL);
1070 if (gimple_has_mem_ops (stmt))
1072 gimple_set_vuse (stmt, NULL_TREE);
1073 gimple_set_vdef (stmt, NULL_TREE);
1078 /* Get the operands of statement STMT. */
1080 void
1081 update_stmt_operands (struct function *fn, gimple stmt)
1083 /* If update_stmt_operands is called before SSA is initialized, do
1084 nothing. */
1085 if (!ssa_operands_active (fn))
1086 return;
1088 timevar_push (TV_TREE_OPS);
1090 gcc_assert (gimple_modified_p (stmt));
1091 build_ssa_operands (fn, stmt);
1092 gimple_set_modified (stmt, false);
1094 timevar_pop (TV_TREE_OPS);
1098 /* Swap operands EXP0 and EXP1 in statement STMT. No attempt is done
1099 to test the validity of the swap operation. */
1101 void
1102 swap_ssa_operands (gimple stmt, tree *exp0, tree *exp1)
1104 tree op0, op1;
1105 op0 = *exp0;
1106 op1 = *exp1;
1108 if (op0 != op1)
1110 /* Attempt to preserve the relative positions of these two operands in
1111 their * respective immediate use lists by adjusting their use pointer
1112 to point to the new operand position. */
1113 use_optype_p use0, use1, ptr;
1114 use0 = use1 = NULL;
1116 /* Find the 2 operands in the cache, if they are there. */
1117 for (ptr = gimple_use_ops (stmt); ptr; ptr = ptr->next)
1118 if (USE_OP_PTR (ptr)->use == exp0)
1120 use0 = ptr;
1121 break;
1124 for (ptr = gimple_use_ops (stmt); ptr; ptr = ptr->next)
1125 if (USE_OP_PTR (ptr)->use == exp1)
1127 use1 = ptr;
1128 break;
1131 /* And adjust their location to point to the new position of the
1132 operand. */
1133 if (use0)
1134 USE_OP_PTR (use0)->use = exp1;
1135 if (use1)
1136 USE_OP_PTR (use1)->use = exp0;
1138 /* Now swap the data. */
1139 *exp0 = op1;
1140 *exp1 = op0;
1145 /* Scan the immediate_use list for VAR making sure its linked properly.
1146 Return TRUE if there is a problem and emit an error message to F. */
1148 DEBUG_FUNCTION bool
1149 verify_imm_links (FILE *f, tree var)
1151 use_operand_p ptr, prev, list;
1152 int count;
1154 gcc_assert (TREE_CODE (var) == SSA_NAME);
1156 list = &(SSA_NAME_IMM_USE_NODE (var));
1157 gcc_assert (list->use == NULL);
1159 if (list->prev == NULL)
1161 gcc_assert (list->next == NULL);
1162 return false;
1165 prev = list;
1166 count = 0;
1167 for (ptr = list->next; ptr != list; )
1169 if (prev != ptr->prev)
1170 goto error;
1172 if (ptr->use == NULL)
1173 goto error; /* 2 roots, or SAFE guard node. */
1174 else if (*(ptr->use) != var)
1175 goto error;
1177 prev = ptr;
1178 ptr = ptr->next;
1180 /* Avoid infinite loops. 50,000,000 uses probably indicates a
1181 problem. */
1182 if (count++ > 50000000)
1183 goto error;
1186 /* Verify list in the other direction. */
1187 prev = list;
1188 for (ptr = list->prev; ptr != list; )
1190 if (prev != ptr->next)
1191 goto error;
1192 prev = ptr;
1193 ptr = ptr->prev;
1194 if (count-- < 0)
1195 goto error;
1198 if (count != 0)
1199 goto error;
1201 return false;
1203 error:
1204 if (ptr->loc.stmt && gimple_modified_p (ptr->loc.stmt))
1206 fprintf (f, " STMT MODIFIED. - <%p> ", (void *)ptr->loc.stmt);
1207 print_gimple_stmt (f, ptr->loc.stmt, 0, TDF_SLIM);
1209 fprintf (f, " IMM ERROR : (use_p : tree - %p:%p)", (void *)ptr,
1210 (void *)ptr->use);
1211 print_generic_expr (f, USE_FROM_PTR (ptr), TDF_SLIM);
1212 fprintf (f, "\n");
1213 return true;
1217 /* Dump all the immediate uses to FILE. */
1219 void
1220 dump_immediate_uses_for (FILE *file, tree var)
1222 imm_use_iterator iter;
1223 use_operand_p use_p;
1225 gcc_assert (var && TREE_CODE (var) == SSA_NAME);
1227 print_generic_expr (file, var, TDF_SLIM);
1228 fprintf (file, " : -->");
1229 if (has_zero_uses (var))
1230 fprintf (file, " no uses.\n");
1231 else
1232 if (has_single_use (var))
1233 fprintf (file, " single use.\n");
1234 else
1235 fprintf (file, "%d uses.\n", num_imm_uses (var));
1237 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
1239 if (use_p->loc.stmt == NULL && use_p->use == NULL)
1240 fprintf (file, "***end of stmt iterator marker***\n");
1241 else
1242 if (!is_gimple_reg (USE_FROM_PTR (use_p)))
1243 print_gimple_stmt (file, USE_STMT (use_p), 0, TDF_VOPS|TDF_MEMSYMS);
1244 else
1245 print_gimple_stmt (file, USE_STMT (use_p), 0, TDF_SLIM);
1247 fprintf (file, "\n");
1251 /* Dump all the immediate uses to FILE. */
1253 void
1254 dump_immediate_uses (FILE *file)
1256 tree var;
1257 unsigned int x;
1259 fprintf (file, "Immediate_uses: \n\n");
1260 for (x = 1; x < num_ssa_names; x++)
1262 var = ssa_name (x);
1263 if (!var)
1264 continue;
1265 dump_immediate_uses_for (file, var);
1270 /* Dump def-use edges on stderr. */
1272 DEBUG_FUNCTION void
1273 debug_immediate_uses (void)
1275 dump_immediate_uses (stderr);
1279 /* Dump def-use edges on stderr. */
1281 DEBUG_FUNCTION void
1282 debug_immediate_uses_for (tree var)
1284 dump_immediate_uses_for (stderr, var);
1288 /* Unlink STMTs virtual definition from the IL by propagating its use. */
1290 void
1291 unlink_stmt_vdef (gimple stmt)
1293 use_operand_p use_p;
1294 imm_use_iterator iter;
1295 gimple use_stmt;
1296 tree vdef = gimple_vdef (stmt);
1297 tree vuse = gimple_vuse (stmt);
1299 if (!vdef
1300 || TREE_CODE (vdef) != SSA_NAME)
1301 return;
1303 FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
1305 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1306 SET_USE (use_p, vuse);
1309 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vdef))
1310 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vuse) = 1;
1314 /* Return true if the var whose chain of uses starts at PTR has no
1315 nondebug uses. */
1316 bool
1317 has_zero_uses_1 (const ssa_use_operand_t *head)
1319 const ssa_use_operand_t *ptr;
1321 for (ptr = head->next; ptr != head; ptr = ptr->next)
1322 if (!is_gimple_debug (USE_STMT (ptr)))
1323 return false;
1325 return true;
1329 /* Return true if the var whose chain of uses starts at PTR has a
1330 single nondebug use. Set USE_P and STMT to that single nondebug
1331 use, if so, or to NULL otherwise. */
1332 bool
1333 single_imm_use_1 (const ssa_use_operand_t *head,
1334 use_operand_p *use_p, gimple *stmt)
1336 ssa_use_operand_t *ptr, *single_use = 0;
1338 for (ptr = head->next; ptr != head; ptr = ptr->next)
1339 if (!is_gimple_debug (USE_STMT (ptr)))
1341 if (single_use)
1343 single_use = NULL;
1344 break;
1346 single_use = ptr;
1349 if (use_p)
1350 *use_p = single_use;
1352 if (stmt)
1353 *stmt = single_use ? single_use->loc.stmt : NULL;
1355 return single_use;