2015-10-18 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / tree-ssa-operands.c
blob544e9dfe080f65dc9968c1ae9be130a4c708ac24
1 /* SSA operands management for trees.
2 Copyright (C) 2003-2015 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 "backend.h"
24 #include "tree.h"
25 #include "gimple.h"
26 #include "hard-reg-set.h"
27 #include "ssa.h"
28 #include "alias.h"
29 #include "fold-const.h"
30 #include "stmt.h"
31 #include "print-tree.h"
32 #include "flags.h"
33 #include "gimple-pretty-print.h"
34 #include "internal-fn.h"
35 #include "tree-inline.h"
36 #include "timevar.h"
37 #include "dumpfile.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 in a place where address-taken does not imply addressable. */
102 #define opf_non_addressable (1 << 3)
104 /* Operand is in a place where opf_non_addressable does not apply. */
105 #define opf_not_non_addressable (1 << 4)
107 /* Operand is having its address taken. */
108 #define opf_address_taken (1 << 5)
110 /* Array for building all the use operands. */
111 static vec<tree *> build_uses;
113 /* The built VDEF operand. */
114 static tree build_vdef;
116 /* The built VUSE operand. */
117 static tree build_vuse;
119 /* Bitmap obstack for our datastructures that needs to survive across
120 compilations of multiple functions. */
121 static bitmap_obstack operands_bitmap_obstack;
123 static void get_expr_operands (struct function *, gimple *, tree *, int);
125 /* Number of functions with initialized ssa_operands. */
126 static int n_initialized = 0;
128 /* Accessor to tree-ssa-operands.c caches. */
129 static inline struct ssa_operands *
130 gimple_ssa_operands (const struct function *fun)
132 return &fun->gimple_df->ssa_operands;
136 /* Return true if the SSA operands cache is active. */
138 bool
139 ssa_operands_active (struct function *fun)
141 if (fun == NULL)
142 return false;
144 return fun->gimple_df && gimple_ssa_operands (fun)->ops_active;
148 /* Create the VOP variable, an artificial global variable to act as a
149 representative of all of the virtual operands FUD chain. */
151 static void
152 create_vop_var (struct function *fn)
154 tree global_var;
156 gcc_assert (fn->gimple_df->vop == NULL_TREE);
158 global_var = build_decl (BUILTINS_LOCATION, VAR_DECL,
159 get_identifier (".MEM"),
160 void_type_node);
161 DECL_ARTIFICIAL (global_var) = 1;
162 DECL_IGNORED_P (global_var) = 1;
163 TREE_READONLY (global_var) = 0;
164 DECL_EXTERNAL (global_var) = 1;
165 TREE_STATIC (global_var) = 1;
166 TREE_USED (global_var) = 1;
167 DECL_CONTEXT (global_var) = NULL_TREE;
168 TREE_THIS_VOLATILE (global_var) = 0;
169 TREE_ADDRESSABLE (global_var) = 0;
170 VAR_DECL_IS_VIRTUAL_OPERAND (global_var) = 1;
172 fn->gimple_df->vop = global_var;
175 /* These are the sizes of the operand memory buffer in bytes which gets
176 allocated each time more operands space is required. The final value is
177 the amount that is allocated every time after that.
178 In 1k we can fit 25 use operands (or 63 def operands) on a host with
179 8 byte pointers, that would be 10 statements each with 1 def and 2
180 uses. */
182 #define OP_SIZE_INIT 0
183 #define OP_SIZE_1 (1024 - sizeof (void *))
184 #define OP_SIZE_2 (1024 * 4 - sizeof (void *))
185 #define OP_SIZE_3 (1024 * 16 - sizeof (void *))
187 /* Initialize the operand cache routines. */
189 void
190 init_ssa_operands (struct function *fn)
192 if (!n_initialized++)
194 build_uses.create (10);
195 build_vuse = NULL_TREE;
196 build_vdef = NULL_TREE;
197 bitmap_obstack_initialize (&operands_bitmap_obstack);
200 gcc_assert (gimple_ssa_operands (fn)->operand_memory == NULL);
201 gimple_ssa_operands (fn)->operand_memory_index
202 = gimple_ssa_operands (fn)->ssa_operand_mem_size;
203 gimple_ssa_operands (fn)->ops_active = true;
204 gimple_ssa_operands (fn)->ssa_operand_mem_size = OP_SIZE_INIT;
205 create_vop_var (fn);
209 /* Dispose of anything required by the operand routines. */
211 void
212 fini_ssa_operands (struct function *fn)
214 struct ssa_operand_memory_d *ptr;
216 if (!--n_initialized)
218 build_uses.release ();
219 build_vdef = NULL_TREE;
220 build_vuse = NULL_TREE;
223 gimple_ssa_operands (fn)->free_uses = NULL;
225 while ((ptr = gimple_ssa_operands (fn)->operand_memory) != NULL)
227 gimple_ssa_operands (fn)->operand_memory
228 = gimple_ssa_operands (fn)->operand_memory->next;
229 ggc_free (ptr);
232 gimple_ssa_operands (fn)->ops_active = false;
234 if (!n_initialized)
235 bitmap_obstack_release (&operands_bitmap_obstack);
237 fn->gimple_df->vop = NULL_TREE;
241 /* Return memory for an operand of size SIZE. */
243 static inline void *
244 ssa_operand_alloc (struct function *fn, unsigned size)
246 char *ptr;
248 gcc_assert (size == sizeof (struct use_optype_d));
250 if (gimple_ssa_operands (fn)->operand_memory_index + size
251 >= gimple_ssa_operands (fn)->ssa_operand_mem_size)
253 struct ssa_operand_memory_d *ptr;
255 switch (gimple_ssa_operands (fn)->ssa_operand_mem_size)
257 case OP_SIZE_INIT:
258 gimple_ssa_operands (fn)->ssa_operand_mem_size = OP_SIZE_1;
259 break;
260 case OP_SIZE_1:
261 gimple_ssa_operands (fn)->ssa_operand_mem_size = OP_SIZE_2;
262 break;
263 case OP_SIZE_2:
264 case OP_SIZE_3:
265 gimple_ssa_operands (fn)->ssa_operand_mem_size = OP_SIZE_3;
266 break;
267 default:
268 gcc_unreachable ();
272 ptr = (ssa_operand_memory_d *) ggc_internal_alloc
273 (sizeof (void *) + gimple_ssa_operands (fn)->ssa_operand_mem_size);
275 ptr->next = gimple_ssa_operands (fn)->operand_memory;
276 gimple_ssa_operands (fn)->operand_memory = ptr;
277 gimple_ssa_operands (fn)->operand_memory_index = 0;
280 ptr = &(gimple_ssa_operands (fn)->operand_memory
281 ->mem[gimple_ssa_operands (fn)->operand_memory_index]);
282 gimple_ssa_operands (fn)->operand_memory_index += size;
283 return ptr;
287 /* Allocate a USE operand. */
289 static inline struct use_optype_d *
290 alloc_use (struct function *fn)
292 struct use_optype_d *ret;
293 if (gimple_ssa_operands (fn)->free_uses)
295 ret = gimple_ssa_operands (fn)->free_uses;
296 gimple_ssa_operands (fn)->free_uses
297 = gimple_ssa_operands (fn)->free_uses->next;
299 else
300 ret = (struct use_optype_d *)
301 ssa_operand_alloc (fn, sizeof (struct use_optype_d));
302 return ret;
306 /* Adds OP to the list of uses of statement STMT after LAST. */
308 static inline use_optype_p
309 add_use_op (struct function *fn, gimple *stmt, tree *op, use_optype_p last)
311 use_optype_p new_use;
313 new_use = alloc_use (fn);
314 USE_OP_PTR (new_use)->use = op;
315 link_imm_use_stmt (USE_OP_PTR (new_use), *op, stmt);
316 last->next = new_use;
317 new_use->next = NULL;
318 return new_use;
323 /* Takes elements from build_defs and turns them into def operands of STMT.
324 TODO -- Make build_defs vec of tree *. */
326 static inline void
327 finalize_ssa_defs (struct function *fn, gimple *stmt)
329 /* Pre-pend the vdef we may have built. */
330 if (build_vdef != NULL_TREE)
332 tree oldvdef = gimple_vdef (stmt);
333 if (oldvdef
334 && TREE_CODE (oldvdef) == SSA_NAME)
335 oldvdef = SSA_NAME_VAR (oldvdef);
336 if (oldvdef != build_vdef)
337 gimple_set_vdef (stmt, build_vdef);
340 /* Clear and unlink a no longer necessary VDEF. */
341 if (build_vdef == NULL_TREE
342 && gimple_vdef (stmt) != NULL_TREE)
344 if (TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
346 unlink_stmt_vdef (stmt);
347 release_ssa_name_fn (fn, gimple_vdef (stmt));
349 gimple_set_vdef (stmt, NULL_TREE);
352 /* If we have a non-SSA_NAME VDEF, mark it for renaming. */
353 if (gimple_vdef (stmt)
354 && TREE_CODE (gimple_vdef (stmt)) != SSA_NAME)
356 fn->gimple_df->rename_vops = 1;
357 fn->gimple_df->ssa_renaming_needed = 1;
362 /* Takes elements from build_uses and turns them into use operands of STMT. */
364 static inline void
365 finalize_ssa_uses (struct function *fn, gimple *stmt)
367 unsigned new_i;
368 struct use_optype_d new_list;
369 use_optype_p old_ops, ptr, last;
371 /* Pre-pend the VUSE we may have built. */
372 if (build_vuse != NULL_TREE)
374 tree oldvuse = gimple_vuse (stmt);
375 if (oldvuse
376 && TREE_CODE (oldvuse) == SSA_NAME)
377 oldvuse = SSA_NAME_VAR (oldvuse);
378 if (oldvuse != (build_vuse != NULL_TREE
379 ? build_vuse : build_vdef))
380 gimple_set_vuse (stmt, NULL_TREE);
381 build_uses.safe_insert (0, gimple_vuse_ptr (stmt));
384 new_list.next = NULL;
385 last = &new_list;
387 old_ops = gimple_use_ops (stmt);
389 /* Clear a no longer necessary VUSE. */
390 if (build_vuse == NULL_TREE
391 && gimple_vuse (stmt) != NULL_TREE)
392 gimple_set_vuse (stmt, NULL_TREE);
394 /* If there is anything in the old list, free it. */
395 if (old_ops)
397 for (ptr = old_ops; ptr->next; ptr = ptr->next)
398 delink_imm_use (USE_OP_PTR (ptr));
399 delink_imm_use (USE_OP_PTR (ptr));
400 ptr->next = gimple_ssa_operands (fn)->free_uses;
401 gimple_ssa_operands (fn)->free_uses = old_ops;
404 /* If we added a VUSE, make sure to set the operand if it is not already
405 present and mark it for renaming. */
406 if (build_vuse != NULL_TREE
407 && gimple_vuse (stmt) == NULL_TREE)
409 gimple_set_vuse (stmt, gimple_vop (fn));
410 fn->gimple_df->rename_vops = 1;
411 fn->gimple_df->ssa_renaming_needed = 1;
414 /* Now create nodes for all the new nodes. */
415 for (new_i = 0; new_i < build_uses.length (); new_i++)
417 tree *op = build_uses[new_i];
418 last = add_use_op (fn, stmt, op, last);
421 /* Now set the stmt's operands. */
422 gimple_set_use_ops (stmt, new_list.next);
426 /* Clear the in_list bits and empty the build array for VDEFs and
427 VUSEs. */
429 static inline void
430 cleanup_build_arrays (void)
432 build_vdef = NULL_TREE;
433 build_vuse = NULL_TREE;
434 build_uses.truncate (0);
438 /* Finalize all the build vectors, fill the new ones into INFO. */
440 static inline void
441 finalize_ssa_stmt_operands (struct function *fn, gimple *stmt)
443 finalize_ssa_defs (fn, stmt);
444 finalize_ssa_uses (fn, stmt);
445 cleanup_build_arrays ();
449 /* Start the process of building up operands vectors in INFO. */
451 static inline void
452 start_ssa_stmt_operands (void)
454 gcc_assert (build_uses.length () == 0);
455 gcc_assert (build_vuse == NULL_TREE);
456 gcc_assert (build_vdef == NULL_TREE);
460 /* Add USE_P to the list of pointers to operands. */
462 static inline void
463 append_use (tree *use_p)
465 build_uses.safe_push (use_p);
469 /* Add VAR to the set of variables that require a VDEF operator. */
471 static inline void
472 append_vdef (tree var)
474 gcc_assert ((build_vdef == NULL_TREE
475 || build_vdef == var)
476 && (build_vuse == NULL_TREE
477 || build_vuse == var));
479 build_vdef = var;
480 build_vuse = var;
484 /* Add VAR to the set of variables that require a VUSE operator. */
486 static inline void
487 append_vuse (tree var)
489 gcc_assert (build_vuse == NULL_TREE
490 || build_vuse == var);
492 build_vuse = var;
495 /* Add virtual operands for STMT. FLAGS is as in get_expr_operands. */
497 static void
498 add_virtual_operand (struct function *fn,
499 gimple *stmt ATTRIBUTE_UNUSED, int flags)
501 /* Add virtual operands to the stmt, unless the caller has specifically
502 requested not to do that (used when adding operands inside an
503 ADDR_EXPR expression). */
504 if (flags & opf_no_vops)
505 return;
507 gcc_assert (!is_gimple_debug (stmt));
509 if (flags & opf_def)
510 append_vdef (gimple_vop (fn));
511 else
512 append_vuse (gimple_vop (fn));
516 /* Add *VAR_P to the appropriate operand array for statement STMT.
517 FLAGS is as in get_expr_operands. If *VAR_P is a GIMPLE register,
518 it will be added to the statement's real operands, otherwise it is
519 added to virtual operands. */
521 static void
522 add_stmt_operand (struct function *fn, tree *var_p, gimple *stmt, int flags)
524 tree var = *var_p;
526 gcc_assert (SSA_VAR_P (*var_p));
528 if (is_gimple_reg (var))
530 /* The variable is a GIMPLE register. Add it to real operands. */
531 if (flags & opf_def)
533 else
534 append_use (var_p);
535 if (DECL_P (*var_p))
536 fn->gimple_df->ssa_renaming_needed = 1;
538 else
540 /* Mark statements with volatile operands. */
541 if (!(flags & opf_no_vops)
542 && TREE_THIS_VOLATILE (var))
543 gimple_set_has_volatile_ops (stmt, true);
545 /* The variable is a memory access. Add virtual operands. */
546 add_virtual_operand (fn, stmt, flags);
550 /* Mark the base address of REF as having its address taken.
551 REF may be a single variable whose address has been taken or any
552 other valid GIMPLE memory reference (structure reference, array,
553 etc). */
555 static void
556 mark_address_taken (tree ref)
558 tree var;
560 /* Note that it is *NOT OKAY* to use the target of a COMPONENT_REF
561 as the only thing we take the address of. If VAR is a structure,
562 taking the address of a field means that the whole structure may
563 be referenced using pointer arithmetic. See PR 21407 and the
564 ensuing mailing list discussion. */
565 var = get_base_address (ref);
566 if (var)
568 if (DECL_P (var))
569 TREE_ADDRESSABLE (var) = 1;
570 else if (TREE_CODE (var) == MEM_REF
571 && TREE_CODE (TREE_OPERAND (var, 0)) == ADDR_EXPR
572 && DECL_P (TREE_OPERAND (TREE_OPERAND (var, 0), 0)))
573 TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (var, 0), 0)) = 1;
578 /* A subroutine of get_expr_operands to handle MEM_REF.
580 STMT is the statement being processed, EXPR is the MEM_REF
581 that got us here.
583 FLAGS is as in get_expr_operands. */
585 static void
586 get_mem_ref_operands (struct function *fn,
587 gimple *stmt, tree expr, int flags)
589 tree *pptr = &TREE_OPERAND (expr, 0);
591 if (!(flags & opf_no_vops)
592 && TREE_THIS_VOLATILE (expr))
593 gimple_set_has_volatile_ops (stmt, true);
595 /* Add the VOP. */
596 add_virtual_operand (fn, stmt, flags);
598 /* If requested, add a USE operand for the base pointer. */
599 get_expr_operands (fn, stmt, pptr,
600 opf_non_addressable | opf_use
601 | (flags & (opf_no_vops|opf_not_non_addressable)));
605 /* A subroutine of get_expr_operands to handle TARGET_MEM_REF. */
607 static void
608 get_tmr_operands (struct function *fn, gimple *stmt, tree expr, int flags)
610 if (!(flags & opf_no_vops)
611 && TREE_THIS_VOLATILE (expr))
612 gimple_set_has_volatile_ops (stmt, true);
614 /* First record the real operands. */
615 get_expr_operands (fn, stmt,
616 &TMR_BASE (expr), opf_use | (flags & opf_no_vops));
617 get_expr_operands (fn, stmt,
618 &TMR_INDEX (expr), opf_use | (flags & opf_no_vops));
619 get_expr_operands (fn, stmt,
620 &TMR_INDEX2 (expr), opf_use | (flags & opf_no_vops));
622 add_virtual_operand (fn, stmt, flags);
626 /* If STMT is a call that may clobber globals and other symbols that
627 escape, add them to the VDEF/VUSE lists for it. */
629 static void
630 maybe_add_call_vops (struct function *fn, gcall *stmt)
632 int call_flags = gimple_call_flags (stmt);
634 /* If aliases have been computed already, add VDEF or VUSE
635 operands for all the symbols that have been found to be
636 call-clobbered. */
637 if (!(call_flags & ECF_NOVOPS))
639 /* A 'pure' or a 'const' function never call-clobbers anything. */
640 if (!(call_flags & (ECF_PURE | ECF_CONST)))
641 add_virtual_operand (fn, stmt, opf_def);
642 else if (!(call_flags & ECF_CONST))
643 add_virtual_operand (fn, stmt, opf_use);
648 /* Scan operands in the ASM_EXPR stmt referred to in INFO. */
650 static void
651 get_asm_stmt_operands (struct function *fn, gasm *stmt)
653 size_t i, noutputs;
654 const char **oconstraints;
655 const char *constraint;
656 bool allows_mem, allows_reg, is_inout;
658 noutputs = gimple_asm_noutputs (stmt);
659 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
661 /* Gather all output operands. */
662 for (i = 0; i < gimple_asm_noutputs (stmt); i++)
664 tree link = gimple_asm_output_op (stmt, i);
665 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
666 oconstraints[i] = constraint;
667 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
668 &allows_reg, &is_inout);
670 /* This should have been split in gimplify_asm_expr. */
671 gcc_assert (!allows_reg || !is_inout);
673 /* Memory operands are addressable. Note that STMT needs the
674 address of this operand. */
675 if (!allows_reg && allows_mem)
676 mark_address_taken (TREE_VALUE (link));
678 get_expr_operands (fn, stmt,
679 &TREE_VALUE (link), opf_def | opf_not_non_addressable);
682 /* Gather all input operands. */
683 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
685 tree link = gimple_asm_input_op (stmt, i);
686 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
687 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
688 &allows_mem, &allows_reg);
690 /* Memory operands are addressable. Note that STMT needs the
691 address of this operand. */
692 if (!allows_reg && allows_mem)
693 mark_address_taken (TREE_VALUE (link));
695 get_expr_operands (fn, stmt, &TREE_VALUE (link), opf_not_non_addressable);
698 /* Clobber all memory and addressable symbols for asm ("" : : : "memory"); */
699 if (gimple_asm_clobbers_memory_p (stmt))
700 add_virtual_operand (fn, stmt, opf_def);
704 /* Recursively scan the expression pointed to by EXPR_P in statement
705 STMT. FLAGS is one of the OPF_* constants modifying how to
706 interpret the operands found. */
708 static void
709 get_expr_operands (struct function *fn, gimple *stmt, tree *expr_p, int flags)
711 enum tree_code code;
712 enum tree_code_class codeclass;
713 tree expr = *expr_p;
714 int uflags = opf_use;
716 if (expr == NULL)
717 return;
719 if (is_gimple_debug (stmt))
720 uflags |= (flags & opf_no_vops);
722 code = TREE_CODE (expr);
723 codeclass = TREE_CODE_CLASS (code);
725 switch (code)
727 case ADDR_EXPR:
728 /* Taking the address of a variable does not represent a
729 reference to it, but the fact that the statement takes its
730 address will be of interest to some passes (e.g. alias
731 resolution). */
732 if ((!(flags & opf_non_addressable)
733 || (flags & opf_not_non_addressable))
734 && !is_gimple_debug (stmt))
735 mark_address_taken (TREE_OPERAND (expr, 0));
737 /* Otherwise, there may be variables referenced inside but there
738 should be no VUSEs created, since the referenced objects are
739 not really accessed. The only operands that we should find
740 here are ARRAY_REF indices which will always be real operands
741 (GIMPLE does not allow non-registers as array indices). */
742 flags |= opf_no_vops;
743 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 0),
744 flags | opf_not_non_addressable | opf_address_taken);
745 return;
747 case SSA_NAME:
748 case VAR_DECL:
749 case PARM_DECL:
750 case RESULT_DECL:
751 if (!(flags & opf_address_taken))
752 add_stmt_operand (fn, expr_p, stmt, flags);
753 return;
755 case DEBUG_EXPR_DECL:
756 gcc_assert (gimple_debug_bind_p (stmt));
757 return;
759 case MEM_REF:
760 get_mem_ref_operands (fn, stmt, expr, flags);
761 return;
763 case TARGET_MEM_REF:
764 get_tmr_operands (fn, stmt, expr, flags);
765 return;
767 case ARRAY_REF:
768 case ARRAY_RANGE_REF:
769 case COMPONENT_REF:
770 case REALPART_EXPR:
771 case IMAGPART_EXPR:
773 if (!(flags & opf_no_vops)
774 && TREE_THIS_VOLATILE (expr))
775 gimple_set_has_volatile_ops (stmt, true);
777 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 0), flags);
779 if (code == COMPONENT_REF)
781 if (!(flags & opf_no_vops)
782 && TREE_THIS_VOLATILE (TREE_OPERAND (expr, 1)))
783 gimple_set_has_volatile_ops (stmt, true);
784 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 2), uflags);
786 else if (code == ARRAY_REF || code == ARRAY_RANGE_REF)
788 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 1), uflags);
789 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 2), uflags);
790 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 3), uflags);
793 return;
796 case WITH_SIZE_EXPR:
797 /* WITH_SIZE_EXPR is a pass-through reference to its first argument,
798 and an rvalue reference to its second argument. */
799 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 1), uflags);
800 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 0), flags);
801 return;
803 case COND_EXPR:
804 case VEC_COND_EXPR:
805 case VEC_PERM_EXPR:
806 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 0), uflags);
807 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 1), uflags);
808 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 2), uflags);
809 return;
811 case CONSTRUCTOR:
813 /* General aggregate CONSTRUCTORs have been decomposed, but they
814 are still in use as the COMPLEX_EXPR equivalent for vectors. */
815 constructor_elt *ce;
816 unsigned HOST_WIDE_INT idx;
818 /* A volatile constructor is actually TREE_CLOBBER_P, transfer
819 the volatility to the statement, don't use TREE_CLOBBER_P for
820 mirroring the other uses of THIS_VOLATILE in this file. */
821 if (!(flags & opf_no_vops)
822 && TREE_THIS_VOLATILE (expr))
823 gimple_set_has_volatile_ops (stmt, true);
825 for (idx = 0;
826 vec_safe_iterate (CONSTRUCTOR_ELTS (expr), idx, &ce);
827 idx++)
828 get_expr_operands (fn, stmt, &ce->value, uflags);
830 return;
833 case BIT_FIELD_REF:
834 if (!(flags & opf_no_vops)
835 && TREE_THIS_VOLATILE (expr))
836 gimple_set_has_volatile_ops (stmt, true);
837 /* FALLTHRU */
839 case VIEW_CONVERT_EXPR:
840 do_unary:
841 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 0), flags);
842 return;
844 case COMPOUND_EXPR:
845 case OBJ_TYPE_REF:
846 case ASSERT_EXPR:
847 do_binary:
849 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 0), flags);
850 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 1), flags);
851 return;
854 case DOT_PROD_EXPR:
855 case SAD_EXPR:
856 case REALIGN_LOAD_EXPR:
857 case WIDEN_MULT_PLUS_EXPR:
858 case WIDEN_MULT_MINUS_EXPR:
859 case FMA_EXPR:
861 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 0), flags);
862 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 1), flags);
863 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 2), flags);
864 return;
867 case FUNCTION_DECL:
868 case LABEL_DECL:
869 case CONST_DECL:
870 case CASE_LABEL_EXPR:
871 /* Expressions that make no memory references. */
872 return;
874 default:
875 if (codeclass == tcc_unary)
876 goto do_unary;
877 if (codeclass == tcc_binary || codeclass == tcc_comparison)
878 goto do_binary;
879 if (codeclass == tcc_constant || codeclass == tcc_type)
880 return;
883 /* If we get here, something has gone wrong. */
884 #ifdef ENABLE_CHECKING
885 fprintf (stderr, "unhandled expression in get_expr_operands():\n");
886 debug_tree (expr);
887 fputs ("\n", stderr);
888 #endif
889 gcc_unreachable ();
893 /* Parse STMT looking for operands. When finished, the various
894 build_* operand vectors will have potential operands in them. */
896 static void
897 parse_ssa_operands (struct function *fn, gimple *stmt)
899 enum gimple_code code = gimple_code (stmt);
900 size_t i, n, start = 0;
902 switch (code)
904 case GIMPLE_ASM:
905 get_asm_stmt_operands (fn, as_a <gasm *> (stmt));
906 break;
908 case GIMPLE_TRANSACTION:
909 /* The start of a transaction is a memory barrier. */
910 add_virtual_operand (fn, stmt, opf_def | opf_use);
911 break;
913 case GIMPLE_DEBUG:
914 if (gimple_debug_bind_p (stmt)
915 && gimple_debug_bind_has_value_p (stmt))
916 get_expr_operands (fn, stmt, gimple_debug_bind_get_value_ptr (stmt),
917 opf_use | opf_no_vops);
918 break;
920 case GIMPLE_RETURN:
921 append_vuse (gimple_vop (fn));
922 goto do_default;
924 case GIMPLE_CALL:
925 /* Add call-clobbered operands, if needed. */
926 maybe_add_call_vops (fn, as_a <gcall *> (stmt));
927 /* FALLTHRU */
929 case GIMPLE_ASSIGN:
930 get_expr_operands (fn, stmt, gimple_op_ptr (stmt, 0), opf_def);
931 start = 1;
932 /* FALLTHRU */
934 default:
935 do_default:
936 n = gimple_num_ops (stmt);
937 for (i = start; i < n; i++)
938 get_expr_operands (fn, stmt, gimple_op_ptr (stmt, i), opf_use);
939 break;
944 /* Create an operands cache for STMT. */
946 static void
947 build_ssa_operands (struct function *fn, gimple *stmt)
949 /* Initially assume that the statement has no volatile operands. */
950 gimple_set_has_volatile_ops (stmt, false);
952 start_ssa_stmt_operands ();
953 parse_ssa_operands (fn, stmt);
954 finalize_ssa_stmt_operands (fn, stmt);
957 /* Verifies SSA statement operands. */
959 DEBUG_FUNCTION bool
960 verify_ssa_operands (struct function *fn, gimple *stmt)
962 use_operand_p use_p;
963 def_operand_p def_p;
964 ssa_op_iter iter;
965 unsigned i;
966 tree def;
967 bool volatile_p = gimple_has_volatile_ops (stmt);
969 /* build_ssa_operands w/o finalizing them. */
970 gimple_set_has_volatile_ops (stmt, false);
971 start_ssa_stmt_operands ();
972 parse_ssa_operands (fn, stmt);
974 /* Now verify the built operands are the same as present in STMT. */
975 def = gimple_vdef (stmt);
976 if (def
977 && TREE_CODE (def) == SSA_NAME)
978 def = SSA_NAME_VAR (def);
979 if (build_vdef != def)
981 error ("virtual definition of statement not up-to-date");
982 return true;
984 if (gimple_vdef (stmt)
985 && ((def_p = gimple_vdef_op (stmt)) == NULL_DEF_OPERAND_P
986 || DEF_FROM_PTR (def_p) != gimple_vdef (stmt)))
988 error ("virtual def operand missing for stmt");
989 return true;
992 tree use = gimple_vuse (stmt);
993 if (use
994 && TREE_CODE (use) == SSA_NAME)
995 use = SSA_NAME_VAR (use);
996 if (build_vuse != use)
998 error ("virtual use of statement not up-to-date");
999 return true;
1001 if (gimple_vuse (stmt)
1002 && ((use_p = gimple_vuse_op (stmt)) == NULL_USE_OPERAND_P
1003 || USE_FROM_PTR (use_p) != gimple_vuse (stmt)))
1005 error ("virtual use operand missing for stmt");
1006 return true;
1009 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1011 tree *op;
1012 FOR_EACH_VEC_ELT (build_uses, i, op)
1014 if (use_p->use == op)
1016 build_uses[i] = NULL;
1017 break;
1020 if (i == build_uses.length ())
1022 error ("excess use operand for stmt");
1023 debug_generic_expr (USE_FROM_PTR (use_p));
1024 return true;
1028 tree *op;
1029 FOR_EACH_VEC_ELT (build_uses, i, op)
1030 if (op != NULL)
1032 error ("use operand missing for stmt");
1033 debug_generic_expr (*op);
1034 return true;
1037 if (gimple_has_volatile_ops (stmt) != volatile_p)
1039 error ("stmt volatile flag not up-to-date");
1040 return true;
1043 cleanup_build_arrays ();
1044 return false;
1048 /* Releases the operands of STMT back to their freelists, and clears
1049 the stmt operand lists. */
1051 void
1052 free_stmt_operands (struct function *fn, gimple *stmt)
1054 use_optype_p uses = gimple_use_ops (stmt), last_use;
1056 if (uses)
1058 for (last_use = uses; last_use->next; last_use = last_use->next)
1059 delink_imm_use (USE_OP_PTR (last_use));
1060 delink_imm_use (USE_OP_PTR (last_use));
1061 last_use->next = gimple_ssa_operands (fn)->free_uses;
1062 gimple_ssa_operands (fn)->free_uses = uses;
1063 gimple_set_use_ops (stmt, NULL);
1066 if (gimple_has_mem_ops (stmt))
1068 gimple_set_vuse (stmt, NULL_TREE);
1069 gimple_set_vdef (stmt, NULL_TREE);
1074 /* Get the operands of statement STMT. */
1076 void
1077 update_stmt_operands (struct function *fn, gimple *stmt)
1079 /* If update_stmt_operands is called before SSA is initialized, do
1080 nothing. */
1081 if (!ssa_operands_active (fn))
1082 return;
1084 timevar_push (TV_TREE_OPS);
1086 gcc_assert (gimple_modified_p (stmt));
1087 build_ssa_operands (fn, stmt);
1088 gimple_set_modified (stmt, false);
1090 timevar_pop (TV_TREE_OPS);
1094 /* Swap operands EXP0 and EXP1 in statement STMT. No attempt is done
1095 to test the validity of the swap operation. */
1097 void
1098 swap_ssa_operands (gimple *stmt, tree *exp0, tree *exp1)
1100 tree op0, op1;
1101 op0 = *exp0;
1102 op1 = *exp1;
1104 if (op0 != op1)
1106 /* Attempt to preserve the relative positions of these two operands in
1107 their * respective immediate use lists by adjusting their use pointer
1108 to point to the new operand position. */
1109 use_optype_p use0, use1, ptr;
1110 use0 = use1 = NULL;
1112 /* Find the 2 operands in the cache, if they are there. */
1113 for (ptr = gimple_use_ops (stmt); ptr; ptr = ptr->next)
1114 if (USE_OP_PTR (ptr)->use == exp0)
1116 use0 = ptr;
1117 break;
1120 for (ptr = gimple_use_ops (stmt); ptr; ptr = ptr->next)
1121 if (USE_OP_PTR (ptr)->use == exp1)
1123 use1 = ptr;
1124 break;
1127 /* And adjust their location to point to the new position of the
1128 operand. */
1129 if (use0)
1130 USE_OP_PTR (use0)->use = exp1;
1131 if (use1)
1132 USE_OP_PTR (use1)->use = exp0;
1134 /* Now swap the data. */
1135 *exp0 = op1;
1136 *exp1 = op0;
1141 /* Scan the immediate_use list for VAR making sure its linked properly.
1142 Return TRUE if there is a problem and emit an error message to F. */
1144 DEBUG_FUNCTION bool
1145 verify_imm_links (FILE *f, tree var)
1147 use_operand_p ptr, prev, list;
1148 int count;
1150 gcc_assert (TREE_CODE (var) == SSA_NAME);
1152 list = &(SSA_NAME_IMM_USE_NODE (var));
1153 gcc_assert (list->use == NULL);
1155 if (list->prev == NULL)
1157 gcc_assert (list->next == NULL);
1158 return false;
1161 prev = list;
1162 count = 0;
1163 for (ptr = list->next; ptr != list; )
1165 if (prev != ptr->prev)
1166 goto error;
1168 if (ptr->use == NULL)
1169 goto error; /* 2 roots, or SAFE guard node. */
1170 else if (*(ptr->use) != var)
1171 goto error;
1173 prev = ptr;
1174 ptr = ptr->next;
1176 /* Avoid infinite loops. 50,000,000 uses probably indicates a
1177 problem. */
1178 if (count++ > 50000000)
1179 goto error;
1182 /* Verify list in the other direction. */
1183 prev = list;
1184 for (ptr = list->prev; ptr != list; )
1186 if (prev != ptr->next)
1187 goto error;
1188 prev = ptr;
1189 ptr = ptr->prev;
1190 if (count-- < 0)
1191 goto error;
1194 if (count != 0)
1195 goto error;
1197 return false;
1199 error:
1200 if (ptr->loc.stmt && gimple_modified_p (ptr->loc.stmt))
1202 fprintf (f, " STMT MODIFIED. - <%p> ", (void *)ptr->loc.stmt);
1203 print_gimple_stmt (f, ptr->loc.stmt, 0, TDF_SLIM);
1205 fprintf (f, " IMM ERROR : (use_p : tree - %p:%p)", (void *)ptr,
1206 (void *)ptr->use);
1207 print_generic_expr (f, USE_FROM_PTR (ptr), TDF_SLIM);
1208 fprintf (f, "\n");
1209 return true;
1213 /* Dump all the immediate uses to FILE. */
1215 void
1216 dump_immediate_uses_for (FILE *file, tree var)
1218 imm_use_iterator iter;
1219 use_operand_p use_p;
1221 gcc_assert (var && TREE_CODE (var) == SSA_NAME);
1223 print_generic_expr (file, var, TDF_SLIM);
1224 fprintf (file, " : -->");
1225 if (has_zero_uses (var))
1226 fprintf (file, " no uses.\n");
1227 else
1228 if (has_single_use (var))
1229 fprintf (file, " single use.\n");
1230 else
1231 fprintf (file, "%d uses.\n", num_imm_uses (var));
1233 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
1235 if (use_p->loc.stmt == NULL && use_p->use == NULL)
1236 fprintf (file, "***end of stmt iterator marker***\n");
1237 else
1238 if (!is_gimple_reg (USE_FROM_PTR (use_p)))
1239 print_gimple_stmt (file, USE_STMT (use_p), 0, TDF_VOPS|TDF_MEMSYMS);
1240 else
1241 print_gimple_stmt (file, USE_STMT (use_p), 0, TDF_SLIM);
1243 fprintf (file, "\n");
1247 /* Dump all the immediate uses to FILE. */
1249 void
1250 dump_immediate_uses (FILE *file)
1252 tree var;
1253 unsigned int x;
1255 fprintf (file, "Immediate_uses: \n\n");
1256 for (x = 1; x < num_ssa_names; x++)
1258 var = ssa_name (x);
1259 if (!var)
1260 continue;
1261 dump_immediate_uses_for (file, var);
1266 /* Dump def-use edges on stderr. */
1268 DEBUG_FUNCTION void
1269 debug_immediate_uses (void)
1271 dump_immediate_uses (stderr);
1275 /* Dump def-use edges on stderr. */
1277 DEBUG_FUNCTION void
1278 debug_immediate_uses_for (tree var)
1280 dump_immediate_uses_for (stderr, var);
1284 /* Unlink STMTs virtual definition from the IL by propagating its use. */
1286 void
1287 unlink_stmt_vdef (gimple *stmt)
1289 use_operand_p use_p;
1290 imm_use_iterator iter;
1291 gimple *use_stmt;
1292 tree vdef = gimple_vdef (stmt);
1293 tree vuse = gimple_vuse (stmt);
1295 if (!vdef
1296 || TREE_CODE (vdef) != SSA_NAME)
1297 return;
1299 FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
1301 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1302 SET_USE (use_p, vuse);
1305 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vdef))
1306 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vuse) = 1;
1309 /* Return true if the var whose chain of uses starts at PTR has a
1310 single nondebug use. Set USE_P and STMT to that single nondebug
1311 use, if so, or to NULL otherwise. */
1312 bool
1313 single_imm_use_1 (const ssa_use_operand_t *head,
1314 use_operand_p *use_p, gimple **stmt)
1316 ssa_use_operand_t *ptr, *single_use = 0;
1318 for (ptr = head->next; ptr != head; ptr = ptr->next)
1319 if (USE_STMT(ptr) && !is_gimple_debug (USE_STMT (ptr)))
1321 if (single_use)
1323 single_use = NULL;
1324 break;
1326 single_use = ptr;
1329 if (use_p)
1330 *use_p = single_use;
1332 if (stmt)
1333 *stmt = single_use ? single_use->loc.stmt : NULL;
1335 return single_use;