2015-06-23 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / tree-ssa-operands.c
blobf3abe54aba8142a99653b1606cfa6b64bd640e20
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 "tm.h"
24 #include "alias.h"
25 #include "symtab.h"
26 #include "tree.h"
27 #include "fold-const.h"
28 #include "stmt.h"
29 #include "print-tree.h"
30 #include "flags.h"
31 #include "hard-reg-set.h"
32 #include "function.h"
33 #include "gimple-pretty-print.h"
34 #include "bitmap.h"
35 #include "predict.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "gimple-expr.h"
40 #include "gimple.h"
41 #include "gimple-ssa.h"
42 #include "tree-phinodes.h"
43 #include "ssa-iterators.h"
44 #include "stringpool.h"
45 #include "tree-ssanames.h"
46 #include "tree-inline.h"
47 #include "timevar.h"
48 #include "dumpfile.h"
49 #include "timevar.h"
50 #include "langhooks.h"
51 #include "diagnostic-core.h"
54 /* This file contains the code required to manage the operands cache of the
55 SSA optimizer. For every stmt, we maintain an operand cache in the stmt
56 annotation. This cache contains operands that will be of interest to
57 optimizers and other passes wishing to manipulate the IL.
59 The operand type are broken up into REAL and VIRTUAL operands. The real
60 operands are represented as pointers into the stmt's operand tree. Thus
61 any manipulation of the real operands will be reflected in the actual tree.
62 Virtual operands are represented solely in the cache, although the base
63 variable for the SSA_NAME may, or may not occur in the stmt's tree.
64 Manipulation of the virtual operands will not be reflected in the stmt tree.
66 The routines in this file are concerned with creating this operand cache
67 from a stmt tree.
69 The operand tree is the parsed by the various get_* routines which look
70 through the stmt tree for the occurrence of operands which may be of
71 interest, and calls are made to the append_* routines whenever one is
72 found. There are 4 of these routines, each representing one of the
73 4 types of operands. Defs, Uses, Virtual Uses, and Virtual May Defs.
75 The append_* routines check for duplication, and simply keep a list of
76 unique objects for each operand type in the build_* extendable vectors.
78 Once the stmt tree is completely parsed, the finalize_ssa_operands()
79 routine is called, which proceeds to perform the finalization routine
80 on each of the 4 operand vectors which have been built up.
82 If the stmt had a previous operand cache, the finalization routines
83 attempt to match up the new operands with the old ones. If it's a perfect
84 match, the old vector is simply reused. If it isn't a perfect match, then
85 a new vector is created and the new operands are placed there. For
86 virtual operands, if the previous cache had SSA_NAME version of a
87 variable, and that same variable occurs in the same operands cache, then
88 the new cache vector will also get the same SSA_NAME.
90 i.e., if a stmt had a VUSE of 'a_5', and 'a' occurs in the new
91 operand vector for VUSE, then the new vector will also be modified
92 such that it contains 'a_5' rather than 'a'. */
95 /* Flags to describe operand properties in helpers. */
97 /* By default, operands are loaded. */
98 #define opf_use 0
100 /* Operand is the target of an assignment expression or a
101 call-clobbered variable. */
102 #define opf_def (1 << 0)
104 /* No virtual operands should be created in the expression. This is used
105 when traversing ADDR_EXPR nodes which have different semantics than
106 other expressions. Inside an ADDR_EXPR node, the only operands that we
107 need to consider are indices into arrays. For instance, &a.b[i] should
108 generate a USE of 'i' but it should not generate a VUSE for 'a' nor a
109 VUSE for 'b'. */
110 #define opf_no_vops (1 << 1)
112 /* Operand is in a place where address-taken does not imply addressable. */
113 #define opf_non_addressable (1 << 3)
115 /* Operand is in a place where opf_non_addressable does not apply. */
116 #define opf_not_non_addressable (1 << 4)
118 /* Operand is having its address taken. */
119 #define opf_address_taken (1 << 5)
121 /* Array for building all the use operands. */
122 static vec<tree> build_uses;
124 /* The built VDEF operand. */
125 static tree build_vdef;
127 /* The built VUSE operand. */
128 static tree build_vuse;
130 /* Bitmap obstack for our datastructures that needs to survive across
131 compilations of multiple functions. */
132 static bitmap_obstack operands_bitmap_obstack;
134 static void get_expr_operands (struct function *, gimple, tree *, int);
136 /* Number of functions with initialized ssa_operands. */
137 static int n_initialized = 0;
139 /* Accessor to tree-ssa-operands.c caches. */
140 static inline struct ssa_operands *
141 gimple_ssa_operands (const struct function *fun)
143 return &fun->gimple_df->ssa_operands;
147 /* Return true if the SSA operands cache is active. */
149 bool
150 ssa_operands_active (struct function *fun)
152 if (fun == NULL)
153 return false;
155 return fun->gimple_df && gimple_ssa_operands (fun)->ops_active;
159 /* Create the VOP variable, an artificial global variable to act as a
160 representative of all of the virtual operands FUD chain. */
162 static void
163 create_vop_var (struct function *fn)
165 tree global_var;
167 gcc_assert (fn->gimple_df->vop == NULL_TREE);
169 global_var = build_decl (BUILTINS_LOCATION, VAR_DECL,
170 get_identifier (".MEM"),
171 void_type_node);
172 DECL_ARTIFICIAL (global_var) = 1;
173 DECL_IGNORED_P (global_var) = 1;
174 TREE_READONLY (global_var) = 0;
175 DECL_EXTERNAL (global_var) = 1;
176 TREE_STATIC (global_var) = 1;
177 TREE_USED (global_var) = 1;
178 DECL_CONTEXT (global_var) = NULL_TREE;
179 TREE_THIS_VOLATILE (global_var) = 0;
180 TREE_ADDRESSABLE (global_var) = 0;
181 VAR_DECL_IS_VIRTUAL_OPERAND (global_var) = 1;
183 fn->gimple_df->vop = global_var;
186 /* These are the sizes of the operand memory buffer in bytes which gets
187 allocated each time more operands space is required. The final value is
188 the amount that is allocated every time after that.
189 In 1k we can fit 25 use operands (or 63 def operands) on a host with
190 8 byte pointers, that would be 10 statements each with 1 def and 2
191 uses. */
193 #define OP_SIZE_INIT 0
194 #define OP_SIZE_1 (1024 - sizeof (void *))
195 #define OP_SIZE_2 (1024 * 4 - sizeof (void *))
196 #define OP_SIZE_3 (1024 * 16 - sizeof (void *))
198 /* Initialize the operand cache routines. */
200 void
201 init_ssa_operands (struct function *fn)
203 if (!n_initialized++)
205 build_uses.create (10);
206 build_vuse = NULL_TREE;
207 build_vdef = NULL_TREE;
208 bitmap_obstack_initialize (&operands_bitmap_obstack);
211 gcc_assert (gimple_ssa_operands (fn)->operand_memory == NULL);
212 gimple_ssa_operands (fn)->operand_memory_index
213 = gimple_ssa_operands (fn)->ssa_operand_mem_size;
214 gimple_ssa_operands (fn)->ops_active = true;
215 gimple_ssa_operands (fn)->ssa_operand_mem_size = OP_SIZE_INIT;
216 create_vop_var (fn);
220 /* Dispose of anything required by the operand routines. */
222 void
223 fini_ssa_operands (struct function *fn)
225 struct ssa_operand_memory_d *ptr;
227 if (!--n_initialized)
229 build_uses.release ();
230 build_vdef = NULL_TREE;
231 build_vuse = NULL_TREE;
234 gimple_ssa_operands (fn)->free_uses = NULL;
236 while ((ptr = gimple_ssa_operands (fn)->operand_memory) != NULL)
238 gimple_ssa_operands (fn)->operand_memory
239 = gimple_ssa_operands (fn)->operand_memory->next;
240 ggc_free (ptr);
243 gimple_ssa_operands (fn)->ops_active = false;
245 if (!n_initialized)
246 bitmap_obstack_release (&operands_bitmap_obstack);
248 fn->gimple_df->vop = NULL_TREE;
252 /* Return memory for an operand of size SIZE. */
254 static inline void *
255 ssa_operand_alloc (struct function *fn, unsigned size)
257 char *ptr;
259 gcc_assert (size == sizeof (struct use_optype_d));
261 if (gimple_ssa_operands (fn)->operand_memory_index + size
262 >= gimple_ssa_operands (fn)->ssa_operand_mem_size)
264 struct ssa_operand_memory_d *ptr;
266 switch (gimple_ssa_operands (fn)->ssa_operand_mem_size)
268 case OP_SIZE_INIT:
269 gimple_ssa_operands (fn)->ssa_operand_mem_size = OP_SIZE_1;
270 break;
271 case OP_SIZE_1:
272 gimple_ssa_operands (fn)->ssa_operand_mem_size = OP_SIZE_2;
273 break;
274 case OP_SIZE_2:
275 case OP_SIZE_3:
276 gimple_ssa_operands (fn)->ssa_operand_mem_size = OP_SIZE_3;
277 break;
278 default:
279 gcc_unreachable ();
283 ptr = (ssa_operand_memory_d *) ggc_internal_alloc
284 (sizeof (void *) + gimple_ssa_operands (fn)->ssa_operand_mem_size);
286 ptr->next = gimple_ssa_operands (fn)->operand_memory;
287 gimple_ssa_operands (fn)->operand_memory = ptr;
288 gimple_ssa_operands (fn)->operand_memory_index = 0;
291 ptr = &(gimple_ssa_operands (fn)->operand_memory
292 ->mem[gimple_ssa_operands (fn)->operand_memory_index]);
293 gimple_ssa_operands (fn)->operand_memory_index += size;
294 return ptr;
298 /* Allocate a USE operand. */
300 static inline struct use_optype_d *
301 alloc_use (struct function *fn)
303 struct use_optype_d *ret;
304 if (gimple_ssa_operands (fn)->free_uses)
306 ret = gimple_ssa_operands (fn)->free_uses;
307 gimple_ssa_operands (fn)->free_uses
308 = gimple_ssa_operands (fn)->free_uses->next;
310 else
311 ret = (struct use_optype_d *)
312 ssa_operand_alloc (fn, sizeof (struct use_optype_d));
313 return ret;
317 /* Adds OP to the list of uses of statement STMT after LAST. */
319 static inline use_optype_p
320 add_use_op (struct function *fn, gimple stmt, tree *op, use_optype_p last)
322 use_optype_p new_use;
324 new_use = alloc_use (fn);
325 USE_OP_PTR (new_use)->use = op;
326 link_imm_use_stmt (USE_OP_PTR (new_use), *op, stmt);
327 last->next = new_use;
328 new_use->next = NULL;
329 return new_use;
334 /* Takes elements from build_defs and turns them into def operands of STMT.
335 TODO -- Make build_defs vec of tree *. */
337 static inline void
338 finalize_ssa_defs (struct function *fn, gimple stmt)
340 /* Pre-pend the vdef we may have built. */
341 if (build_vdef != NULL_TREE)
343 tree oldvdef = gimple_vdef (stmt);
344 if (oldvdef
345 && TREE_CODE (oldvdef) == SSA_NAME)
346 oldvdef = SSA_NAME_VAR (oldvdef);
347 if (oldvdef != build_vdef)
348 gimple_set_vdef (stmt, build_vdef);
351 /* Clear and unlink a no longer necessary VDEF. */
352 if (build_vdef == NULL_TREE
353 && gimple_vdef (stmt) != NULL_TREE)
355 if (TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
357 unlink_stmt_vdef (stmt);
358 release_ssa_name_fn (fn, gimple_vdef (stmt));
360 gimple_set_vdef (stmt, NULL_TREE);
363 /* If we have a non-SSA_NAME VDEF, mark it for renaming. */
364 if (gimple_vdef (stmt)
365 && TREE_CODE (gimple_vdef (stmt)) != SSA_NAME)
367 fn->gimple_df->rename_vops = 1;
368 fn->gimple_df->ssa_renaming_needed = 1;
373 /* Takes elements from build_uses and turns them into use operands of STMT.
374 TODO -- Make build_uses vec of tree *. */
376 static inline void
377 finalize_ssa_uses (struct function *fn, gimple stmt)
379 unsigned new_i;
380 struct use_optype_d new_list;
381 use_optype_p old_ops, ptr, last;
383 /* Pre-pend the VUSE we may have built. */
384 if (build_vuse != NULL_TREE)
386 tree oldvuse = gimple_vuse (stmt);
387 if (oldvuse
388 && TREE_CODE (oldvuse) == SSA_NAME)
389 oldvuse = SSA_NAME_VAR (oldvuse);
390 if (oldvuse != (build_vuse != NULL_TREE
391 ? build_vuse : build_vdef))
392 gimple_set_vuse (stmt, NULL_TREE);
393 build_uses.safe_insert (0, (tree)gimple_vuse_ptr (stmt));
396 new_list.next = NULL;
397 last = &new_list;
399 old_ops = gimple_use_ops (stmt);
401 /* Clear a no longer necessary VUSE. */
402 if (build_vuse == NULL_TREE
403 && gimple_vuse (stmt) != NULL_TREE)
404 gimple_set_vuse (stmt, NULL_TREE);
406 /* If there is anything in the old list, free it. */
407 if (old_ops)
409 for (ptr = old_ops; ptr->next; ptr = ptr->next)
410 delink_imm_use (USE_OP_PTR (ptr));
411 delink_imm_use (USE_OP_PTR (ptr));
412 ptr->next = gimple_ssa_operands (fn)->free_uses;
413 gimple_ssa_operands (fn)->free_uses = old_ops;
416 /* If we added a VUSE, make sure to set the operand if it is not already
417 present and mark it for renaming. */
418 if (build_vuse != NULL_TREE
419 && gimple_vuse (stmt) == NULL_TREE)
421 gimple_set_vuse (stmt, gimple_vop (fn));
422 fn->gimple_df->rename_vops = 1;
423 fn->gimple_df->ssa_renaming_needed = 1;
426 /* Now create nodes for all the new nodes. */
427 for (new_i = 0; new_i < build_uses.length (); new_i++)
429 tree *op = (tree *) build_uses[new_i];
430 last = add_use_op (fn, stmt, op, last);
433 /* Now set the stmt's operands. */
434 gimple_set_use_ops (stmt, new_list.next);
438 /* Clear the in_list bits and empty the build array for VDEFs and
439 VUSEs. */
441 static inline void
442 cleanup_build_arrays (void)
444 build_vdef = NULL_TREE;
445 build_vuse = NULL_TREE;
446 build_uses.truncate (0);
450 /* Finalize all the build vectors, fill the new ones into INFO. */
452 static inline void
453 finalize_ssa_stmt_operands (struct function *fn, gimple stmt)
455 finalize_ssa_defs (fn, stmt);
456 finalize_ssa_uses (fn, stmt);
457 cleanup_build_arrays ();
461 /* Start the process of building up operands vectors in INFO. */
463 static inline void
464 start_ssa_stmt_operands (void)
466 gcc_assert (build_uses.length () == 0);
467 gcc_assert (build_vuse == NULL_TREE);
468 gcc_assert (build_vdef == NULL_TREE);
472 /* Add USE_P to the list of pointers to operands. */
474 static inline void
475 append_use (tree *use_p)
477 build_uses.safe_push ((tree) use_p);
481 /* Add VAR to the set of variables that require a VDEF operator. */
483 static inline void
484 append_vdef (tree var)
486 gcc_assert ((build_vdef == NULL_TREE
487 || build_vdef == var)
488 && (build_vuse == NULL_TREE
489 || build_vuse == var));
491 build_vdef = var;
492 build_vuse = var;
496 /* Add VAR to the set of variables that require a VUSE operator. */
498 static inline void
499 append_vuse (tree var)
501 gcc_assert (build_vuse == NULL_TREE
502 || build_vuse == var);
504 build_vuse = var;
507 /* Add virtual operands for STMT. FLAGS is as in get_expr_operands. */
509 static void
510 add_virtual_operand (struct function *fn,
511 gimple stmt ATTRIBUTE_UNUSED, int flags)
513 /* Add virtual operands to the stmt, unless the caller has specifically
514 requested not to do that (used when adding operands inside an
515 ADDR_EXPR expression). */
516 if (flags & opf_no_vops)
517 return;
519 gcc_assert (!is_gimple_debug (stmt));
521 if (flags & opf_def)
522 append_vdef (gimple_vop (fn));
523 else
524 append_vuse (gimple_vop (fn));
528 /* Add *VAR_P to the appropriate operand array for statement STMT.
529 FLAGS is as in get_expr_operands. If *VAR_P is a GIMPLE register,
530 it will be added to the statement's real operands, otherwise it is
531 added to virtual operands. */
533 static void
534 add_stmt_operand (struct function *fn, tree *var_p, gimple stmt, int flags)
536 tree var = *var_p;
538 gcc_assert (SSA_VAR_P (*var_p));
540 if (is_gimple_reg (var))
542 /* The variable is a GIMPLE register. Add it to real operands. */
543 if (flags & opf_def)
545 else
546 append_use (var_p);
547 if (DECL_P (*var_p))
548 fn->gimple_df->ssa_renaming_needed = 1;
550 else
552 /* Mark statements with volatile operands. */
553 if (!(flags & opf_no_vops)
554 && TREE_THIS_VOLATILE (var))
555 gimple_set_has_volatile_ops (stmt, true);
557 /* The variable is a memory access. Add virtual operands. */
558 add_virtual_operand (fn, stmt, flags);
562 /* Mark the base address of REF as having its address taken.
563 REF may be a single variable whose address has been taken or any
564 other valid GIMPLE memory reference (structure reference, array,
565 etc). */
567 static void
568 mark_address_taken (tree ref)
570 tree var;
572 /* Note that it is *NOT OKAY* to use the target of a COMPONENT_REF
573 as the only thing we take the address of. If VAR is a structure,
574 taking the address of a field means that the whole structure may
575 be referenced using pointer arithmetic. See PR 21407 and the
576 ensuing mailing list discussion. */
577 var = get_base_address (ref);
578 if (var)
580 if (DECL_P (var))
581 TREE_ADDRESSABLE (var) = 1;
582 else if (TREE_CODE (var) == MEM_REF
583 && TREE_CODE (TREE_OPERAND (var, 0)) == ADDR_EXPR
584 && DECL_P (TREE_OPERAND (TREE_OPERAND (var, 0), 0)))
585 TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (var, 0), 0)) = 1;
590 /* A subroutine of get_expr_operands to handle MEM_REF.
592 STMT is the statement being processed, EXPR is the MEM_REF
593 that got us here.
595 FLAGS is as in get_expr_operands. */
597 static void
598 get_mem_ref_operands (struct function *fn,
599 gimple stmt, tree expr, int flags)
601 tree *pptr = &TREE_OPERAND (expr, 0);
603 if (!(flags & opf_no_vops)
604 && TREE_THIS_VOLATILE (expr))
605 gimple_set_has_volatile_ops (stmt, true);
607 /* Add the VOP. */
608 add_virtual_operand (fn, stmt, flags);
610 /* If requested, add a USE operand for the base pointer. */
611 get_expr_operands (fn, stmt, pptr,
612 opf_non_addressable | opf_use
613 | (flags & (opf_no_vops|opf_not_non_addressable)));
617 /* A subroutine of get_expr_operands to handle TARGET_MEM_REF. */
619 static void
620 get_tmr_operands (struct function *fn, gimple stmt, tree expr, int flags)
622 if (!(flags & opf_no_vops)
623 && TREE_THIS_VOLATILE (expr))
624 gimple_set_has_volatile_ops (stmt, true);
626 /* First record the real operands. */
627 get_expr_operands (fn, stmt,
628 &TMR_BASE (expr), opf_use | (flags & opf_no_vops));
629 get_expr_operands (fn, stmt,
630 &TMR_INDEX (expr), opf_use | (flags & opf_no_vops));
631 get_expr_operands (fn, stmt,
632 &TMR_INDEX2 (expr), opf_use | (flags & opf_no_vops));
634 add_virtual_operand (fn, stmt, flags);
638 /* If STMT is a call that may clobber globals and other symbols that
639 escape, add them to the VDEF/VUSE lists for it. */
641 static void
642 maybe_add_call_vops (struct function *fn, gcall *stmt)
644 int call_flags = gimple_call_flags (stmt);
646 /* If aliases have been computed already, add VDEF or VUSE
647 operands for all the symbols that have been found to be
648 call-clobbered. */
649 if (!(call_flags & ECF_NOVOPS))
651 /* A 'pure' or a 'const' function never call-clobbers anything. */
652 if (!(call_flags & (ECF_PURE | ECF_CONST)))
653 add_virtual_operand (fn, stmt, opf_def);
654 else if (!(call_flags & ECF_CONST))
655 add_virtual_operand (fn, stmt, opf_use);
660 /* Scan operands in the ASM_EXPR stmt referred to in INFO. */
662 static void
663 get_asm_stmt_operands (struct function *fn, gasm *stmt)
665 size_t i, noutputs;
666 const char **oconstraints;
667 const char *constraint;
668 bool allows_mem, allows_reg, is_inout;
670 noutputs = gimple_asm_noutputs (stmt);
671 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
673 /* Gather all output operands. */
674 for (i = 0; i < gimple_asm_noutputs (stmt); i++)
676 tree link = gimple_asm_output_op (stmt, i);
677 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
678 oconstraints[i] = constraint;
679 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
680 &allows_reg, &is_inout);
682 /* This should have been split in gimplify_asm_expr. */
683 gcc_assert (!allows_reg || !is_inout);
685 /* Memory operands are addressable. Note that STMT needs the
686 address of this operand. */
687 if (!allows_reg && allows_mem)
688 mark_address_taken (TREE_VALUE (link));
690 get_expr_operands (fn, stmt,
691 &TREE_VALUE (link), opf_def | opf_not_non_addressable);
694 /* Gather all input operands. */
695 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
697 tree link = gimple_asm_input_op (stmt, i);
698 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
699 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
700 &allows_mem, &allows_reg);
702 /* Memory operands are addressable. Note that STMT needs the
703 address of this operand. */
704 if (!allows_reg && allows_mem)
705 mark_address_taken (TREE_VALUE (link));
707 get_expr_operands (fn, stmt, &TREE_VALUE (link), opf_not_non_addressable);
710 /* Clobber all memory and addressable symbols for asm ("" : : : "memory"); */
711 if (gimple_asm_clobbers_memory_p (stmt))
712 add_virtual_operand (fn, stmt, opf_def);
716 /* Recursively scan the expression pointed to by EXPR_P in statement
717 STMT. FLAGS is one of the OPF_* constants modifying how to
718 interpret the operands found. */
720 static void
721 get_expr_operands (struct function *fn, gimple stmt, tree *expr_p, int flags)
723 enum tree_code code;
724 enum tree_code_class codeclass;
725 tree expr = *expr_p;
726 int uflags = opf_use;
728 if (expr == NULL)
729 return;
731 if (is_gimple_debug (stmt))
732 uflags |= (flags & opf_no_vops);
734 code = TREE_CODE (expr);
735 codeclass = TREE_CODE_CLASS (code);
737 switch (code)
739 case ADDR_EXPR:
740 /* Taking the address of a variable does not represent a
741 reference to it, but the fact that the statement takes its
742 address will be of interest to some passes (e.g. alias
743 resolution). */
744 if ((!(flags & opf_non_addressable)
745 || (flags & opf_not_non_addressable))
746 && !is_gimple_debug (stmt))
747 mark_address_taken (TREE_OPERAND (expr, 0));
749 /* Otherwise, there may be variables referenced inside but there
750 should be no VUSEs created, since the referenced objects are
751 not really accessed. The only operands that we should find
752 here are ARRAY_REF indices which will always be real operands
753 (GIMPLE does not allow non-registers as array indices). */
754 flags |= opf_no_vops;
755 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 0),
756 flags | opf_not_non_addressable | opf_address_taken);
757 return;
759 case SSA_NAME:
760 case VAR_DECL:
761 case PARM_DECL:
762 case RESULT_DECL:
763 if (!(flags & opf_address_taken))
764 add_stmt_operand (fn, expr_p, stmt, flags);
765 return;
767 case DEBUG_EXPR_DECL:
768 gcc_assert (gimple_debug_bind_p (stmt));
769 return;
771 case MEM_REF:
772 get_mem_ref_operands (fn, stmt, expr, flags);
773 return;
775 case TARGET_MEM_REF:
776 get_tmr_operands (fn, stmt, expr, flags);
777 return;
779 case ARRAY_REF:
780 case ARRAY_RANGE_REF:
781 case COMPONENT_REF:
782 case REALPART_EXPR:
783 case IMAGPART_EXPR:
785 if (!(flags & opf_no_vops)
786 && TREE_THIS_VOLATILE (expr))
787 gimple_set_has_volatile_ops (stmt, true);
789 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 0), flags);
791 if (code == COMPONENT_REF)
793 if (!(flags & opf_no_vops)
794 && TREE_THIS_VOLATILE (TREE_OPERAND (expr, 1)))
795 gimple_set_has_volatile_ops (stmt, true);
796 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 2), uflags);
798 else if (code == ARRAY_REF || code == ARRAY_RANGE_REF)
800 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 1), uflags);
801 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 2), uflags);
802 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 3), uflags);
805 return;
808 case WITH_SIZE_EXPR:
809 /* WITH_SIZE_EXPR is a pass-through reference to its first argument,
810 and an rvalue reference to its second argument. */
811 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 1), uflags);
812 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 0), flags);
813 return;
815 case COND_EXPR:
816 case VEC_COND_EXPR:
817 case VEC_PERM_EXPR:
818 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 0), uflags);
819 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 1), uflags);
820 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 2), uflags);
821 return;
823 case CONSTRUCTOR:
825 /* General aggregate CONSTRUCTORs have been decomposed, but they
826 are still in use as the COMPLEX_EXPR equivalent for vectors. */
827 constructor_elt *ce;
828 unsigned HOST_WIDE_INT idx;
830 /* A volatile constructor is actually TREE_CLOBBER_P, transfer
831 the volatility to the statement, don't use TREE_CLOBBER_P for
832 mirroring the other uses of THIS_VOLATILE in this file. */
833 if (!(flags & opf_no_vops)
834 && TREE_THIS_VOLATILE (expr))
835 gimple_set_has_volatile_ops (stmt, true);
837 for (idx = 0;
838 vec_safe_iterate (CONSTRUCTOR_ELTS (expr), idx, &ce);
839 idx++)
840 get_expr_operands (fn, stmt, &ce->value, uflags);
842 return;
845 case BIT_FIELD_REF:
846 if (!(flags & opf_no_vops)
847 && TREE_THIS_VOLATILE (expr))
848 gimple_set_has_volatile_ops (stmt, true);
849 /* FALLTHRU */
851 case VIEW_CONVERT_EXPR:
852 do_unary:
853 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 0), flags);
854 return;
856 case COMPOUND_EXPR:
857 case OBJ_TYPE_REF:
858 case ASSERT_EXPR:
859 do_binary:
861 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 0), flags);
862 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 1), flags);
863 return;
866 case DOT_PROD_EXPR:
867 case SAD_EXPR:
868 case REALIGN_LOAD_EXPR:
869 case WIDEN_MULT_PLUS_EXPR:
870 case WIDEN_MULT_MINUS_EXPR:
871 case FMA_EXPR:
873 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 0), flags);
874 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 1), flags);
875 get_expr_operands (fn, stmt, &TREE_OPERAND (expr, 2), flags);
876 return;
879 case FUNCTION_DECL:
880 case LABEL_DECL:
881 case CONST_DECL:
882 case CASE_LABEL_EXPR:
883 /* Expressions that make no memory references. */
884 return;
886 default:
887 if (codeclass == tcc_unary)
888 goto do_unary;
889 if (codeclass == tcc_binary || codeclass == tcc_comparison)
890 goto do_binary;
891 if (codeclass == tcc_constant || codeclass == tcc_type)
892 return;
895 /* If we get here, something has gone wrong. */
896 #ifdef ENABLE_CHECKING
897 fprintf (stderr, "unhandled expression in get_expr_operands():\n");
898 debug_tree (expr);
899 fputs ("\n", stderr);
900 #endif
901 gcc_unreachable ();
905 /* Parse STMT looking for operands. When finished, the various
906 build_* operand vectors will have potential operands in them. */
908 static void
909 parse_ssa_operands (struct function *fn, gimple stmt)
911 enum gimple_code code = gimple_code (stmt);
912 size_t i, n, start = 0;
914 switch (code)
916 case GIMPLE_ASM:
917 get_asm_stmt_operands (fn, as_a <gasm *> (stmt));
918 break;
920 case GIMPLE_TRANSACTION:
921 /* The start of a transaction is a memory barrier. */
922 add_virtual_operand (fn, stmt, opf_def | opf_use);
923 break;
925 case GIMPLE_DEBUG:
926 if (gimple_debug_bind_p (stmt)
927 && gimple_debug_bind_has_value_p (stmt))
928 get_expr_operands (fn, stmt, gimple_debug_bind_get_value_ptr (stmt),
929 opf_use | opf_no_vops);
930 break;
932 case GIMPLE_RETURN:
933 append_vuse (gimple_vop (fn));
934 goto do_default;
936 case GIMPLE_CALL:
937 /* Add call-clobbered operands, if needed. */
938 maybe_add_call_vops (fn, as_a <gcall *> (stmt));
939 /* FALLTHRU */
941 case GIMPLE_ASSIGN:
942 get_expr_operands (fn, stmt, gimple_op_ptr (stmt, 0), opf_def);
943 start = 1;
944 /* FALLTHRU */
946 default:
947 do_default:
948 n = gimple_num_ops (stmt);
949 for (i = start; i < n; i++)
950 get_expr_operands (fn, stmt, gimple_op_ptr (stmt, i), opf_use);
951 break;
956 /* Create an operands cache for STMT. */
958 static void
959 build_ssa_operands (struct function *fn, gimple stmt)
961 /* Initially assume that the statement has no volatile operands. */
962 gimple_set_has_volatile_ops (stmt, false);
964 start_ssa_stmt_operands ();
965 parse_ssa_operands (fn, stmt);
966 finalize_ssa_stmt_operands (fn, stmt);
969 /* Verifies SSA statement operands. */
971 DEBUG_FUNCTION bool
972 verify_ssa_operands (struct function *fn, gimple stmt)
974 use_operand_p use_p;
975 def_operand_p def_p;
976 ssa_op_iter iter;
977 unsigned i;
978 tree use, def;
979 bool volatile_p = gimple_has_volatile_ops (stmt);
981 /* build_ssa_operands w/o finalizing them. */
982 gimple_set_has_volatile_ops (stmt, false);
983 start_ssa_stmt_operands ();
984 parse_ssa_operands (fn, stmt);
986 /* Now verify the built operands are the same as present in STMT. */
987 def = gimple_vdef (stmt);
988 if (def
989 && TREE_CODE (def) == SSA_NAME)
990 def = SSA_NAME_VAR (def);
991 if (build_vdef != def)
993 error ("virtual definition of statement not up-to-date");
994 return true;
996 if (gimple_vdef (stmt)
997 && ((def_p = gimple_vdef_op (stmt)) == NULL_DEF_OPERAND_P
998 || DEF_FROM_PTR (def_p) != gimple_vdef (stmt)))
1000 error ("virtual def operand missing for stmt");
1001 return true;
1004 use = gimple_vuse (stmt);
1005 if (use
1006 && TREE_CODE (use) == SSA_NAME)
1007 use = SSA_NAME_VAR (use);
1008 if (build_vuse != use)
1010 error ("virtual use of statement not up-to-date");
1011 return true;
1013 if (gimple_vuse (stmt)
1014 && ((use_p = gimple_vuse_op (stmt)) == NULL_USE_OPERAND_P
1015 || USE_FROM_PTR (use_p) != gimple_vuse (stmt)))
1017 error ("virtual use operand missing for stmt");
1018 return true;
1021 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1023 FOR_EACH_VEC_ELT (build_uses, i, use)
1025 if (use_p->use == (tree *)use)
1027 build_uses[i] = NULL_TREE;
1028 break;
1031 if (i == build_uses.length ())
1033 error ("excess use operand for stmt");
1034 debug_generic_expr (USE_FROM_PTR (use_p));
1035 return true;
1038 FOR_EACH_VEC_ELT (build_uses, i, use)
1039 if (use != NULL_TREE)
1041 error ("use operand missing for stmt");
1042 debug_generic_expr (*(tree *)use);
1043 return true;
1046 if (gimple_has_volatile_ops (stmt) != volatile_p)
1048 error ("stmt volatile flag not up-to-date");
1049 return true;
1052 cleanup_build_arrays ();
1053 return false;
1057 /* Releases the operands of STMT back to their freelists, and clears
1058 the stmt operand lists. */
1060 void
1061 free_stmt_operands (struct function *fn, gimple stmt)
1063 use_optype_p uses = gimple_use_ops (stmt), last_use;
1065 if (uses)
1067 for (last_use = uses; last_use->next; last_use = last_use->next)
1068 delink_imm_use (USE_OP_PTR (last_use));
1069 delink_imm_use (USE_OP_PTR (last_use));
1070 last_use->next = gimple_ssa_operands (fn)->free_uses;
1071 gimple_ssa_operands (fn)->free_uses = uses;
1072 gimple_set_use_ops (stmt, NULL);
1075 if (gimple_has_mem_ops (stmt))
1077 gimple_set_vuse (stmt, NULL_TREE);
1078 gimple_set_vdef (stmt, NULL_TREE);
1083 /* Get the operands of statement STMT. */
1085 void
1086 update_stmt_operands (struct function *fn, gimple stmt)
1088 /* If update_stmt_operands is called before SSA is initialized, do
1089 nothing. */
1090 if (!ssa_operands_active (fn))
1091 return;
1093 timevar_push (TV_TREE_OPS);
1095 gcc_assert (gimple_modified_p (stmt));
1096 build_ssa_operands (fn, 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 if (op0 != op1)
1115 /* Attempt to preserve the relative positions of these two operands in
1116 their * respective immediate use lists by adjusting their use pointer
1117 to point to the new operand position. */
1118 use_optype_p use0, use1, ptr;
1119 use0 = use1 = NULL;
1121 /* Find the 2 operands in the cache, if they are there. */
1122 for (ptr = gimple_use_ops (stmt); ptr; ptr = ptr->next)
1123 if (USE_OP_PTR (ptr)->use == exp0)
1125 use0 = ptr;
1126 break;
1129 for (ptr = gimple_use_ops (stmt); ptr; ptr = ptr->next)
1130 if (USE_OP_PTR (ptr)->use == exp1)
1132 use1 = ptr;
1133 break;
1136 /* And adjust their location to point to the new position of the
1137 operand. */
1138 if (use0)
1139 USE_OP_PTR (use0)->use = exp1;
1140 if (use1)
1141 USE_OP_PTR (use1)->use = exp0;
1143 /* Now swap the data. */
1144 *exp0 = op1;
1145 *exp1 = op0;
1150 /* Scan the immediate_use list for VAR making sure its linked properly.
1151 Return TRUE if there is a problem and emit an error message to F. */
1153 DEBUG_FUNCTION bool
1154 verify_imm_links (FILE *f, tree var)
1156 use_operand_p ptr, prev, list;
1157 int count;
1159 gcc_assert (TREE_CODE (var) == SSA_NAME);
1161 list = &(SSA_NAME_IMM_USE_NODE (var));
1162 gcc_assert (list->use == NULL);
1164 if (list->prev == NULL)
1166 gcc_assert (list->next == NULL);
1167 return false;
1170 prev = list;
1171 count = 0;
1172 for (ptr = list->next; ptr != list; )
1174 if (prev != ptr->prev)
1175 goto error;
1177 if (ptr->use == NULL)
1178 goto error; /* 2 roots, or SAFE guard node. */
1179 else if (*(ptr->use) != var)
1180 goto error;
1182 prev = ptr;
1183 ptr = ptr->next;
1185 /* Avoid infinite loops. 50,000,000 uses probably indicates a
1186 problem. */
1187 if (count++ > 50000000)
1188 goto error;
1191 /* Verify list in the other direction. */
1192 prev = list;
1193 for (ptr = list->prev; ptr != list; )
1195 if (prev != ptr->next)
1196 goto error;
1197 prev = ptr;
1198 ptr = ptr->prev;
1199 if (count-- < 0)
1200 goto error;
1203 if (count != 0)
1204 goto error;
1206 return false;
1208 error:
1209 if (ptr->loc.stmt && gimple_modified_p (ptr->loc.stmt))
1211 fprintf (f, " STMT MODIFIED. - <%p> ", (void *)ptr->loc.stmt);
1212 print_gimple_stmt (f, ptr->loc.stmt, 0, TDF_SLIM);
1214 fprintf (f, " IMM ERROR : (use_p : tree - %p:%p)", (void *)ptr,
1215 (void *)ptr->use);
1216 print_generic_expr (f, USE_FROM_PTR (ptr), TDF_SLIM);
1217 fprintf (f, "\n");
1218 return true;
1222 /* Dump all the immediate uses to FILE. */
1224 void
1225 dump_immediate_uses_for (FILE *file, tree var)
1227 imm_use_iterator iter;
1228 use_operand_p use_p;
1230 gcc_assert (var && TREE_CODE (var) == SSA_NAME);
1232 print_generic_expr (file, var, TDF_SLIM);
1233 fprintf (file, " : -->");
1234 if (has_zero_uses (var))
1235 fprintf (file, " no uses.\n");
1236 else
1237 if (has_single_use (var))
1238 fprintf (file, " single use.\n");
1239 else
1240 fprintf (file, "%d uses.\n", num_imm_uses (var));
1242 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
1244 if (use_p->loc.stmt == NULL && use_p->use == NULL)
1245 fprintf (file, "***end of stmt iterator marker***\n");
1246 else
1247 if (!is_gimple_reg (USE_FROM_PTR (use_p)))
1248 print_gimple_stmt (file, USE_STMT (use_p), 0, TDF_VOPS|TDF_MEMSYMS);
1249 else
1250 print_gimple_stmt (file, USE_STMT (use_p), 0, TDF_SLIM);
1252 fprintf (file, "\n");
1256 /* Dump all the immediate uses to FILE. */
1258 void
1259 dump_immediate_uses (FILE *file)
1261 tree var;
1262 unsigned int x;
1264 fprintf (file, "Immediate_uses: \n\n");
1265 for (x = 1; x < num_ssa_names; x++)
1267 var = ssa_name (x);
1268 if (!var)
1269 continue;
1270 dump_immediate_uses_for (file, var);
1275 /* Dump def-use edges on stderr. */
1277 DEBUG_FUNCTION void
1278 debug_immediate_uses (void)
1280 dump_immediate_uses (stderr);
1284 /* Dump def-use edges on stderr. */
1286 DEBUG_FUNCTION void
1287 debug_immediate_uses_for (tree var)
1289 dump_immediate_uses_for (stderr, var);
1293 /* Unlink STMTs virtual definition from the IL by propagating its use. */
1295 void
1296 unlink_stmt_vdef (gimple stmt)
1298 use_operand_p use_p;
1299 imm_use_iterator iter;
1300 gimple use_stmt;
1301 tree vdef = gimple_vdef (stmt);
1302 tree vuse = gimple_vuse (stmt);
1304 if (!vdef
1305 || TREE_CODE (vdef) != SSA_NAME)
1306 return;
1308 FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
1310 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1311 SET_USE (use_p, vuse);
1314 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vdef))
1315 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vuse) = 1;
1319 /* Return true if the var whose chain of uses starts at PTR has no
1320 nondebug uses. */
1321 bool
1322 has_zero_uses_1 (const ssa_use_operand_t *head)
1324 const ssa_use_operand_t *ptr;
1326 for (ptr = head->next; ptr != head; ptr = ptr->next)
1327 if (!is_gimple_debug (USE_STMT (ptr)))
1328 return false;
1330 return true;
1334 /* Return true if the var whose chain of uses starts at PTR has a
1335 single nondebug use. Set USE_P and STMT to that single nondebug
1336 use, if so, or to NULL otherwise. */
1337 bool
1338 single_imm_use_1 (const ssa_use_operand_t *head,
1339 use_operand_p *use_p, gimple *stmt)
1341 ssa_use_operand_t *ptr, *single_use = 0;
1343 for (ptr = head->next; ptr != head; ptr = ptr->next)
1344 if (!is_gimple_debug (USE_STMT (ptr)))
1346 if (single_use)
1348 single_use = NULL;
1349 break;
1351 single_use = ptr;
1354 if (use_p)
1355 *use_p = single_use;
1357 if (stmt)
1358 *stmt = single_use ? single_use->loc.stmt : NULL;
1360 return single_use;