* config/rx/rx.c (ADD_RX_BUILTIN0): New macro, used for builtins
[official-gcc.git] / gcc / tree-ssa-operands.c
blob4e05d2df0467cec39da93b6c07cf945ba662f465
1 /* SSA operands management for trees.
2 Copyright (C) 2003-2013 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 "flags.h"
26 #include "function.h"
27 #include "gimple-pretty-print.h"
28 #include "bitmap.h"
29 #include "gimple.h"
30 #include "gimple-ssa.h"
31 #include "tree-phinodes.h"
32 #include "ssa-iterators.h"
33 #include "tree-ssanames.h"
34 #include "tree-inline.h"
35 #include "timevar.h"
36 #include "dumpfile.h"
37 #include "ggc.h"
38 #include "timevar.h"
39 #include "langhooks.h"
40 #include "diagnostic-core.h"
43 /* This file contains the code required to manage the operands cache of the
44 SSA optimizer. For every stmt, we maintain an operand cache in the stmt
45 annotation. This cache contains operands that will be of interest to
46 optimizers and other passes wishing to manipulate the IL.
48 The operand type are broken up into REAL and VIRTUAL operands. The real
49 operands are represented as pointers into the stmt's operand tree. Thus
50 any manipulation of the real operands will be reflected in the actual tree.
51 Virtual operands are represented solely in the cache, although the base
52 variable for the SSA_NAME may, or may not occur in the stmt's tree.
53 Manipulation of the virtual operands will not be reflected in the stmt tree.
55 The routines in this file are concerned with creating this operand cache
56 from a stmt tree.
58 The operand tree is the parsed by the various get_* routines which look
59 through the stmt tree for the occurrence of operands which may be of
60 interest, and calls are made to the append_* routines whenever one is
61 found. There are 4 of these routines, each representing one of the
62 4 types of operands. Defs, Uses, Virtual Uses, and Virtual May Defs.
64 The append_* routines check for duplication, and simply keep a list of
65 unique objects for each operand type in the build_* extendable vectors.
67 Once the stmt tree is completely parsed, the finalize_ssa_operands()
68 routine is called, which proceeds to perform the finalization routine
69 on each of the 4 operand vectors which have been built up.
71 If the stmt had a previous operand cache, the finalization routines
72 attempt to match up the new operands with the old ones. If it's a perfect
73 match, the old vector is simply reused. If it isn't a perfect match, then
74 a new vector is created and the new operands are placed there. For
75 virtual operands, if the previous cache had SSA_NAME version of a
76 variable, and that same variable occurs in the same operands cache, then
77 the new cache vector will also get the same SSA_NAME.
79 i.e., if a stmt had a VUSE of 'a_5', and 'a' occurs in the new
80 operand vector for VUSE, then the new vector will also be modified
81 such that it contains 'a_5' rather than 'a'. */
84 /* Flags to describe operand properties in helpers. */
86 /* By default, operands are loaded. */
87 #define opf_use 0
89 /* Operand is the target of an assignment expression or a
90 call-clobbered variable. */
91 #define opf_def (1 << 0)
93 /* No virtual operands should be created in the expression. This is used
94 when traversing ADDR_EXPR nodes which have different semantics than
95 other expressions. Inside an ADDR_EXPR node, the only operands that we
96 need to consider are indices into arrays. For instance, &a.b[i] should
97 generate a USE of 'i' but it should not generate a VUSE for 'a' nor a
98 VUSE for 'b'. */
99 #define opf_no_vops (1 << 1)
101 /* Operand is an implicit reference. This is used to distinguish
102 explicit assignments in the form of MODIFY_EXPR from
103 clobbering sites like function calls or ASM_EXPRs. */
104 #define opf_implicit (1 << 2)
106 /* Operand is in a place where address-taken does not imply addressable. */
107 #define opf_non_addressable (1 << 3)
109 /* Operand is in a place where opf_non_addressable does not apply. */
110 #define opf_not_non_addressable (1 << 4)
112 /* Array for building all the use operands. */
113 static vec<tree> 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;
130 /* Accessor to tree-ssa-operands.c caches. */
131 static inline struct ssa_operands *
132 gimple_ssa_operands (const struct function *fun)
134 return &fun->gimple_df->ssa_operands;
138 /* Return true if the SSA operands cache is active. */
140 bool
141 ssa_operands_active (struct function *fun)
143 if (fun == NULL)
144 return false;
146 return fun->gimple_df && gimple_ssa_operands (fun)->ops_active;
150 /* Create the VOP variable, an artificial global variable to act as a
151 representative of all of the virtual operands FUD chain. */
153 static void
154 create_vop_var (struct function *fn)
156 tree global_var;
158 gcc_assert (fn->gimple_df->vop == NULL_TREE);
160 global_var = build_decl (BUILTINS_LOCATION, VAR_DECL,
161 get_identifier (".MEM"),
162 void_type_node);
163 DECL_ARTIFICIAL (global_var) = 1;
164 TREE_READONLY (global_var) = 0;
165 DECL_EXTERNAL (global_var) = 1;
166 TREE_STATIC (global_var) = 1;
167 TREE_USED (global_var) = 1;
168 DECL_CONTEXT (global_var) = NULL_TREE;
169 TREE_THIS_VOLATILE (global_var) = 0;
170 TREE_ADDRESSABLE (global_var) = 0;
171 VAR_DECL_IS_VIRTUAL_OPERAND (global_var) = 1;
173 fn->gimple_df->vop = global_var;
176 /* These are the sizes of the operand memory buffer in bytes which gets
177 allocated each time more operands space is required. The final value is
178 the amount that is allocated every time after that.
179 In 1k we can fit 25 use operands (or 63 def operands) on a host with
180 8 byte pointers, that would be 10 statements each with 1 def and 2
181 uses. */
183 #define OP_SIZE_INIT 0
184 #define OP_SIZE_1 (1024 - sizeof (void *))
185 #define OP_SIZE_2 (1024 * 4 - sizeof (void *))
186 #define OP_SIZE_3 (1024 * 16 - sizeof (void *))
188 /* Initialize the operand cache routines. */
190 void
191 init_ssa_operands (struct function *fn)
193 if (!n_initialized++)
195 build_uses.create (10);
196 build_vuse = NULL_TREE;
197 build_vdef = NULL_TREE;
198 bitmap_obstack_initialize (&operands_bitmap_obstack);
201 gcc_assert (gimple_ssa_operands (fn)->operand_memory == NULL);
202 gimple_ssa_operands (fn)->operand_memory_index
203 = gimple_ssa_operands (fn)->ssa_operand_mem_size;
204 gimple_ssa_operands (fn)->ops_active = true;
205 gimple_ssa_operands (fn)->ssa_operand_mem_size = OP_SIZE_INIT;
206 create_vop_var (fn);
210 /* Dispose of anything required by the operand routines. */
212 void
213 fini_ssa_operands (void)
215 struct ssa_operand_memory_d *ptr;
217 if (!--n_initialized)
219 build_uses.release ();
220 build_vdef = NULL_TREE;
221 build_vuse = NULL_TREE;
224 gimple_ssa_operands (cfun)->free_uses = NULL;
226 while ((ptr = gimple_ssa_operands (cfun)->operand_memory) != NULL)
228 gimple_ssa_operands (cfun)->operand_memory
229 = gimple_ssa_operands (cfun)->operand_memory->next;
230 ggc_free (ptr);
233 gimple_ssa_operands (cfun)->ops_active = false;
235 if (!n_initialized)
236 bitmap_obstack_release (&operands_bitmap_obstack);
238 cfun->gimple_df->vop = NULL_TREE;
242 /* Return memory for an operand of size SIZE. */
244 static inline void *
245 ssa_operand_alloc (unsigned size)
247 char *ptr;
249 gcc_assert (size == sizeof (struct use_optype_d));
251 if (gimple_ssa_operands (cfun)->operand_memory_index + size
252 >= gimple_ssa_operands (cfun)->ssa_operand_mem_size)
254 struct ssa_operand_memory_d *ptr;
256 switch (gimple_ssa_operands (cfun)->ssa_operand_mem_size)
258 case OP_SIZE_INIT:
259 gimple_ssa_operands (cfun)->ssa_operand_mem_size = OP_SIZE_1;
260 break;
261 case OP_SIZE_1:
262 gimple_ssa_operands (cfun)->ssa_operand_mem_size = OP_SIZE_2;
263 break;
264 case OP_SIZE_2:
265 case OP_SIZE_3:
266 gimple_ssa_operands (cfun)->ssa_operand_mem_size = OP_SIZE_3;
267 break;
268 default:
269 gcc_unreachable ();
273 ptr = ggc_alloc_ssa_operand_memory_d (sizeof (void *)
274 + gimple_ssa_operands (cfun)->ssa_operand_mem_size);
276 ptr->next = gimple_ssa_operands (cfun)->operand_memory;
277 gimple_ssa_operands (cfun)->operand_memory = ptr;
278 gimple_ssa_operands (cfun)->operand_memory_index = 0;
281 ptr = &(gimple_ssa_operands (cfun)->operand_memory
282 ->mem[gimple_ssa_operands (cfun)->operand_memory_index]);
283 gimple_ssa_operands (cfun)->operand_memory_index += size;
284 return ptr;
288 /* Allocate a USE operand. */
290 static inline struct use_optype_d *
291 alloc_use (void)
293 struct use_optype_d *ret;
294 if (gimple_ssa_operands (cfun)->free_uses)
296 ret = gimple_ssa_operands (cfun)->free_uses;
297 gimple_ssa_operands (cfun)->free_uses
298 = gimple_ssa_operands (cfun)->free_uses->next;
300 else
301 ret = (struct use_optype_d *)
302 ssa_operand_alloc (sizeof (struct use_optype_d));
303 return ret;
307 /* Adds OP to the list of uses of statement STMT after LAST. */
309 static inline use_optype_p
310 add_use_op (gimple stmt, tree *op, use_optype_p last)
312 use_optype_p new_use;
314 new_use = alloc_use ();
315 USE_OP_PTR (new_use)->use = op;
316 link_imm_use_stmt (USE_OP_PTR (new_use), *op, stmt);
317 last->next = new_use;
318 new_use->next = NULL;
319 return new_use;
324 /* Takes elements from build_defs and turns them into def operands of STMT.
325 TODO -- Make build_defs vec of tree *. */
327 static inline void
328 finalize_ssa_defs (gimple stmt)
330 /* Pre-pend the vdef we may have built. */
331 if (build_vdef != NULL_TREE)
333 tree oldvdef = gimple_vdef (stmt);
334 if (oldvdef
335 && TREE_CODE (oldvdef) == SSA_NAME)
336 oldvdef = SSA_NAME_VAR (oldvdef);
337 if (oldvdef != build_vdef)
338 gimple_set_vdef (stmt, build_vdef);
341 /* Clear and unlink a no longer necessary VDEF. */
342 if (build_vdef == NULL_TREE
343 && gimple_vdef (stmt) != NULL_TREE)
345 if (TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
347 unlink_stmt_vdef (stmt);
348 release_ssa_name (gimple_vdef (stmt));
350 gimple_set_vdef (stmt, NULL_TREE);
353 /* If we have a non-SSA_NAME VDEF, mark it for renaming. */
354 if (gimple_vdef (stmt)
355 && TREE_CODE (gimple_vdef (stmt)) != SSA_NAME)
357 cfun->gimple_df->rename_vops = 1;
358 cfun->gimple_df->ssa_renaming_needed = 1;
363 /* Takes elements from build_uses and turns them into use operands of STMT.
364 TODO -- Make build_uses vec of tree *. */
366 static inline void
367 finalize_ssa_uses (gimple stmt)
369 unsigned new_i;
370 struct use_optype_d new_list;
371 use_optype_p old_ops, ptr, last;
373 /* Pre-pend the VUSE we may have built. */
374 if (build_vuse != NULL_TREE)
376 tree oldvuse = gimple_vuse (stmt);
377 if (oldvuse
378 && TREE_CODE (oldvuse) == SSA_NAME)
379 oldvuse = SSA_NAME_VAR (oldvuse);
380 if (oldvuse != (build_vuse != NULL_TREE
381 ? build_vuse : build_vdef))
382 gimple_set_vuse (stmt, NULL_TREE);
383 build_uses.safe_insert (0, (tree)gimple_vuse_ptr (stmt));
386 new_list.next = NULL;
387 last = &new_list;
389 old_ops = gimple_use_ops (stmt);
391 /* Clear a no longer necessary VUSE. */
392 if (build_vuse == NULL_TREE
393 && gimple_vuse (stmt) != NULL_TREE)
394 gimple_set_vuse (stmt, NULL_TREE);
396 /* If there is anything in the old list, free it. */
397 if (old_ops)
399 for (ptr = old_ops; ptr; ptr = ptr->next)
400 delink_imm_use (USE_OP_PTR (ptr));
401 old_ops->next = gimple_ssa_operands (cfun)->free_uses;
402 gimple_ssa_operands (cfun)->free_uses = old_ops;
405 /* If we added a VUSE, make sure to set the operand if it is not already
406 present and mark it for renaming. */
407 if (build_vuse != NULL_TREE
408 && gimple_vuse (stmt) == NULL_TREE)
410 gimple_set_vuse (stmt, gimple_vop (cfun));
411 cfun->gimple_df->rename_vops = 1;
412 cfun->gimple_df->ssa_renaming_needed = 1;
415 /* Now create nodes for all the new nodes. */
416 for (new_i = 0; new_i < build_uses.length (); new_i++)
418 tree *op = (tree *) build_uses[new_i];
419 last = add_use_op (stmt, op, last);
422 /* Now set the stmt's operands. */
423 gimple_set_use_ops (stmt, new_list.next);
427 /* Clear the in_list bits and empty the build array for VDEFs and
428 VUSEs. */
430 static inline void
431 cleanup_build_arrays (void)
433 build_vdef = NULL_TREE;
434 build_vuse = NULL_TREE;
435 build_uses.truncate (0);
439 /* Finalize all the build vectors, fill the new ones into INFO. */
441 static inline void
442 finalize_ssa_stmt_operands (gimple stmt)
444 finalize_ssa_defs (stmt);
445 finalize_ssa_uses (stmt);
446 cleanup_build_arrays ();
450 /* Start the process of building up operands vectors in INFO. */
452 static inline void
453 start_ssa_stmt_operands (void)
455 gcc_assert (build_uses.length () == 0);
456 gcc_assert (build_vuse == NULL_TREE);
457 gcc_assert (build_vdef == NULL_TREE);
461 /* Add USE_P to the list of pointers to operands. */
463 static inline void
464 append_use (tree *use_p)
466 build_uses.safe_push ((tree) use_p);
470 /* Add VAR to the set of variables that require a VDEF operator. */
472 static inline void
473 append_vdef (tree var)
475 if (!optimize)
476 return;
478 gcc_assert ((build_vdef == NULL_TREE
479 || build_vdef == var)
480 && (build_vuse == NULL_TREE
481 || build_vuse == var));
483 build_vdef = var;
484 build_vuse = var;
488 /* Add VAR to the set of variables that require a VUSE operator. */
490 static inline void
491 append_vuse (tree var)
493 if (!optimize)
494 return;
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 (gimple stmt ATTRIBUTE_UNUSED, int flags)
507 /* Add virtual operands to the stmt, unless the caller has specifically
508 requested not to do that (used when adding operands inside an
509 ADDR_EXPR expression). */
510 if (flags & opf_no_vops)
511 return;
513 gcc_assert (!is_gimple_debug (stmt));
515 if (flags & opf_def)
516 append_vdef (gimple_vop (cfun));
517 else
518 append_vuse (gimple_vop (cfun));
522 /* Add *VAR_P to the appropriate operand array for statement STMT.
523 FLAGS is as in get_expr_operands. If *VAR_P is a GIMPLE register,
524 it will be added to the statement's real operands, otherwise it is
525 added to virtual operands. */
527 static void
528 add_stmt_operand (tree *var_p, gimple stmt, int flags)
530 tree var = *var_p;
532 gcc_assert (SSA_VAR_P (*var_p));
534 if (is_gimple_reg (var))
536 /* The variable is a GIMPLE register. Add it to real operands. */
537 if (flags & opf_def)
539 else
540 append_use (var_p);
541 if (DECL_P (*var_p))
542 cfun->gimple_df->ssa_renaming_needed = 1;
544 else
546 /* Mark statements with volatile operands. */
547 if (!(flags & opf_no_vops)
548 && TREE_THIS_VOLATILE (var))
549 gimple_set_has_volatile_ops (stmt, true);
551 /* The variable is a memory access. Add virtual operands. */
552 add_virtual_operand (stmt, flags);
556 /* Mark the base address of REF as having its address taken.
557 REF may be a single variable whose address has been taken or any
558 other valid GIMPLE memory reference (structure reference, array,
559 etc). */
561 static void
562 mark_address_taken (tree ref)
564 tree var;
566 /* Note that it is *NOT OKAY* to use the target of a COMPONENT_REF
567 as the only thing we take the address of. If VAR is a structure,
568 taking the address of a field means that the whole structure may
569 be referenced using pointer arithmetic. See PR 21407 and the
570 ensuing mailing list discussion. */
571 var = get_base_address (ref);
572 if (var)
574 if (DECL_P (var))
575 TREE_ADDRESSABLE (var) = 1;
576 else if (TREE_CODE (var) == MEM_REF
577 && TREE_CODE (TREE_OPERAND (var, 0)) == ADDR_EXPR
578 && DECL_P (TREE_OPERAND (TREE_OPERAND (var, 0), 0)))
579 TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (var, 0), 0)) = 1;
584 /* A subroutine of get_expr_operands to handle MEM_REF.
586 STMT is the statement being processed, EXPR is the MEM_REF
587 that got us here.
589 FLAGS is as in get_expr_operands. */
591 static void
592 get_indirect_ref_operands (gimple stmt, tree expr, int flags)
594 tree *pptr = &TREE_OPERAND (expr, 0);
596 if (!(flags & opf_no_vops)
597 && TREE_THIS_VOLATILE (expr))
598 gimple_set_has_volatile_ops (stmt, true);
600 /* Add the VOP. */
601 add_virtual_operand (stmt, flags);
603 /* If requested, add a USE operand for the base pointer. */
604 get_expr_operands (stmt, pptr,
605 opf_non_addressable | opf_use
606 | (flags & (opf_no_vops|opf_not_non_addressable)));
610 /* A subroutine of get_expr_operands to handle TARGET_MEM_REF. */
612 static void
613 get_tmr_operands (gimple stmt, tree expr, int flags)
615 if (!(flags & opf_no_vops)
616 && TREE_THIS_VOLATILE (expr))
617 gimple_set_has_volatile_ops (stmt, true);
619 /* First record the real operands. */
620 get_expr_operands (stmt, &TMR_BASE (expr), opf_use | (flags & opf_no_vops));
621 get_expr_operands (stmt, &TMR_INDEX (expr), opf_use | (flags & opf_no_vops));
622 get_expr_operands (stmt, &TMR_INDEX2 (expr), opf_use | (flags & opf_no_vops));
624 add_virtual_operand (stmt, flags);
628 /* If STMT is a call that may clobber globals and other symbols that
629 escape, add them to the VDEF/VUSE lists for it. */
631 static void
632 maybe_add_call_vops (gimple stmt)
634 int call_flags = gimple_call_flags (stmt);
636 /* If aliases have been computed already, add VDEF or VUSE
637 operands for all the symbols that have been found to be
638 call-clobbered. */
639 if (!(call_flags & ECF_NOVOPS))
641 /* A 'pure' or a 'const' function never call-clobbers anything.
642 A 'noreturn' function might, but since we don't return anyway
643 there is no point in recording that. */
644 if (!(call_flags & (ECF_PURE | ECF_CONST | ECF_NORETURN)))
645 add_virtual_operand (stmt, opf_def);
646 else if (!(call_flags & ECF_CONST))
647 add_virtual_operand (stmt, opf_use);
652 /* Scan operands in the ASM_EXPR stmt referred to in INFO. */
654 static void
655 get_asm_expr_operands (gimple stmt)
657 size_t i, noutputs;
658 const char **oconstraints;
659 const char *constraint;
660 bool allows_mem, allows_reg, is_inout;
662 noutputs = gimple_asm_noutputs (stmt);
663 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
665 /* Gather all output operands. */
666 for (i = 0; i < gimple_asm_noutputs (stmt); i++)
668 tree link = gimple_asm_output_op (stmt, i);
669 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
670 oconstraints[i] = constraint;
671 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
672 &allows_reg, &is_inout);
674 /* This should have been split in gimplify_asm_expr. */
675 gcc_assert (!allows_reg || !is_inout);
677 /* Memory operands are addressable. Note that STMT needs the
678 address of this operand. */
679 if (!allows_reg && allows_mem)
680 mark_address_taken (TREE_VALUE (link));
682 get_expr_operands (stmt, &TREE_VALUE (link), opf_def | opf_not_non_addressable);
685 /* Gather all input operands. */
686 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
688 tree link = gimple_asm_input_op (stmt, i);
689 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
690 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
691 &allows_mem, &allows_reg);
693 /* Memory operands are addressable. Note that STMT needs the
694 address of this operand. */
695 if (!allows_reg && allows_mem)
696 mark_address_taken (TREE_VALUE (link));
698 get_expr_operands (stmt, &TREE_VALUE (link), opf_not_non_addressable);
701 /* Clobber all memory and addressable symbols for asm ("" : : : "memory"); */
702 if (gimple_asm_clobbers_memory_p (stmt))
703 add_virtual_operand (stmt, opf_def);
707 /* Recursively scan the expression pointed to by EXPR_P in statement
708 STMT. FLAGS is one of the OPF_* constants modifying how to
709 interpret the operands found. */
711 static void
712 get_expr_operands (gimple stmt, tree *expr_p, int flags)
714 enum tree_code code;
715 enum tree_code_class codeclass;
716 tree expr = *expr_p;
717 int uflags = opf_use;
719 if (expr == NULL)
720 return;
722 if (is_gimple_debug (stmt))
723 uflags |= (flags & opf_no_vops);
725 code = TREE_CODE (expr);
726 codeclass = TREE_CODE_CLASS (code);
728 switch (code)
730 case ADDR_EXPR:
731 /* Taking the address of a variable does not represent a
732 reference to it, but the fact that the statement takes its
733 address will be of interest to some passes (e.g. alias
734 resolution). */
735 if ((!(flags & opf_non_addressable)
736 || (flags & opf_not_non_addressable))
737 && !is_gimple_debug (stmt))
738 mark_address_taken (TREE_OPERAND (expr, 0));
740 /* If the address is invariant, there may be no interesting
741 variable references inside. */
742 if (is_gimple_min_invariant (expr))
743 return;
745 /* Otherwise, there may be variables referenced inside but there
746 should be no VUSEs created, since the referenced objects are
747 not really accessed. The only operands that we should find
748 here are ARRAY_REF indices which will always be real operands
749 (GIMPLE does not allow non-registers as array indices). */
750 flags |= opf_no_vops;
751 get_expr_operands (stmt, &TREE_OPERAND (expr, 0),
752 flags | opf_not_non_addressable);
753 return;
755 case SSA_NAME:
756 case VAR_DECL:
757 case PARM_DECL:
758 case RESULT_DECL:
759 add_stmt_operand (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_indirect_ref_operands (stmt, expr, flags);
768 return;
770 case TARGET_MEM_REF:
771 get_tmr_operands (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 (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 (stmt, &TREE_OPERAND (expr, 2), uflags);
793 else if (code == ARRAY_REF || code == ARRAY_RANGE_REF)
795 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), uflags);
796 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), uflags);
797 get_expr_operands (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 (stmt, &TREE_OPERAND (expr, 1), uflags);
807 get_expr_operands (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 (stmt, &TREE_OPERAND (expr, 0), uflags);
814 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), uflags);
815 get_expr_operands (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 (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 (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 (stmt, &TREE_OPERAND (expr, 0), flags);
857 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
858 return;
861 case DOT_PROD_EXPR:
862 case REALIGN_LOAD_EXPR:
863 case WIDEN_MULT_PLUS_EXPR:
864 case WIDEN_MULT_MINUS_EXPR:
865 case FMA_EXPR:
867 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
868 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
869 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), flags);
870 return;
873 case FUNCTION_DECL:
874 case LABEL_DECL:
875 case CONST_DECL:
876 case CASE_LABEL_EXPR:
877 /* Expressions that make no memory references. */
878 return;
880 default:
881 if (codeclass == tcc_unary)
882 goto do_unary;
883 if (codeclass == tcc_binary || codeclass == tcc_comparison)
884 goto do_binary;
885 if (codeclass == tcc_constant || codeclass == tcc_type)
886 return;
889 /* If we get here, something has gone wrong. */
890 #ifdef ENABLE_CHECKING
891 fprintf (stderr, "unhandled expression in get_expr_operands():\n");
892 debug_tree (expr);
893 fputs ("\n", stderr);
894 #endif
895 gcc_unreachable ();
899 /* Parse STMT looking for operands. When finished, the various
900 build_* operand vectors will have potential operands in them. */
902 static void
903 parse_ssa_operands (gimple stmt)
905 enum gimple_code code = gimple_code (stmt);
906 size_t i, n, start = 0;
908 switch (code)
910 case GIMPLE_ASM:
911 get_asm_expr_operands (stmt);
912 break;
914 case GIMPLE_TRANSACTION:
915 /* The start of a transaction is a memory barrier. */
916 add_virtual_operand (stmt, opf_def | opf_use);
917 break;
919 case GIMPLE_DEBUG:
920 if (gimple_debug_bind_p (stmt)
921 && gimple_debug_bind_has_value_p (stmt))
922 get_expr_operands (stmt, gimple_debug_bind_get_value_ptr (stmt),
923 opf_use | opf_no_vops);
924 break;
926 case GIMPLE_RETURN:
927 append_vuse (gimple_vop (cfun));
928 goto do_default;
930 case GIMPLE_CALL:
931 /* Add call-clobbered operands, if needed. */
932 maybe_add_call_vops (stmt);
933 /* FALLTHRU */
935 case GIMPLE_ASSIGN:
936 get_expr_operands (stmt, gimple_op_ptr (stmt, 0), opf_def);
937 start = 1;
938 /* FALLTHRU */
940 default:
941 do_default:
942 n = gimple_num_ops (stmt);
943 for (i = start; i < n; i++)
944 get_expr_operands (stmt, gimple_op_ptr (stmt, i), opf_use);
945 break;
950 /* Create an operands cache for STMT. */
952 static void
953 build_ssa_operands (gimple stmt)
955 /* Initially assume that the statement has no volatile operands. */
956 gimple_set_has_volatile_ops (stmt, false);
958 start_ssa_stmt_operands ();
959 parse_ssa_operands (stmt);
960 finalize_ssa_stmt_operands (stmt);
963 /* Verifies SSA statement operands. */
965 DEBUG_FUNCTION bool
966 verify_ssa_operands (gimple stmt)
968 use_operand_p use_p;
969 def_operand_p def_p;
970 ssa_op_iter iter;
971 unsigned i;
972 tree use, def;
973 bool volatile_p = gimple_has_volatile_ops (stmt);
975 /* build_ssa_operands w/o finalizing them. */
976 gimple_set_has_volatile_ops (stmt, false);
977 start_ssa_stmt_operands ();
978 parse_ssa_operands (stmt);
980 /* Now verify the built operands are the same as present in STMT. */
981 def = gimple_vdef (stmt);
982 if (def
983 && TREE_CODE (def) == SSA_NAME)
984 def = SSA_NAME_VAR (def);
985 if (build_vdef != def)
987 error ("virtual definition of statement not up-to-date");
988 return true;
990 if (gimple_vdef (stmt)
991 && ((def_p = gimple_vdef_op (stmt)) == NULL_DEF_OPERAND_P
992 || DEF_FROM_PTR (def_p) != gimple_vdef (stmt)))
994 error ("virtual def operand missing for stmt");
995 return true;
998 use = gimple_vuse (stmt);
999 if (use
1000 && TREE_CODE (use) == SSA_NAME)
1001 use = SSA_NAME_VAR (use);
1002 if (build_vuse != use)
1004 error ("virtual use of statement not up-to-date");
1005 return true;
1007 if (gimple_vuse (stmt)
1008 && ((use_p = gimple_vuse_op (stmt)) == NULL_USE_OPERAND_P
1009 || USE_FROM_PTR (use_p) != gimple_vuse (stmt)))
1011 error ("virtual use operand missing for stmt");
1012 return true;
1015 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1017 FOR_EACH_VEC_ELT (build_uses, i, use)
1019 if (use_p->use == (tree *)use)
1021 build_uses[i] = NULL_TREE;
1022 break;
1025 if (i == build_uses.length ())
1027 error ("excess use operand for stmt");
1028 debug_generic_expr (USE_FROM_PTR (use_p));
1029 return true;
1032 FOR_EACH_VEC_ELT (build_uses, i, use)
1033 if (use != NULL_TREE)
1035 error ("use operand missing for stmt");
1036 debug_generic_expr (*(tree *)use);
1037 return true;
1040 if (gimple_has_volatile_ops (stmt) != volatile_p)
1042 error ("stmt volatile flag not up-to-date");
1043 return true;
1046 cleanup_build_arrays ();
1047 return false;
1051 /* Releases the operands of STMT back to their freelists, and clears
1052 the stmt operand lists. */
1054 void
1055 free_stmt_operands (gimple stmt)
1057 use_optype_p uses = gimple_use_ops (stmt), last_use;
1059 if (uses)
1061 for (last_use = uses; last_use->next; last_use = last_use->next)
1062 delink_imm_use (USE_OP_PTR (last_use));
1063 delink_imm_use (USE_OP_PTR (last_use));
1064 last_use->next = gimple_ssa_operands (cfun)->free_uses;
1065 gimple_ssa_operands (cfun)->free_uses = uses;
1066 gimple_set_use_ops (stmt, NULL);
1069 if (gimple_has_mem_ops (stmt))
1071 gimple_set_vuse (stmt, NULL_TREE);
1072 gimple_set_vdef (stmt, NULL_TREE);
1077 /* Get the operands of statement STMT. */
1079 void
1080 update_stmt_operands (gimple stmt)
1082 /* If update_stmt_operands is called before SSA is initialized, do
1083 nothing. */
1084 if (!ssa_operands_active (cfun))
1085 return;
1087 timevar_push (TV_TREE_OPS);
1089 /* If the stmt is a noreturn call queue it to be processed by
1090 split_bbs_on_noreturn_calls during cfg cleanup. */
1091 if (is_gimple_call (stmt)
1092 && gimple_call_noreturn_p (stmt))
1093 vec_safe_push (MODIFIED_NORETURN_CALLS (cfun), stmt);
1095 gcc_assert (gimple_modified_p (stmt));
1096 build_ssa_operands (stmt);
1097 gimple_set_modified (stmt, false);
1099 timevar_pop (TV_TREE_OPS);
1103 /* Swap operands EXP0 and EXP1 in statement STMT. No attempt is done
1104 to test the validity of the swap operation. */
1106 void
1107 swap_ssa_operands (gimple stmt, tree *exp0, tree *exp1)
1109 tree op0, op1;
1110 op0 = *exp0;
1111 op1 = *exp1;
1113 gcc_checking_assert (ssa_operands_active (cfun));
1115 if (op0 != op1)
1117 /* Attempt to preserve the relative positions of these two operands in
1118 their * respective immediate use lists by adjusting their use pointer
1119 to point to the new operand position. */
1120 use_optype_p use0, use1, ptr;
1121 use0 = use1 = NULL;
1123 /* Find the 2 operands in the cache, if they are there. */
1124 for (ptr = gimple_use_ops (stmt); ptr; ptr = ptr->next)
1125 if (USE_OP_PTR (ptr)->use == exp0)
1127 use0 = ptr;
1128 break;
1131 for (ptr = gimple_use_ops (stmt); ptr; ptr = ptr->next)
1132 if (USE_OP_PTR (ptr)->use == exp1)
1134 use1 = ptr;
1135 break;
1138 /* And adjust their location to point to the new position of the
1139 operand. */
1140 if (use0)
1141 USE_OP_PTR (use0)->use = exp1;
1142 if (use1)
1143 USE_OP_PTR (use1)->use = exp0;
1145 /* Now swap the data. */
1146 *exp0 = op1;
1147 *exp1 = op0;
1152 /* Scan the immediate_use list for VAR making sure its linked properly.
1153 Return TRUE if there is a problem and emit an error message to F. */
1155 DEBUG_FUNCTION bool
1156 verify_imm_links (FILE *f, tree var)
1158 use_operand_p ptr, prev, list;
1159 int count;
1161 gcc_assert (TREE_CODE (var) == SSA_NAME);
1163 list = &(SSA_NAME_IMM_USE_NODE (var));
1164 gcc_assert (list->use == NULL);
1166 if (list->prev == NULL)
1168 gcc_assert (list->next == NULL);
1169 return false;
1172 prev = list;
1173 count = 0;
1174 for (ptr = list->next; ptr != list; )
1176 if (prev != ptr->prev)
1177 goto error;
1179 if (ptr->use == NULL)
1180 goto error; /* 2 roots, or SAFE guard node. */
1181 else if (*(ptr->use) != var)
1182 goto error;
1184 prev = ptr;
1185 ptr = ptr->next;
1187 /* Avoid infinite loops. 50,000,000 uses probably indicates a
1188 problem. */
1189 if (count++ > 50000000)
1190 goto error;
1193 /* Verify list in the other direction. */
1194 prev = list;
1195 for (ptr = list->prev; ptr != list; )
1197 if (prev != ptr->next)
1198 goto error;
1199 prev = ptr;
1200 ptr = ptr->prev;
1201 if (count-- < 0)
1202 goto error;
1205 if (count != 0)
1206 goto error;
1208 return false;
1210 error:
1211 if (ptr->loc.stmt && gimple_modified_p (ptr->loc.stmt))
1213 fprintf (f, " STMT MODIFIED. - <%p> ", (void *)ptr->loc.stmt);
1214 print_gimple_stmt (f, ptr->loc.stmt, 0, TDF_SLIM);
1216 fprintf (f, " IMM ERROR : (use_p : tree - %p:%p)", (void *)ptr,
1217 (void *)ptr->use);
1218 print_generic_expr (f, USE_FROM_PTR (ptr), TDF_SLIM);
1219 fprintf (f, "\n");
1220 return true;
1224 /* Dump all the immediate uses to FILE. */
1226 void
1227 dump_immediate_uses_for (FILE *file, tree var)
1229 imm_use_iterator iter;
1230 use_operand_p use_p;
1232 gcc_assert (var && TREE_CODE (var) == SSA_NAME);
1234 print_generic_expr (file, var, TDF_SLIM);
1235 fprintf (file, " : -->");
1236 if (has_zero_uses (var))
1237 fprintf (file, " no uses.\n");
1238 else
1239 if (has_single_use (var))
1240 fprintf (file, " single use.\n");
1241 else
1242 fprintf (file, "%d uses.\n", num_imm_uses (var));
1244 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
1246 if (use_p->loc.stmt == NULL && use_p->use == NULL)
1247 fprintf (file, "***end of stmt iterator marker***\n");
1248 else
1249 if (!is_gimple_reg (USE_FROM_PTR (use_p)))
1250 print_gimple_stmt (file, USE_STMT (use_p), 0, TDF_VOPS|TDF_MEMSYMS);
1251 else
1252 print_gimple_stmt (file, USE_STMT (use_p), 0, TDF_SLIM);
1254 fprintf (file, "\n");
1258 /* Dump all the immediate uses to FILE. */
1260 void
1261 dump_immediate_uses (FILE *file)
1263 tree var;
1264 unsigned int x;
1266 fprintf (file, "Immediate_uses: \n\n");
1267 for (x = 1; x < num_ssa_names; x++)
1269 var = ssa_name (x);
1270 if (!var)
1271 continue;
1272 dump_immediate_uses_for (file, var);
1277 /* Dump def-use edges on stderr. */
1279 DEBUG_FUNCTION void
1280 debug_immediate_uses (void)
1282 dump_immediate_uses (stderr);
1286 /* Dump def-use edges on stderr. */
1288 DEBUG_FUNCTION void
1289 debug_immediate_uses_for (tree var)
1291 dump_immediate_uses_for (stderr, var);
1295 /* Unlink STMTs virtual definition from the IL by propagating its use. */
1297 void
1298 unlink_stmt_vdef (gimple stmt)
1300 use_operand_p use_p;
1301 imm_use_iterator iter;
1302 gimple use_stmt;
1303 tree vdef = gimple_vdef (stmt);
1304 tree vuse = gimple_vuse (stmt);
1306 if (!vdef
1307 || TREE_CODE (vdef) != SSA_NAME)
1308 return;
1310 FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
1312 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1313 SET_USE (use_p, vuse);
1316 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vdef))
1317 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vuse) = 1;
1321 /* Return true if the var whose chain of uses starts at PTR has no
1322 nondebug uses. */
1323 bool
1324 has_zero_uses_1 (const ssa_use_operand_t *head)
1326 const ssa_use_operand_t *ptr;
1328 for (ptr = head->next; ptr != head; ptr = ptr->next)
1329 if (!is_gimple_debug (USE_STMT (ptr)))
1330 return false;
1332 return true;
1336 /* Return true if the var whose chain of uses starts at PTR has a
1337 single nondebug use. Set USE_P and STMT to that single nondebug
1338 use, if so, or to NULL otherwise. */
1339 bool
1340 single_imm_use_1 (const ssa_use_operand_t *head,
1341 use_operand_p *use_p, gimple *stmt)
1343 ssa_use_operand_t *ptr, *single_use = 0;
1345 for (ptr = head->next; ptr != head; ptr = ptr->next)
1346 if (!is_gimple_debug (USE_STMT (ptr)))
1348 if (single_use)
1350 single_use = NULL;
1351 break;
1353 single_use = ptr;
1356 if (use_p)
1357 *use_p = single_use;
1359 if (stmt)
1360 *stmt = single_use ? single_use->loc.stmt : NULL;
1362 return single_use;