ada: Fix infinite loop with multiple limited with clauses
[official-gcc.git] / gcc / tree-ssa-operands.cc
blob57e393ae1644810ed0a6f363aec369e415b47ceb
1 /* SSA operands management for trees.
2 Copyright (C) 2003-2023 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 "timevar.h"
27 #include "ssa.h"
28 #include "gimple-pretty-print.h"
29 #include "diagnostic-core.h"
30 #include "stmt.h"
31 #include "print-tree.h"
32 #include "dumpfile.h"
33 #include "value-query.h"
36 /* This file contains the code required to manage the operands cache of the
37 SSA optimizer. For every stmt, we maintain an operand cache in the stmt
38 annotation. This cache contains operands that will be of interest to
39 optimizers and other passes wishing to manipulate the IL.
41 The operand type are broken up into REAL and VIRTUAL operands. The real
42 operands are represented as pointers into the stmt's operand tree. Thus
43 any manipulation of the real operands will be reflected in the actual tree.
44 Virtual operands are represented solely in the cache, although the base
45 variable for the SSA_NAME may, or may not occur in the stmt's tree.
46 Manipulation of the virtual operands will not be reflected in the stmt tree.
48 The routines in this file are concerned with creating this operand cache
49 from a stmt tree.
51 The operand tree is the parsed by the various get_* routines which look
52 through the stmt tree for the occurrence of operands which may be of
53 interest, and calls are made to the append_* routines whenever one is
54 found. There are 4 of these routines, each representing one of the
55 4 types of operands. Defs, Uses, Virtual Uses, and Virtual May Defs.
57 The append_* routines check for duplication, and simply keep a list of
58 unique objects for each operand type in the build_* extendable vectors.
60 Once the stmt tree is completely parsed, the finalize_ssa_operands()
61 routine is called, which proceeds to perform the finalization routine
62 on each of the 4 operand vectors which have been built up.
64 If the stmt had a previous operand cache, the finalization routines
65 attempt to match up the new operands with the old ones. If it's a perfect
66 match, the old vector is simply reused. If it isn't a perfect match, then
67 a new vector is created and the new operands are placed there. For
68 virtual operands, if the previous cache had SSA_NAME version of a
69 variable, and that same variable occurs in the same operands cache, then
70 the new cache vector will also get the same SSA_NAME.
72 i.e., if a stmt had a VUSE of 'a_5', and 'a' occurs in the new
73 operand vector for VUSE, then the new vector will also be modified
74 such that it contains 'a_5' rather than 'a'. */
77 /* Flags to describe operand properties in helpers. */
79 /* By default, operands are loaded. */
80 #define opf_use 0
82 /* Operand is the target of an assignment expression or a
83 call-clobbered variable. */
84 #define opf_def (1 << 0)
86 /* No virtual operands should be created in the expression. This is used
87 when traversing ADDR_EXPR nodes which have different semantics than
88 other expressions. Inside an ADDR_EXPR node, the only operands that we
89 need to consider are indices into arrays. For instance, &a.b[i] should
90 generate a USE of 'i' but it should not generate a VUSE for 'a' nor a
91 VUSE for 'b'. */
92 #define opf_no_vops (1 << 1)
94 /* Operand is in a place where address-taken does not imply addressable. */
95 #define opf_non_addressable (1 << 3)
97 /* Operand is in a place where opf_non_addressable does not apply. */
98 #define opf_not_non_addressable (1 << 4)
100 /* Operand is having its address taken. */
101 #define opf_address_taken (1 << 5)
103 /* Class containing temporary per-stmt state. */
105 class operands_scanner
107 public:
108 operands_scanner (struct function *fun, gimple *statement)
110 build_vuse = NULL_TREE;
111 build_vdef = NULL_TREE;
112 fn = fun;
113 stmt = statement;
116 /* Create an operands cache for STMT. */
117 void build_ssa_operands ();
119 /* Verifies SSA statement operands. */
120 DEBUG_FUNCTION bool verify_ssa_operands ();
122 private:
123 /* Disable copy and assign of this class, as it may have problems with
124 build_uses vec. */
125 DISABLE_COPY_AND_ASSIGN (operands_scanner);
127 /* Array for building all the use operands. */
128 auto_vec<tree *, 16> build_uses;
130 /* The built VDEF operand. */
131 tree build_vdef;
133 /* The built VUSE operand. */
134 tree build_vuse;
136 /* Function which STMT belongs to. */
137 struct function *fn;
139 /* Statement to work on. */
140 gimple *stmt;
142 /* Takes elements from build_uses and turns them into use operands of STMT. */
143 void finalize_ssa_uses ();
145 /* Clear the in_list bits and empty the build array for VDEFs and
146 VUSEs. */
147 void cleanup_build_arrays ();
149 /* Finalize all the build vectors, fill the new ones into INFO. */
150 void finalize_ssa_stmt_operands ();
152 /* Start the process of building up operands vectors in INFO. */
153 void start_ssa_stmt_operands ();
155 /* Add USE_P to the list of pointers to operands. */
156 void append_use (tree *use_p);
158 /* Add VAR to the set of variables that require a VDEF operator. */
159 void append_vdef (tree var);
161 /* Add VAR to the set of variables that require a VUSE operator. */
162 void append_vuse (tree var);
164 /* Add virtual operands for STMT. FLAGS is as in get_expr_operands. */
165 void add_virtual_operand (int flags);
168 /* Add *VAR_P to the appropriate operand array for statement STMT.
169 FLAGS is as in get_expr_operands. If *VAR_P is a GIMPLE register,
170 it will be added to the statement's real operands, otherwise it is
171 added to virtual operands. */
172 void add_stmt_operand (tree *var_p, int flags);
174 /* A subroutine of get_expr_operands to handle MEM_REF.
176 STMT is the statement being processed, EXPR is the MEM_REF
177 that got us here.
179 FLAGS is as in get_expr_operands. */
180 void get_mem_ref_operands (tree expr, int flags);
182 /* A subroutine of get_expr_operands to handle TARGET_MEM_REF. */
183 void get_tmr_operands (tree expr, int flags);
186 /* If STMT is a call that may clobber globals and other symbols that
187 escape, add them to the VDEF/VUSE lists for it. */
188 void maybe_add_call_vops (gcall *stmt);
190 /* Scan operands in the ASM_EXPR stmt referred to in INFO. */
191 void get_asm_stmt_operands (gasm *stmt);
194 /* Recursively scan the expression pointed to by EXPR_P in statement
195 STMT. FLAGS is one of the OPF_* constants modifying how to
196 interpret the operands found. */
197 void get_expr_operands (tree *expr_p, int flags);
199 /* Parse STMT looking for operands. When finished, the various
200 build_* operand vectors will have potential operands in them. */
201 void parse_ssa_operands ();
204 /* Takes elements from build_defs and turns them into def operands of STMT.
205 TODO -- Make build_defs vec of tree *. */
206 void finalize_ssa_defs ();
209 /* Accessor to tree-ssa-operands.cc caches. */
210 static inline struct ssa_operands *
211 gimple_ssa_operands (const struct function *fun)
213 return &fun->gimple_df->ssa_operands;
217 /* Return true if the SSA operands cache is active. */
219 bool
220 ssa_operands_active (struct function *fun)
222 if (fun == NULL)
223 return false;
225 return fun->gimple_df && gimple_ssa_operands (fun)->ops_active;
229 /* Create the VOP variable, an artificial global variable to act as a
230 representative of all of the virtual operands FUD chain. */
232 static void
233 create_vop_var (struct function *fn)
235 tree global_var;
237 gcc_assert (fn->gimple_df->vop == NULL_TREE);
239 global_var = build_decl (BUILTINS_LOCATION, VAR_DECL,
240 get_identifier (".MEM"),
241 void_type_node);
242 DECL_ARTIFICIAL (global_var) = 1;
243 DECL_IGNORED_P (global_var) = 1;
244 TREE_READONLY (global_var) = 0;
245 DECL_EXTERNAL (global_var) = 1;
246 TREE_STATIC (global_var) = 1;
247 TREE_USED (global_var) = 1;
248 DECL_CONTEXT (global_var) = NULL_TREE;
249 TREE_THIS_VOLATILE (global_var) = 0;
250 TREE_ADDRESSABLE (global_var) = 0;
251 VAR_DECL_IS_VIRTUAL_OPERAND (global_var) = 1;
253 fn->gimple_df->vop = global_var;
256 /* These are the sizes of the operand memory buffer in bytes which gets
257 allocated each time more operands space is required. The final value is
258 the amount that is allocated every time after that.
259 In 1k we can fit 25 use operands (or 63 def operands) on a host with
260 8 byte pointers, that would be 10 statements each with 1 def and 2
261 uses. */
263 #define OP_SIZE_INIT 0
264 #define OP_SIZE_1 (1024 - sizeof (void *))
265 #define OP_SIZE_2 (1024 * 4 - sizeof (void *))
266 #define OP_SIZE_3 (1024 * 16 - sizeof (void *))
268 /* Initialize the operand cache routines. */
270 void
271 init_ssa_operands (struct function *fn)
273 gcc_assert (gimple_ssa_operands (fn)->operand_memory == NULL);
274 gimple_ssa_operands (fn)->operand_memory_index
275 = gimple_ssa_operands (fn)->ssa_operand_mem_size;
276 gimple_ssa_operands (fn)->ops_active = true;
277 gimple_ssa_operands (fn)->ssa_operand_mem_size = OP_SIZE_INIT;
278 create_vop_var (fn);
282 /* Dispose of anything required by the operand routines. */
284 void
285 fini_ssa_operands (struct function *fn)
287 struct ssa_operand_memory_d *ptr;
289 gimple_ssa_operands (fn)->free_uses = NULL;
291 while ((ptr = gimple_ssa_operands (fn)->operand_memory) != NULL)
293 gimple_ssa_operands (fn)->operand_memory
294 = gimple_ssa_operands (fn)->operand_memory->next;
295 ggc_free (ptr);
298 gimple_ssa_operands (fn)->ops_active = false;
300 fn->gimple_df->vop = NULL_TREE;
304 /* Return memory for an operand of size SIZE. */
306 static inline void *
307 ssa_operand_alloc (struct function *fn, unsigned size)
309 char *ptr;
311 gcc_assert (size == sizeof (struct use_optype_d));
313 if (gimple_ssa_operands (fn)->operand_memory_index + size
314 >= gimple_ssa_operands (fn)->ssa_operand_mem_size)
316 struct ssa_operand_memory_d *ptr;
318 switch (gimple_ssa_operands (fn)->ssa_operand_mem_size)
320 case OP_SIZE_INIT:
321 gimple_ssa_operands (fn)->ssa_operand_mem_size = OP_SIZE_1;
322 break;
323 case OP_SIZE_1:
324 gimple_ssa_operands (fn)->ssa_operand_mem_size = OP_SIZE_2;
325 break;
326 case OP_SIZE_2:
327 case OP_SIZE_3:
328 gimple_ssa_operands (fn)->ssa_operand_mem_size = OP_SIZE_3;
329 break;
330 default:
331 gcc_unreachable ();
335 ptr = (ssa_operand_memory_d *) ggc_internal_alloc
336 (sizeof (void *) + gimple_ssa_operands (fn)->ssa_operand_mem_size);
338 ptr->next = gimple_ssa_operands (fn)->operand_memory;
339 gimple_ssa_operands (fn)->operand_memory = ptr;
340 gimple_ssa_operands (fn)->operand_memory_index = 0;
343 ptr = &(gimple_ssa_operands (fn)->operand_memory
344 ->mem[gimple_ssa_operands (fn)->operand_memory_index]);
345 gimple_ssa_operands (fn)->operand_memory_index += size;
346 return ptr;
350 /* Allocate a USE operand. */
352 static inline struct use_optype_d *
353 alloc_use (struct function *fn)
355 struct use_optype_d *ret;
356 if (gimple_ssa_operands (fn)->free_uses)
358 ret = gimple_ssa_operands (fn)->free_uses;
359 gimple_ssa_operands (fn)->free_uses
360 = gimple_ssa_operands (fn)->free_uses->next;
362 else
363 ret = (struct use_optype_d *)
364 ssa_operand_alloc (fn, sizeof (struct use_optype_d));
365 return ret;
369 /* Adds OP to the list of uses of statement STMT after LAST. */
371 static inline use_optype_p
372 add_use_op (struct function *fn, gimple *stmt, tree *op, use_optype_p last)
374 use_optype_p new_use;
376 new_use = alloc_use (fn);
377 USE_OP_PTR (new_use)->use = op;
378 link_imm_use_stmt (USE_OP_PTR (new_use), *op, stmt);
379 last->next = new_use;
380 new_use->next = NULL;
381 return new_use;
386 /* Takes elements from build_defs and turns them into def operands of STMT.
387 TODO -- Make build_defs vec of tree *. */
389 inline void
390 operands_scanner::finalize_ssa_defs ()
392 /* Pre-pend the vdef we may have built. */
393 if (build_vdef != NULL_TREE)
395 tree oldvdef = gimple_vdef (stmt);
396 if (oldvdef
397 && TREE_CODE (oldvdef) == SSA_NAME)
398 oldvdef = SSA_NAME_VAR (oldvdef);
399 if (oldvdef != build_vdef)
400 gimple_set_vdef (stmt, build_vdef);
403 /* Clear and unlink a no longer necessary VDEF. */
404 if (build_vdef == NULL_TREE
405 && gimple_vdef (stmt) != NULL_TREE)
407 if (TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
409 unlink_stmt_vdef (stmt);
410 release_ssa_name_fn (fn, gimple_vdef (stmt));
412 gimple_set_vdef (stmt, NULL_TREE);
415 /* If we have a non-SSA_NAME VDEF, mark it for renaming. */
416 if (gimple_vdef (stmt)
417 && TREE_CODE (gimple_vdef (stmt)) != SSA_NAME)
419 fn->gimple_df->rename_vops = 1;
420 fn->gimple_df->ssa_renaming_needed = 1;
425 /* Takes elements from build_uses and turns them into use operands of STMT. */
427 inline void
428 operands_scanner::finalize_ssa_uses ()
430 unsigned new_i;
431 struct use_optype_d new_list;
432 use_optype_p old_ops, ptr, last;
434 /* Pre-pend the VUSE we may have built. */
435 if (build_vuse != NULL_TREE)
437 tree oldvuse = gimple_vuse (stmt);
438 if (oldvuse
439 && TREE_CODE (oldvuse) == SSA_NAME)
440 oldvuse = SSA_NAME_VAR (oldvuse);
441 if (oldvuse != (build_vuse != NULL_TREE
442 ? build_vuse : build_vdef))
443 gimple_set_vuse (stmt, NULL_TREE);
444 build_uses.safe_insert (0, gimple_vuse_ptr (stmt));
447 new_list.next = NULL;
448 last = &new_list;
450 old_ops = gimple_use_ops (stmt);
452 /* Clear a no longer necessary VUSE. */
453 if (build_vuse == NULL_TREE
454 && gimple_vuse (stmt) != NULL_TREE)
455 gimple_set_vuse (stmt, NULL_TREE);
457 /* If there is anything in the old list, free it. */
458 if (old_ops)
460 for (ptr = old_ops; ptr->next; ptr = ptr->next)
461 delink_imm_use (USE_OP_PTR (ptr));
462 delink_imm_use (USE_OP_PTR (ptr));
463 ptr->next = gimple_ssa_operands (fn)->free_uses;
464 gimple_ssa_operands (fn)->free_uses = old_ops;
467 /* If we added a VUSE, make sure to set the operand if it is not already
468 present and mark it for renaming. */
469 if (build_vuse != NULL_TREE
470 && gimple_vuse (stmt) == NULL_TREE)
472 gimple_set_vuse (stmt, gimple_vop (fn));
473 fn->gimple_df->rename_vops = 1;
474 fn->gimple_df->ssa_renaming_needed = 1;
477 /* Now create nodes for all the new nodes. */
478 for (new_i = 0; new_i < build_uses.length (); new_i++)
480 tree *op = build_uses[new_i];
481 last = add_use_op (fn, stmt, op, last);
484 /* Now set the stmt's operands. */
485 gimple_set_use_ops (stmt, new_list.next);
489 /* Clear the in_list bits and empty the build array for VDEFs and
490 VUSEs. */
492 inline void
493 operands_scanner::cleanup_build_arrays ()
495 build_vdef = NULL_TREE;
496 build_vuse = NULL_TREE;
497 build_uses.truncate (0);
501 /* Finalize all the build vectors, fill the new ones into INFO. */
503 inline void
504 operands_scanner::finalize_ssa_stmt_operands ()
506 finalize_ssa_defs ();
507 finalize_ssa_uses ();
508 cleanup_build_arrays ();
512 /* Start the process of building up operands vectors in INFO. */
514 inline void
515 operands_scanner::start_ssa_stmt_operands ()
517 gcc_assert (build_uses.length () == 0);
518 gcc_assert (build_vuse == NULL_TREE);
519 gcc_assert (build_vdef == NULL_TREE);
523 /* Add USE_P to the list of pointers to operands. */
525 inline void
526 operands_scanner::append_use (tree *use_p)
528 build_uses.safe_push (use_p);
532 /* Add VAR to the set of variables that require a VDEF operator. */
534 inline void
535 operands_scanner::append_vdef (tree var)
537 gcc_assert ((build_vdef == NULL_TREE
538 || build_vdef == var)
539 && (build_vuse == NULL_TREE
540 || build_vuse == var));
542 build_vdef = var;
543 build_vuse = var;
547 /* Add VAR to the set of variables that require a VUSE operator. */
549 inline void
550 operands_scanner::append_vuse (tree var)
552 gcc_assert (build_vuse == NULL_TREE
553 || build_vuse == var);
555 build_vuse = var;
558 /* Add virtual operands for STMT. FLAGS is as in get_expr_operands. */
560 void
561 operands_scanner::add_virtual_operand (int flags)
563 /* Add virtual operands to the stmt, unless the caller has specifically
564 requested not to do that (used when adding operands inside an
565 ADDR_EXPR expression). */
566 if (flags & opf_no_vops)
567 return;
569 gcc_assert (!is_gimple_debug (stmt));
571 if (flags & opf_def)
572 append_vdef (gimple_vop (fn));
573 else
574 append_vuse (gimple_vop (fn));
578 /* Add *VAR_P to the appropriate operand array for statement STMT.
579 FLAGS is as in get_expr_operands. If *VAR_P is a GIMPLE register,
580 it will be added to the statement's real operands, otherwise it is
581 added to virtual operands. */
583 void
584 operands_scanner::add_stmt_operand (tree *var_p, int flags)
586 tree var = *var_p;
588 gcc_assert (SSA_VAR_P (*var_p)
589 || TREE_CODE (*var_p) == STRING_CST
590 || TREE_CODE (*var_p) == CONST_DECL);
592 if (is_gimple_reg (var))
594 /* The variable is a GIMPLE register. Add it to real operands. */
595 if (flags & opf_def)
597 else
598 append_use (var_p);
599 if (DECL_P (*var_p))
600 fn->gimple_df->ssa_renaming_needed = 1;
602 else
604 /* Mark statements with volatile operands. */
605 if (!(flags & opf_no_vops)
606 && TREE_THIS_VOLATILE (var))
607 gimple_set_has_volatile_ops (stmt, true);
609 /* The variable is a memory access. Add virtual operands. */
610 add_virtual_operand (flags);
614 /* Mark the base address of REF as having its address taken.
615 REF may be a single variable whose address has been taken or any
616 other valid GIMPLE memory reference (structure reference, array,
617 etc). */
619 static void
620 mark_address_taken (tree ref)
622 tree var;
624 /* Note that it is *NOT OKAY* to use the target of a COMPONENT_REF
625 as the only thing we take the address of. If VAR is a structure,
626 taking the address of a field means that the whole structure may
627 be referenced using pointer arithmetic. See PR 21407 and the
628 ensuing mailing list discussion. */
629 var = get_base_address (ref);
630 if (VAR_P (var)
631 || TREE_CODE (var) == RESULT_DECL
632 || TREE_CODE (var) == PARM_DECL)
633 TREE_ADDRESSABLE (var) = 1;
637 /* A subroutine of get_expr_operands to handle MEM_REF.
639 STMT is the statement being processed, EXPR is the MEM_REF
640 that got us here.
642 FLAGS is as in get_expr_operands. */
644 void
645 operands_scanner::get_mem_ref_operands (tree expr, int flags)
647 tree *pptr = &TREE_OPERAND (expr, 0);
649 if (!(flags & opf_no_vops)
650 && TREE_THIS_VOLATILE (expr))
651 gimple_set_has_volatile_ops (stmt, true);
653 /* Add the VOP. */
654 add_virtual_operand (flags);
656 /* If requested, add a USE operand for the base pointer. */
657 get_expr_operands (pptr,
658 opf_non_addressable | opf_use
659 | (flags & (opf_no_vops|opf_not_non_addressable)));
663 /* A subroutine of get_expr_operands to handle TARGET_MEM_REF. */
665 void
666 operands_scanner::get_tmr_operands(tree expr, int flags)
668 if (!(flags & opf_no_vops)
669 && TREE_THIS_VOLATILE (expr))
670 gimple_set_has_volatile_ops (stmt, true);
672 /* First record the real operands. */
673 get_expr_operands (&TMR_BASE (expr),
674 opf_non_addressable | opf_use
675 | (flags & (opf_no_vops|opf_not_non_addressable)));
676 get_expr_operands (&TMR_INDEX (expr), opf_use | (flags & opf_no_vops));
677 get_expr_operands (&TMR_INDEX2 (expr), opf_use | (flags & opf_no_vops));
679 add_virtual_operand (flags);
683 /* If STMT is a call that may clobber globals and other symbols that
684 escape, add them to the VDEF/VUSE lists for it. */
686 void
687 operands_scanner::maybe_add_call_vops (gcall *stmt)
689 int call_flags = gimple_call_flags (stmt);
691 /* If aliases have been computed already, add VDEF or VUSE
692 operands for all the symbols that have been found to be
693 call-clobbered. */
694 if (!(call_flags & ECF_NOVOPS))
696 /* A 'pure' or a 'const' function never call-clobbers anything. */
697 if (!(call_flags & (ECF_PURE | ECF_CONST)))
698 add_virtual_operand (opf_def);
699 else if (!(call_flags & ECF_CONST))
700 add_virtual_operand (opf_use);
705 /* Scan operands in the ASM_EXPR stmt referred to in INFO. */
707 void
708 operands_scanner::get_asm_stmt_operands (gasm *stmt)
710 size_t i, noutputs;
711 const char **oconstraints;
712 const char *constraint;
713 bool allows_mem, allows_reg, is_inout;
715 noutputs = gimple_asm_noutputs (stmt);
716 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
718 /* Gather all output operands. */
719 for (i = 0; i < gimple_asm_noutputs (stmt); i++)
721 tree link = gimple_asm_output_op (stmt, i);
722 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
723 oconstraints[i] = constraint;
724 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
725 &allows_reg, &is_inout);
727 /* This should have been split in gimplify_asm_expr. */
728 gcc_assert (!allows_reg || !is_inout);
730 /* Memory operands are addressable. Note that STMT needs the
731 address of this operand. */
732 if (!allows_reg && allows_mem)
733 mark_address_taken (TREE_VALUE (link));
735 get_expr_operands (&TREE_VALUE (link), opf_def | opf_not_non_addressable);
738 /* Gather all input operands. */
739 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
741 tree link = gimple_asm_input_op (stmt, i);
742 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
743 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
744 &allows_mem, &allows_reg);
746 /* Memory operands are addressable. Note that STMT needs the
747 address of this operand. */
748 if (!allows_reg && allows_mem)
749 mark_address_taken (TREE_VALUE (link));
751 get_expr_operands (&TREE_VALUE (link), opf_not_non_addressable);
754 /* Clobber all memory and addressable symbols for asm ("" : : : "memory"); */
755 if (gimple_asm_clobbers_memory_p (stmt))
756 add_virtual_operand (opf_def);
760 /* Recursively scan the expression pointed to by EXPR_P in statement
761 STMT. FLAGS is one of the OPF_* constants modifying how to
762 interpret the operands found. */
764 void
765 operands_scanner::get_expr_operands (tree *expr_p, int flags)
767 enum tree_code code;
768 enum tree_code_class codeclass;
769 tree expr = *expr_p;
770 int uflags = opf_use;
772 if (expr == NULL)
773 return;
775 if (is_gimple_debug (stmt))
776 uflags |= (flags & opf_no_vops);
778 code = TREE_CODE (expr);
779 codeclass = TREE_CODE_CLASS (code);
781 switch (code)
783 case ADDR_EXPR:
784 /* Taking the address of a variable does not represent a
785 reference to it, but the fact that the statement takes its
786 address will be of interest to some passes (e.g. alias
787 resolution). */
788 if ((!(flags & opf_non_addressable)
789 || (flags & opf_not_non_addressable))
790 && !is_gimple_debug (stmt))
791 mark_address_taken (TREE_OPERAND (expr, 0));
793 /* Otherwise, there may be variables referenced inside but there
794 should be no VUSEs created, since the referenced objects are
795 not really accessed. The only operands that we should find
796 here are ARRAY_REF indices which will always be real operands
797 (GIMPLE does not allow non-registers as array indices). */
798 flags |= opf_no_vops;
799 get_expr_operands (&TREE_OPERAND (expr, 0),
800 flags | opf_not_non_addressable | opf_address_taken);
801 return;
803 case SSA_NAME:
804 case VAR_DECL:
805 case PARM_DECL:
806 case RESULT_DECL:
807 case STRING_CST:
808 case CONST_DECL:
809 if (!(flags & opf_address_taken))
810 add_stmt_operand (expr_p, flags);
811 return;
813 case DEBUG_EXPR_DECL:
814 gcc_assert (gimple_debug_bind_p (stmt));
815 return;
817 case MEM_REF:
818 get_mem_ref_operands (expr, flags);
819 return;
821 case TARGET_MEM_REF:
822 get_tmr_operands (expr, flags);
823 return;
825 case ARRAY_REF:
826 case ARRAY_RANGE_REF:
827 case COMPONENT_REF:
828 case REALPART_EXPR:
829 case IMAGPART_EXPR:
831 if (!(flags & opf_no_vops)
832 && TREE_THIS_VOLATILE (expr))
833 gimple_set_has_volatile_ops (stmt, true);
835 get_expr_operands (&TREE_OPERAND (expr, 0), flags);
837 if (code == COMPONENT_REF)
838 get_expr_operands (&TREE_OPERAND (expr, 2), uflags);
839 else if (code == ARRAY_REF || code == ARRAY_RANGE_REF)
841 get_expr_operands (&TREE_OPERAND (expr, 1), uflags);
842 get_expr_operands (&TREE_OPERAND (expr, 2), uflags);
843 get_expr_operands (&TREE_OPERAND (expr, 3), uflags);
846 return;
849 case WITH_SIZE_EXPR:
850 /* WITH_SIZE_EXPR is a pass-through reference to its first argument,
851 and an rvalue reference to its second argument. */
852 get_expr_operands (&TREE_OPERAND (expr, 1), uflags);
853 get_expr_operands (&TREE_OPERAND (expr, 0), flags);
854 return;
856 case COND_EXPR:
857 case VEC_COND_EXPR:
858 case VEC_PERM_EXPR:
859 get_expr_operands (&TREE_OPERAND (expr, 0), uflags);
860 get_expr_operands (&TREE_OPERAND (expr, 1), uflags);
861 get_expr_operands (&TREE_OPERAND (expr, 2), uflags);
862 return;
864 case CONSTRUCTOR:
866 /* General aggregate CONSTRUCTORs have been decomposed, but they
867 are still in use as the COMPLEX_EXPR equivalent for vectors. */
868 constructor_elt *ce;
869 unsigned HOST_WIDE_INT idx;
871 /* A volatile constructor is actually TREE_CLOBBER_P, transfer
872 the volatility to the statement, don't use TREE_CLOBBER_P for
873 mirroring the other uses of THIS_VOLATILE in this file. */
874 if (!(flags & opf_no_vops)
875 && TREE_THIS_VOLATILE (expr))
876 gimple_set_has_volatile_ops (stmt, true);
878 for (idx = 0;
879 vec_safe_iterate (CONSTRUCTOR_ELTS (expr), idx, &ce);
880 idx++)
881 get_expr_operands (&ce->value, uflags);
883 return;
886 case BIT_FIELD_REF:
887 if (!(flags & opf_no_vops)
888 && TREE_THIS_VOLATILE (expr))
889 gimple_set_has_volatile_ops (stmt, true);
890 /* FALLTHRU */
892 case VIEW_CONVERT_EXPR:
893 do_unary:
894 get_expr_operands (&TREE_OPERAND (expr, 0), flags);
895 return;
897 case BIT_INSERT_EXPR:
898 case COMPOUND_EXPR:
899 case OBJ_TYPE_REF:
900 do_binary:
902 get_expr_operands (&TREE_OPERAND (expr, 0), flags);
903 get_expr_operands (&TREE_OPERAND (expr, 1), flags);
904 return;
907 case DOT_PROD_EXPR:
908 case SAD_EXPR:
909 case REALIGN_LOAD_EXPR:
910 case WIDEN_MULT_PLUS_EXPR:
911 case WIDEN_MULT_MINUS_EXPR:
913 get_expr_operands (&TREE_OPERAND (expr, 0), flags);
914 get_expr_operands (&TREE_OPERAND (expr, 1), flags);
915 get_expr_operands (&TREE_OPERAND (expr, 2), flags);
916 return;
919 case FUNCTION_DECL:
920 case LABEL_DECL:
921 case CASE_LABEL_EXPR:
922 /* Expressions that make no memory references. */
923 return;
925 default:
926 if (codeclass == tcc_unary)
927 goto do_unary;
928 if (codeclass == tcc_binary || codeclass == tcc_comparison)
929 goto do_binary;
930 if (codeclass == tcc_constant || codeclass == tcc_type)
931 return;
934 /* If we get here, something has gone wrong. */
935 if (flag_checking)
937 fprintf (stderr, "unhandled expression in get_expr_operands():\n");
938 debug_tree (expr);
939 fputs ("\n", stderr);
940 gcc_unreachable ();
945 /* Parse STMT looking for operands. When finished, the various
946 build_* operand vectors will have potential operands in them. */
948 void
949 operands_scanner::parse_ssa_operands ()
951 enum gimple_code code = gimple_code (stmt);
952 size_t i, n, start = 0;
954 switch (code)
956 case GIMPLE_ASM:
957 get_asm_stmt_operands (as_a <gasm *> (stmt));
958 break;
960 case GIMPLE_TRANSACTION:
961 /* The start of a transaction is a memory barrier. */
962 add_virtual_operand (opf_def | opf_use);
963 break;
965 case GIMPLE_DEBUG:
966 if (gimple_debug_bind_p (stmt)
967 && gimple_debug_bind_has_value_p (stmt))
968 get_expr_operands (gimple_debug_bind_get_value_ptr (stmt),
969 opf_use | opf_no_vops);
970 break;
972 case GIMPLE_RETURN:
973 append_vuse (gimple_vop (fn));
974 goto do_default;
976 case GIMPLE_CALL:
977 /* Add call-clobbered operands, if needed. */
978 maybe_add_call_vops (as_a <gcall *> (stmt));
979 /* FALLTHRU */
981 case GIMPLE_ASSIGN:
982 get_expr_operands (gimple_op_ptr (stmt, 0), opf_def);
983 start = 1;
984 /* FALLTHRU */
986 default:
987 do_default:
988 n = gimple_num_ops (stmt);
989 for (i = start; i < n; i++)
990 get_expr_operands (gimple_op_ptr (stmt, i), opf_use);
991 break;
996 /* Create an operands cache for STMT. */
998 void
999 operands_scanner::build_ssa_operands ()
1001 /* Initially assume that the statement has no volatile operands. */
1002 gimple_set_has_volatile_ops (stmt, false);
1004 start_ssa_stmt_operands ();
1005 parse_ssa_operands ();
1006 finalize_ssa_stmt_operands ();
1009 /* Verifies SSA statement operands. */
1011 DEBUG_FUNCTION bool
1012 operands_scanner::verify_ssa_operands ()
1014 use_operand_p use_p;
1015 def_operand_p def_p;
1016 ssa_op_iter iter;
1017 unsigned i;
1018 tree def;
1019 bool volatile_p = gimple_has_volatile_ops (stmt);
1021 /* build_ssa_operands w/o finalizing them. */
1022 gimple_set_has_volatile_ops (stmt, false);
1023 start_ssa_stmt_operands ();
1024 parse_ssa_operands ();
1026 /* Now verify the built operands are the same as present in STMT. */
1027 def = gimple_vdef (stmt);
1028 if (def
1029 && TREE_CODE (def) == SSA_NAME)
1030 def = SSA_NAME_VAR (def);
1031 if (build_vdef != def)
1033 error ("virtual definition of statement not up to date");
1034 return true;
1036 if (gimple_vdef (stmt)
1037 && ((def_p = gimple_vdef_op (stmt)) == NULL_DEF_OPERAND_P
1038 || DEF_FROM_PTR (def_p) != gimple_vdef (stmt)))
1040 error ("virtual def operand missing for statement");
1041 return true;
1044 tree use = gimple_vuse (stmt);
1045 if (use
1046 && TREE_CODE (use) == SSA_NAME)
1047 use = SSA_NAME_VAR (use);
1048 if (build_vuse != use)
1050 error ("virtual use of statement not up to date");
1051 return true;
1053 if (gimple_vuse (stmt)
1054 && ((use_p = gimple_vuse_op (stmt)) == NULL_USE_OPERAND_P
1055 || USE_FROM_PTR (use_p) != gimple_vuse (stmt)))
1057 error ("virtual use operand missing for statement");
1058 return true;
1061 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1063 tree *op;
1064 FOR_EACH_VEC_ELT (build_uses, i, op)
1066 if (use_p->use == op)
1068 build_uses[i] = NULL;
1069 break;
1072 if (i == build_uses.length ())
1074 error ("excess use operand for statement");
1075 debug_generic_expr (USE_FROM_PTR (use_p));
1076 return true;
1080 tree *op;
1081 FOR_EACH_VEC_ELT (build_uses, i, op)
1082 if (op != NULL)
1084 error ("use operand missing for statement");
1085 debug_generic_expr (*op);
1086 return true;
1089 if (gimple_has_volatile_ops (stmt) != volatile_p)
1091 error ("statement volatile flag not up to date");
1092 return true;
1095 cleanup_build_arrays ();
1096 return false;
1099 /* Interface for external use. */
1101 DEBUG_FUNCTION bool
1102 verify_ssa_operands (struct function *fn, gimple *stmt)
1104 return operands_scanner (fn, stmt).verify_ssa_operands ();
1108 /* Releases the operands of STMT back to their freelists, and clears
1109 the stmt operand lists. */
1111 void
1112 free_stmt_operands (struct function *fn, gimple *stmt)
1114 use_optype_p uses = gimple_use_ops (stmt), last_use;
1116 if (uses)
1118 for (last_use = uses; last_use->next; last_use = last_use->next)
1119 delink_imm_use (USE_OP_PTR (last_use));
1120 delink_imm_use (USE_OP_PTR (last_use));
1121 last_use->next = gimple_ssa_operands (fn)->free_uses;
1122 gimple_ssa_operands (fn)->free_uses = uses;
1123 gimple_set_use_ops (stmt, NULL);
1126 if (gimple_has_mem_ops (stmt))
1128 gimple_set_vuse (stmt, NULL_TREE);
1129 gimple_set_vdef (stmt, NULL_TREE);
1134 /* Get the operands of statement STMT. */
1136 void
1137 update_stmt_operands (struct function *fn, gimple *stmt)
1139 /* If update_stmt_operands is called before SSA is initialized, do
1140 nothing. */
1141 if (!ssa_operands_active (fn))
1142 return;
1144 timevar_push (TV_TREE_OPS);
1146 gcc_assert (gimple_modified_p (stmt));
1147 operands_scanner (fn, stmt).build_ssa_operands ();
1148 gimple_set_modified (stmt, false);
1149 // Inform the active range query an update has happened.
1150 get_range_query (fn)->update_stmt (stmt);
1152 timevar_pop (TV_TREE_OPS);
1156 /* Swap operands EXP0 and EXP1 in statement STMT. No attempt is done
1157 to test the validity of the swap operation. */
1159 void
1160 swap_ssa_operands (gimple *stmt, tree *exp0, tree *exp1)
1162 tree op0, op1;
1163 op0 = *exp0;
1164 op1 = *exp1;
1166 if (op0 != op1)
1168 /* Attempt to preserve the relative positions of these two operands in
1169 their * respective immediate use lists by adjusting their use pointer
1170 to point to the new operand position. */
1171 use_optype_p use0, use1, ptr;
1172 use0 = use1 = NULL;
1174 /* Find the 2 operands in the cache, if they are there. */
1175 for (ptr = gimple_use_ops (stmt); ptr; ptr = ptr->next)
1176 if (USE_OP_PTR (ptr)->use == exp0)
1178 use0 = ptr;
1179 break;
1182 for (ptr = gimple_use_ops (stmt); ptr; ptr = ptr->next)
1183 if (USE_OP_PTR (ptr)->use == exp1)
1185 use1 = ptr;
1186 break;
1189 /* And adjust their location to point to the new position of the
1190 operand. */
1191 if (use0)
1192 USE_OP_PTR (use0)->use = exp1;
1193 if (use1)
1194 USE_OP_PTR (use1)->use = exp0;
1196 /* Now swap the data. */
1197 *exp0 = op1;
1198 *exp1 = op0;
1203 /* Scan the immediate_use list for VAR making sure its linked properly.
1204 Return TRUE if there is a problem and emit an error message to F. */
1206 DEBUG_FUNCTION bool
1207 verify_imm_links (FILE *f, tree var)
1209 use_operand_p ptr, prev, list;
1210 unsigned int count;
1212 gcc_assert (TREE_CODE (var) == SSA_NAME);
1214 list = &(SSA_NAME_IMM_USE_NODE (var));
1215 gcc_assert (list->use == NULL);
1217 if (list->prev == NULL)
1219 gcc_assert (list->next == NULL);
1220 return false;
1223 prev = list;
1224 count = 0;
1225 for (ptr = list->next; ptr != list; )
1227 if (prev != ptr->prev)
1229 fprintf (f, "prev != ptr->prev\n");
1230 goto error;
1233 if (ptr->use == NULL)
1235 fprintf (f, "ptr->use == NULL\n");
1236 goto error; /* 2 roots, or SAFE guard node. */
1238 else if (*(ptr->use) != var)
1240 fprintf (f, "*(ptr->use) != var\n");
1241 goto error;
1244 prev = ptr;
1245 ptr = ptr->next;
1247 count++;
1248 if (count == 0)
1250 fprintf (f, "number of immediate uses doesn't fit unsigned int\n");
1251 goto error;
1255 /* Verify list in the other direction. */
1256 prev = list;
1257 for (ptr = list->prev; ptr != list; )
1259 if (prev != ptr->next)
1261 fprintf (f, "prev != ptr->next\n");
1262 goto error;
1264 prev = ptr;
1265 ptr = ptr->prev;
1266 if (count == 0)
1268 fprintf (f, "count-- < 0\n");
1269 goto error;
1271 count--;
1274 if (count != 0)
1276 fprintf (f, "count != 0\n");
1277 goto error;
1280 return false;
1282 error:
1283 if (ptr->loc.stmt && gimple_modified_p (ptr->loc.stmt))
1285 fprintf (f, " STMT MODIFIED. - <%p> ", (void *)ptr->loc.stmt);
1286 print_gimple_stmt (f, ptr->loc.stmt, 0, TDF_SLIM);
1288 fprintf (f, " IMM ERROR : (use_p : tree - %p:%p)", (void *)ptr,
1289 (void *)ptr->use);
1290 print_generic_expr (f, USE_FROM_PTR (ptr), TDF_SLIM);
1291 fprintf (f, "\n");
1292 return true;
1296 /* Dump all the immediate uses to FILE. */
1298 void
1299 dump_immediate_uses_for (FILE *file, tree var)
1301 imm_use_iterator iter;
1302 use_operand_p use_p;
1304 gcc_assert (var && TREE_CODE (var) == SSA_NAME);
1306 print_generic_expr (file, var, TDF_SLIM);
1307 fprintf (file, " : -->");
1308 if (has_zero_uses (var))
1309 fprintf (file, " no uses.\n");
1310 else
1311 if (has_single_use (var))
1312 fprintf (file, " single use.\n");
1313 else
1314 fprintf (file, "%d uses.\n", num_imm_uses (var));
1316 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
1318 if (use_p->loc.stmt == NULL && use_p->use == NULL)
1319 fprintf (file, "***end of stmt iterator marker***\n");
1320 else
1321 if (!is_gimple_reg (USE_FROM_PTR (use_p)))
1322 print_gimple_stmt (file, USE_STMT (use_p), 0, TDF_VOPS|TDF_MEMSYMS);
1323 else
1324 print_gimple_stmt (file, USE_STMT (use_p), 0, TDF_SLIM);
1326 fprintf (file, "\n");
1330 /* Dump all the immediate uses to FILE. */
1332 void
1333 dump_immediate_uses (FILE *file)
1335 tree var;
1336 unsigned int x;
1338 fprintf (file, "Immediate_uses: \n\n");
1339 FOR_EACH_SSA_NAME (x, var, cfun)
1341 dump_immediate_uses_for (file, var);
1346 /* Dump def-use edges on stderr. */
1348 DEBUG_FUNCTION void
1349 debug_immediate_uses (void)
1351 dump_immediate_uses (stderr);
1355 /* Dump def-use edges on stderr. */
1357 DEBUG_FUNCTION void
1358 debug_immediate_uses_for (tree var)
1360 dump_immediate_uses_for (stderr, var);
1364 /* Unlink STMTs virtual definition from the IL by propagating its use. */
1366 void
1367 unlink_stmt_vdef (gimple *stmt)
1369 use_operand_p use_p;
1370 imm_use_iterator iter;
1371 gimple *use_stmt;
1372 tree vdef = gimple_vdef (stmt);
1373 tree vuse = gimple_vuse (stmt);
1375 if (!vdef
1376 || TREE_CODE (vdef) != SSA_NAME)
1377 return;
1379 FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
1381 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1382 SET_USE (use_p, vuse);
1385 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vdef))
1386 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vuse) = 1;
1389 /* Return true if the var whose chain of uses starts at PTR has a
1390 single nondebug use. Set USE_P and STMT to that single nondebug
1391 use, if so, or to NULL otherwise. */
1392 bool
1393 single_imm_use_1 (const ssa_use_operand_t *head,
1394 use_operand_p *use_p, gimple **stmt)
1396 ssa_use_operand_t *ptr, *single_use = 0;
1398 for (ptr = head->next; ptr != head; ptr = ptr->next)
1399 if (USE_STMT(ptr) && !is_gimple_debug (USE_STMT (ptr)))
1401 if (single_use)
1403 single_use = NULL;
1404 break;
1406 single_use = ptr;
1409 if (use_p)
1410 *use_p = single_use;
1412 if (stmt)
1413 *stmt = single_use ? single_use->loc.stmt : NULL;
1415 return single_use;