1 /* SSA operands management for trees.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
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/>. */
22 #include "coretypes.h"
26 #include "print-tree.h"
32 #include "hard-reg-set.h"
35 #include "gimple-pretty-print.h"
37 #include "basic-block.h"
38 #include "tree-ssa-alias.h"
39 #include "internal-fn.h"
40 #include "gimple-expr.h"
43 #include "gimple-ssa.h"
44 #include "tree-phinodes.h"
45 #include "ssa-iterators.h"
46 #include "stringpool.h"
47 #include "tree-ssanames.h"
48 #include "tree-inline.h"
52 #include "langhooks.h"
53 #include "diagnostic-core.h"
56 /* This file contains the code required to manage the operands cache of the
57 SSA optimizer. For every stmt, we maintain an operand cache in the stmt
58 annotation. This cache contains operands that will be of interest to
59 optimizers and other passes wishing to manipulate the IL.
61 The operand type are broken up into REAL and VIRTUAL operands. The real
62 operands are represented as pointers into the stmt's operand tree. Thus
63 any manipulation of the real operands will be reflected in the actual tree.
64 Virtual operands are represented solely in the cache, although the base
65 variable for the SSA_NAME may, or may not occur in the stmt's tree.
66 Manipulation of the virtual operands will not be reflected in the stmt tree.
68 The routines in this file are concerned with creating this operand cache
71 The operand tree is the parsed by the various get_* routines which look
72 through the stmt tree for the occurrence of operands which may be of
73 interest, and calls are made to the append_* routines whenever one is
74 found. There are 4 of these routines, each representing one of the
75 4 types of operands. Defs, Uses, Virtual Uses, and Virtual May Defs.
77 The append_* routines check for duplication, and simply keep a list of
78 unique objects for each operand type in the build_* extendable vectors.
80 Once the stmt tree is completely parsed, the finalize_ssa_operands()
81 routine is called, which proceeds to perform the finalization routine
82 on each of the 4 operand vectors which have been built up.
84 If the stmt had a previous operand cache, the finalization routines
85 attempt to match up the new operands with the old ones. If it's a perfect
86 match, the old vector is simply reused. If it isn't a perfect match, then
87 a new vector is created and the new operands are placed there. For
88 virtual operands, if the previous cache had SSA_NAME version of a
89 variable, and that same variable occurs in the same operands cache, then
90 the new cache vector will also get the same SSA_NAME.
92 i.e., if a stmt had a VUSE of 'a_5', and 'a' occurs in the new
93 operand vector for VUSE, then the new vector will also be modified
94 such that it contains 'a_5' rather than 'a'. */
97 /* Flags to describe operand properties in helpers. */
99 /* By default, operands are loaded. */
102 /* Operand is the target of an assignment expression or a
103 call-clobbered variable. */
104 #define opf_def (1 << 0)
106 /* No virtual operands should be created in the expression. This is used
107 when traversing ADDR_EXPR nodes which have different semantics than
108 other expressions. Inside an ADDR_EXPR node, the only operands that we
109 need to consider are indices into arrays. For instance, &a.b[i] should
110 generate a USE of 'i' but it should not generate a VUSE for 'a' nor a
112 #define opf_no_vops (1 << 1)
114 /* Operand is in a place where address-taken does not imply addressable. */
115 #define opf_non_addressable (1 << 3)
117 /* Operand is in a place where opf_non_addressable does not apply. */
118 #define opf_not_non_addressable (1 << 4)
120 /* Operand is having its address taken. */
121 #define opf_address_taken (1 << 5)
123 /* Array for building all the use operands. */
124 static vec
<tree
> build_uses
;
126 /* The built VDEF operand. */
127 static tree build_vdef
;
129 /* The built VUSE operand. */
130 static tree build_vuse
;
132 /* Bitmap obstack for our datastructures that needs to survive across
133 compilations of multiple functions. */
134 static bitmap_obstack operands_bitmap_obstack
;
136 static void get_expr_operands (struct function
*, gimple
, tree
*, int);
138 /* Number of functions with initialized ssa_operands. */
139 static int n_initialized
= 0;
141 /* Accessor to tree-ssa-operands.c caches. */
142 static inline struct ssa_operands
*
143 gimple_ssa_operands (const struct function
*fun
)
145 return &fun
->gimple_df
->ssa_operands
;
149 /* Return true if the SSA operands cache is active. */
152 ssa_operands_active (struct function
*fun
)
157 return fun
->gimple_df
&& gimple_ssa_operands (fun
)->ops_active
;
161 /* Create the VOP variable, an artificial global variable to act as a
162 representative of all of the virtual operands FUD chain. */
165 create_vop_var (struct function
*fn
)
169 gcc_assert (fn
->gimple_df
->vop
== NULL_TREE
);
171 global_var
= build_decl (BUILTINS_LOCATION
, VAR_DECL
,
172 get_identifier (".MEM"),
174 DECL_ARTIFICIAL (global_var
) = 1;
175 DECL_IGNORED_P (global_var
) = 1;
176 TREE_READONLY (global_var
) = 0;
177 DECL_EXTERNAL (global_var
) = 1;
178 TREE_STATIC (global_var
) = 1;
179 TREE_USED (global_var
) = 1;
180 DECL_CONTEXT (global_var
) = NULL_TREE
;
181 TREE_THIS_VOLATILE (global_var
) = 0;
182 TREE_ADDRESSABLE (global_var
) = 0;
183 VAR_DECL_IS_VIRTUAL_OPERAND (global_var
) = 1;
185 fn
->gimple_df
->vop
= global_var
;
188 /* These are the sizes of the operand memory buffer in bytes which gets
189 allocated each time more operands space is required. The final value is
190 the amount that is allocated every time after that.
191 In 1k we can fit 25 use operands (or 63 def operands) on a host with
192 8 byte pointers, that would be 10 statements each with 1 def and 2
195 #define OP_SIZE_INIT 0
196 #define OP_SIZE_1 (1024 - sizeof (void *))
197 #define OP_SIZE_2 (1024 * 4 - sizeof (void *))
198 #define OP_SIZE_3 (1024 * 16 - sizeof (void *))
200 /* Initialize the operand cache routines. */
203 init_ssa_operands (struct function
*fn
)
205 if (!n_initialized
++)
207 build_uses
.create (10);
208 build_vuse
= NULL_TREE
;
209 build_vdef
= NULL_TREE
;
210 bitmap_obstack_initialize (&operands_bitmap_obstack
);
213 gcc_assert (gimple_ssa_operands (fn
)->operand_memory
== NULL
);
214 gimple_ssa_operands (fn
)->operand_memory_index
215 = gimple_ssa_operands (fn
)->ssa_operand_mem_size
;
216 gimple_ssa_operands (fn
)->ops_active
= true;
217 gimple_ssa_operands (fn
)->ssa_operand_mem_size
= OP_SIZE_INIT
;
222 /* Dispose of anything required by the operand routines. */
225 fini_ssa_operands (struct function
*fn
)
227 struct ssa_operand_memory_d
*ptr
;
229 if (!--n_initialized
)
231 build_uses
.release ();
232 build_vdef
= NULL_TREE
;
233 build_vuse
= NULL_TREE
;
236 gimple_ssa_operands (fn
)->free_uses
= NULL
;
238 while ((ptr
= gimple_ssa_operands (fn
)->operand_memory
) != NULL
)
240 gimple_ssa_operands (fn
)->operand_memory
241 = gimple_ssa_operands (fn
)->operand_memory
->next
;
245 gimple_ssa_operands (fn
)->ops_active
= false;
248 bitmap_obstack_release (&operands_bitmap_obstack
);
250 fn
->gimple_df
->vop
= NULL_TREE
;
254 /* Return memory for an operand of size SIZE. */
257 ssa_operand_alloc (struct function
*fn
, unsigned size
)
261 gcc_assert (size
== sizeof (struct use_optype_d
));
263 if (gimple_ssa_operands (fn
)->operand_memory_index
+ size
264 >= gimple_ssa_operands (fn
)->ssa_operand_mem_size
)
266 struct ssa_operand_memory_d
*ptr
;
268 switch (gimple_ssa_operands (fn
)->ssa_operand_mem_size
)
271 gimple_ssa_operands (fn
)->ssa_operand_mem_size
= OP_SIZE_1
;
274 gimple_ssa_operands (fn
)->ssa_operand_mem_size
= OP_SIZE_2
;
278 gimple_ssa_operands (fn
)->ssa_operand_mem_size
= OP_SIZE_3
;
285 ptr
= (ssa_operand_memory_d
*) ggc_internal_alloc
286 (sizeof (void *) + gimple_ssa_operands (fn
)->ssa_operand_mem_size
);
288 ptr
->next
= gimple_ssa_operands (fn
)->operand_memory
;
289 gimple_ssa_operands (fn
)->operand_memory
= ptr
;
290 gimple_ssa_operands (fn
)->operand_memory_index
= 0;
293 ptr
= &(gimple_ssa_operands (fn
)->operand_memory
294 ->mem
[gimple_ssa_operands (fn
)->operand_memory_index
]);
295 gimple_ssa_operands (fn
)->operand_memory_index
+= size
;
300 /* Allocate a USE operand. */
302 static inline struct use_optype_d
*
303 alloc_use (struct function
*fn
)
305 struct use_optype_d
*ret
;
306 if (gimple_ssa_operands (fn
)->free_uses
)
308 ret
= gimple_ssa_operands (fn
)->free_uses
;
309 gimple_ssa_operands (fn
)->free_uses
310 = gimple_ssa_operands (fn
)->free_uses
->next
;
313 ret
= (struct use_optype_d
*)
314 ssa_operand_alloc (fn
, sizeof (struct use_optype_d
));
319 /* Adds OP to the list of uses of statement STMT after LAST. */
321 static inline use_optype_p
322 add_use_op (struct function
*fn
, gimple stmt
, tree
*op
, use_optype_p last
)
324 use_optype_p new_use
;
326 new_use
= alloc_use (fn
);
327 USE_OP_PTR (new_use
)->use
= op
;
328 link_imm_use_stmt (USE_OP_PTR (new_use
), *op
, stmt
);
329 last
->next
= new_use
;
330 new_use
->next
= NULL
;
336 /* Takes elements from build_defs and turns them into def operands of STMT.
337 TODO -- Make build_defs vec of tree *. */
340 finalize_ssa_defs (struct function
*fn
, gimple stmt
)
342 /* Pre-pend the vdef we may have built. */
343 if (build_vdef
!= NULL_TREE
)
345 tree oldvdef
= gimple_vdef (stmt
);
347 && TREE_CODE (oldvdef
) == SSA_NAME
)
348 oldvdef
= SSA_NAME_VAR (oldvdef
);
349 if (oldvdef
!= build_vdef
)
350 gimple_set_vdef (stmt
, build_vdef
);
353 /* Clear and unlink a no longer necessary VDEF. */
354 if (build_vdef
== NULL_TREE
355 && gimple_vdef (stmt
) != NULL_TREE
)
357 if (TREE_CODE (gimple_vdef (stmt
)) == SSA_NAME
)
359 unlink_stmt_vdef (stmt
);
360 release_ssa_name_fn (fn
, gimple_vdef (stmt
));
362 gimple_set_vdef (stmt
, NULL_TREE
);
365 /* If we have a non-SSA_NAME VDEF, mark it for renaming. */
366 if (gimple_vdef (stmt
)
367 && TREE_CODE (gimple_vdef (stmt
)) != SSA_NAME
)
369 fn
->gimple_df
->rename_vops
= 1;
370 fn
->gimple_df
->ssa_renaming_needed
= 1;
375 /* Takes elements from build_uses and turns them into use operands of STMT.
376 TODO -- Make build_uses vec of tree *. */
379 finalize_ssa_uses (struct function
*fn
, gimple stmt
)
382 struct use_optype_d new_list
;
383 use_optype_p old_ops
, ptr
, last
;
385 /* Pre-pend the VUSE we may have built. */
386 if (build_vuse
!= NULL_TREE
)
388 tree oldvuse
= gimple_vuse (stmt
);
390 && TREE_CODE (oldvuse
) == SSA_NAME
)
391 oldvuse
= SSA_NAME_VAR (oldvuse
);
392 if (oldvuse
!= (build_vuse
!= NULL_TREE
393 ? build_vuse
: build_vdef
))
394 gimple_set_vuse (stmt
, NULL_TREE
);
395 build_uses
.safe_insert (0, (tree
)gimple_vuse_ptr (stmt
));
398 new_list
.next
= NULL
;
401 old_ops
= gimple_use_ops (stmt
);
403 /* Clear a no longer necessary VUSE. */
404 if (build_vuse
== NULL_TREE
405 && gimple_vuse (stmt
) != NULL_TREE
)
406 gimple_set_vuse (stmt
, NULL_TREE
);
408 /* If there is anything in the old list, free it. */
411 for (ptr
= old_ops
; ptr
; ptr
= ptr
->next
)
412 delink_imm_use (USE_OP_PTR (ptr
));
413 old_ops
->next
= gimple_ssa_operands (fn
)->free_uses
;
414 gimple_ssa_operands (fn
)->free_uses
= old_ops
;
417 /* If we added a VUSE, make sure to set the operand if it is not already
418 present and mark it for renaming. */
419 if (build_vuse
!= NULL_TREE
420 && gimple_vuse (stmt
) == NULL_TREE
)
422 gimple_set_vuse (stmt
, gimple_vop (fn
));
423 fn
->gimple_df
->rename_vops
= 1;
424 fn
->gimple_df
->ssa_renaming_needed
= 1;
427 /* Now create nodes for all the new nodes. */
428 for (new_i
= 0; new_i
< build_uses
.length (); new_i
++)
430 tree
*op
= (tree
*) build_uses
[new_i
];
431 last
= add_use_op (fn
, stmt
, op
, last
);
434 /* Now set the stmt's operands. */
435 gimple_set_use_ops (stmt
, new_list
.next
);
439 /* Clear the in_list bits and empty the build array for VDEFs and
443 cleanup_build_arrays (void)
445 build_vdef
= NULL_TREE
;
446 build_vuse
= NULL_TREE
;
447 build_uses
.truncate (0);
451 /* Finalize all the build vectors, fill the new ones into INFO. */
454 finalize_ssa_stmt_operands (struct function
*fn
, gimple stmt
)
456 finalize_ssa_defs (fn
, stmt
);
457 finalize_ssa_uses (fn
, stmt
);
458 cleanup_build_arrays ();
462 /* Start the process of building up operands vectors in INFO. */
465 start_ssa_stmt_operands (void)
467 gcc_assert (build_uses
.length () == 0);
468 gcc_assert (build_vuse
== NULL_TREE
);
469 gcc_assert (build_vdef
== NULL_TREE
);
473 /* Add USE_P to the list of pointers to operands. */
476 append_use (tree
*use_p
)
478 build_uses
.safe_push ((tree
) use_p
);
482 /* Add VAR to the set of variables that require a VDEF operator. */
485 append_vdef (tree var
)
487 gcc_assert ((build_vdef
== NULL_TREE
488 || build_vdef
== var
)
489 && (build_vuse
== NULL_TREE
490 || build_vuse
== var
));
497 /* Add VAR to the set of variables that require a VUSE operator. */
500 append_vuse (tree var
)
502 gcc_assert (build_vuse
== NULL_TREE
503 || build_vuse
== var
);
508 /* Add virtual operands for STMT. FLAGS is as in get_expr_operands. */
511 add_virtual_operand (struct function
*fn
,
512 gimple stmt ATTRIBUTE_UNUSED
, int flags
)
514 /* Add virtual operands to the stmt, unless the caller has specifically
515 requested not to do that (used when adding operands inside an
516 ADDR_EXPR expression). */
517 if (flags
& opf_no_vops
)
520 gcc_assert (!is_gimple_debug (stmt
));
523 append_vdef (gimple_vop (fn
));
525 append_vuse (gimple_vop (fn
));
529 /* Add *VAR_P to the appropriate operand array for statement STMT.
530 FLAGS is as in get_expr_operands. If *VAR_P is a GIMPLE register,
531 it will be added to the statement's real operands, otherwise it is
532 added to virtual operands. */
535 add_stmt_operand (struct function
*fn
, tree
*var_p
, gimple stmt
, int flags
)
539 gcc_assert (SSA_VAR_P (*var_p
));
541 if (is_gimple_reg (var
))
543 /* The variable is a GIMPLE register. Add it to real operands. */
549 fn
->gimple_df
->ssa_renaming_needed
= 1;
553 /* Mark statements with volatile operands. */
554 if (!(flags
& opf_no_vops
)
555 && TREE_THIS_VOLATILE (var
))
556 gimple_set_has_volatile_ops (stmt
, true);
558 /* The variable is a memory access. Add virtual operands. */
559 add_virtual_operand (fn
, stmt
, flags
);
563 /* Mark the base address of REF as having its address taken.
564 REF may be a single variable whose address has been taken or any
565 other valid GIMPLE memory reference (structure reference, array,
569 mark_address_taken (tree ref
)
573 /* Note that it is *NOT OKAY* to use the target of a COMPONENT_REF
574 as the only thing we take the address of. If VAR is a structure,
575 taking the address of a field means that the whole structure may
576 be referenced using pointer arithmetic. See PR 21407 and the
577 ensuing mailing list discussion. */
578 var
= get_base_address (ref
);
582 TREE_ADDRESSABLE (var
) = 1;
583 else if (TREE_CODE (var
) == MEM_REF
584 && TREE_CODE (TREE_OPERAND (var
, 0)) == ADDR_EXPR
585 && DECL_P (TREE_OPERAND (TREE_OPERAND (var
, 0), 0)))
586 TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (var
, 0), 0)) = 1;
591 /* A subroutine of get_expr_operands to handle MEM_REF.
593 STMT is the statement being processed, EXPR is the MEM_REF
596 FLAGS is as in get_expr_operands. */
599 get_mem_ref_operands (struct function
*fn
,
600 gimple stmt
, tree expr
, int flags
)
602 tree
*pptr
= &TREE_OPERAND (expr
, 0);
604 if (!(flags
& opf_no_vops
)
605 && TREE_THIS_VOLATILE (expr
))
606 gimple_set_has_volatile_ops (stmt
, true);
609 add_virtual_operand (fn
, stmt
, flags
);
611 /* If requested, add a USE operand for the base pointer. */
612 get_expr_operands (fn
, stmt
, pptr
,
613 opf_non_addressable
| opf_use
614 | (flags
& (opf_no_vops
|opf_not_non_addressable
)));
618 /* A subroutine of get_expr_operands to handle TARGET_MEM_REF. */
621 get_tmr_operands (struct function
*fn
, gimple stmt
, tree expr
, int flags
)
623 if (!(flags
& opf_no_vops
)
624 && TREE_THIS_VOLATILE (expr
))
625 gimple_set_has_volatile_ops (stmt
, true);
627 /* First record the real operands. */
628 get_expr_operands (fn
, stmt
,
629 &TMR_BASE (expr
), opf_use
| (flags
& opf_no_vops
));
630 get_expr_operands (fn
, stmt
,
631 &TMR_INDEX (expr
), opf_use
| (flags
& opf_no_vops
));
632 get_expr_operands (fn
, stmt
,
633 &TMR_INDEX2 (expr
), opf_use
| (flags
& opf_no_vops
));
635 add_virtual_operand (fn
, stmt
, flags
);
639 /* If STMT is a call that may clobber globals and other symbols that
640 escape, add them to the VDEF/VUSE lists for it. */
643 maybe_add_call_vops (struct function
*fn
, gimple stmt
)
645 int call_flags
= gimple_call_flags (stmt
);
647 /* If aliases have been computed already, add VDEF or VUSE
648 operands for all the symbols that have been found to be
650 if (!(call_flags
& ECF_NOVOPS
))
652 /* A 'pure' or a 'const' function never call-clobbers anything. */
653 if (!(call_flags
& (ECF_PURE
| ECF_CONST
)))
654 add_virtual_operand (fn
, stmt
, opf_def
);
655 else if (!(call_flags
& ECF_CONST
))
656 add_virtual_operand (fn
, stmt
, opf_use
);
661 /* Scan operands in the ASM_EXPR stmt referred to in INFO. */
664 get_asm_stmt_operands (struct function
*fn
, gimple stmt
)
667 const char **oconstraints
;
668 const char *constraint
;
669 bool allows_mem
, allows_reg
, is_inout
;
671 noutputs
= gimple_asm_noutputs (stmt
);
672 oconstraints
= (const char **) alloca ((noutputs
) * sizeof (const char *));
674 /* Gather all output operands. */
675 for (i
= 0; i
< gimple_asm_noutputs (stmt
); i
++)
677 tree link
= gimple_asm_output_op (stmt
, i
);
678 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
679 oconstraints
[i
] = constraint
;
680 parse_output_constraint (&constraint
, i
, 0, 0, &allows_mem
,
681 &allows_reg
, &is_inout
);
683 /* This should have been split in gimplify_asm_expr. */
684 gcc_assert (!allows_reg
|| !is_inout
);
686 /* Memory operands are addressable. Note that STMT needs the
687 address of this operand. */
688 if (!allows_reg
&& allows_mem
)
689 mark_address_taken (TREE_VALUE (link
));
691 get_expr_operands (fn
, stmt
,
692 &TREE_VALUE (link
), opf_def
| opf_not_non_addressable
);
695 /* Gather all input operands. */
696 for (i
= 0; i
< gimple_asm_ninputs (stmt
); i
++)
698 tree link
= gimple_asm_input_op (stmt
, i
);
699 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
700 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0, oconstraints
,
701 &allows_mem
, &allows_reg
);
703 /* Memory operands are addressable. Note that STMT needs the
704 address of this operand. */
705 if (!allows_reg
&& allows_mem
)
706 mark_address_taken (TREE_VALUE (link
));
708 get_expr_operands (fn
, stmt
, &TREE_VALUE (link
), opf_not_non_addressable
);
711 /* Clobber all memory and addressable symbols for asm ("" : : : "memory"); */
712 if (gimple_asm_clobbers_memory_p (stmt
))
713 add_virtual_operand (fn
, stmt
, opf_def
);
717 /* Recursively scan the expression pointed to by EXPR_P in statement
718 STMT. FLAGS is one of the OPF_* constants modifying how to
719 interpret the operands found. */
722 get_expr_operands (struct function
*fn
, gimple stmt
, tree
*expr_p
, int flags
)
725 enum tree_code_class codeclass
;
727 int uflags
= opf_use
;
732 if (is_gimple_debug (stmt
))
733 uflags
|= (flags
& opf_no_vops
);
735 code
= TREE_CODE (expr
);
736 codeclass
= TREE_CODE_CLASS (code
);
741 /* Taking the address of a variable does not represent a
742 reference to it, but the fact that the statement takes its
743 address will be of interest to some passes (e.g. alias
745 if ((!(flags
& opf_non_addressable
)
746 || (flags
& opf_not_non_addressable
))
747 && !is_gimple_debug (stmt
))
748 mark_address_taken (TREE_OPERAND (expr
, 0));
750 /* Otherwise, there may be variables referenced inside but there
751 should be no VUSEs created, since the referenced objects are
752 not really accessed. The only operands that we should find
753 here are ARRAY_REF indices which will always be real operands
754 (GIMPLE does not allow non-registers as array indices). */
755 flags
|= opf_no_vops
;
756 get_expr_operands (fn
, stmt
, &TREE_OPERAND (expr
, 0),
757 flags
| opf_not_non_addressable
| opf_address_taken
);
764 if (!(flags
& opf_address_taken
))
765 add_stmt_operand (fn
, expr_p
, stmt
, flags
);
768 case DEBUG_EXPR_DECL
:
769 gcc_assert (gimple_debug_bind_p (stmt
));
773 get_mem_ref_operands (fn
, stmt
, expr
, flags
);
777 get_tmr_operands (fn
, stmt
, expr
, flags
);
781 case ARRAY_RANGE_REF
:
786 if (!(flags
& opf_no_vops
)
787 && TREE_THIS_VOLATILE (expr
))
788 gimple_set_has_volatile_ops (stmt
, true);
790 get_expr_operands (fn
, stmt
, &TREE_OPERAND (expr
, 0), flags
);
792 if (code
== COMPONENT_REF
)
794 if (!(flags
& opf_no_vops
)
795 && TREE_THIS_VOLATILE (TREE_OPERAND (expr
, 1)))
796 gimple_set_has_volatile_ops (stmt
, true);
797 get_expr_operands (fn
, stmt
, &TREE_OPERAND (expr
, 2), uflags
);
799 else if (code
== ARRAY_REF
|| code
== ARRAY_RANGE_REF
)
801 get_expr_operands (fn
, stmt
, &TREE_OPERAND (expr
, 1), uflags
);
802 get_expr_operands (fn
, stmt
, &TREE_OPERAND (expr
, 2), uflags
);
803 get_expr_operands (fn
, stmt
, &TREE_OPERAND (expr
, 3), uflags
);
810 /* WITH_SIZE_EXPR is a pass-through reference to its first argument,
811 and an rvalue reference to its second argument. */
812 get_expr_operands (fn
, stmt
, &TREE_OPERAND (expr
, 1), uflags
);
813 get_expr_operands (fn
, stmt
, &TREE_OPERAND (expr
, 0), flags
);
819 get_expr_operands (fn
, stmt
, &TREE_OPERAND (expr
, 0), uflags
);
820 get_expr_operands (fn
, stmt
, &TREE_OPERAND (expr
, 1), uflags
);
821 get_expr_operands (fn
, stmt
, &TREE_OPERAND (expr
, 2), uflags
);
826 /* General aggregate CONSTRUCTORs have been decomposed, but they
827 are still in use as the COMPLEX_EXPR equivalent for vectors. */
829 unsigned HOST_WIDE_INT idx
;
831 /* A volatile constructor is actually TREE_CLOBBER_P, transfer
832 the volatility to the statement, don't use TREE_CLOBBER_P for
833 mirroring the other uses of THIS_VOLATILE in this file. */
834 if (!(flags
& opf_no_vops
)
835 && TREE_THIS_VOLATILE (expr
))
836 gimple_set_has_volatile_ops (stmt
, true);
839 vec_safe_iterate (CONSTRUCTOR_ELTS (expr
), idx
, &ce
);
841 get_expr_operands (fn
, stmt
, &ce
->value
, uflags
);
847 if (!(flags
& opf_no_vops
)
848 && TREE_THIS_VOLATILE (expr
))
849 gimple_set_has_volatile_ops (stmt
, true);
852 case VIEW_CONVERT_EXPR
:
854 get_expr_operands (fn
, stmt
, &TREE_OPERAND (expr
, 0), flags
);
862 get_expr_operands (fn
, stmt
, &TREE_OPERAND (expr
, 0), flags
);
863 get_expr_operands (fn
, stmt
, &TREE_OPERAND (expr
, 1), flags
);
869 case REALIGN_LOAD_EXPR
:
870 case WIDEN_MULT_PLUS_EXPR
:
871 case WIDEN_MULT_MINUS_EXPR
:
874 get_expr_operands (fn
, stmt
, &TREE_OPERAND (expr
, 0), flags
);
875 get_expr_operands (fn
, stmt
, &TREE_OPERAND (expr
, 1), flags
);
876 get_expr_operands (fn
, stmt
, &TREE_OPERAND (expr
, 2), flags
);
883 case CASE_LABEL_EXPR
:
884 /* Expressions that make no memory references. */
888 if (codeclass
== tcc_unary
)
890 if (codeclass
== tcc_binary
|| codeclass
== tcc_comparison
)
892 if (codeclass
== tcc_constant
|| codeclass
== tcc_type
)
896 /* If we get here, something has gone wrong. */
897 #ifdef ENABLE_CHECKING
898 fprintf (stderr
, "unhandled expression in get_expr_operands():\n");
900 fputs ("\n", stderr
);
906 /* Parse STMT looking for operands. When finished, the various
907 build_* operand vectors will have potential operands in them. */
910 parse_ssa_operands (struct function
*fn
, gimple stmt
)
912 enum gimple_code code
= gimple_code (stmt
);
913 size_t i
, n
, start
= 0;
918 get_asm_stmt_operands (fn
, stmt
);
921 case GIMPLE_TRANSACTION
:
922 /* The start of a transaction is a memory barrier. */
923 add_virtual_operand (fn
, stmt
, opf_def
| opf_use
);
927 if (gimple_debug_bind_p (stmt
)
928 && gimple_debug_bind_has_value_p (stmt
))
929 get_expr_operands (fn
, stmt
, gimple_debug_bind_get_value_ptr (stmt
),
930 opf_use
| opf_no_vops
);
934 append_vuse (gimple_vop (fn
));
938 /* Add call-clobbered operands, if needed. */
939 maybe_add_call_vops (fn
, stmt
);
943 get_expr_operands (fn
, stmt
, gimple_op_ptr (stmt
, 0), opf_def
);
949 n
= gimple_num_ops (stmt
);
950 for (i
= start
; i
< n
; i
++)
951 get_expr_operands (fn
, stmt
, gimple_op_ptr (stmt
, i
), opf_use
);
957 /* Create an operands cache for STMT. */
960 build_ssa_operands (struct function
*fn
, gimple stmt
)
962 /* Initially assume that the statement has no volatile operands. */
963 gimple_set_has_volatile_ops (stmt
, false);
965 start_ssa_stmt_operands ();
966 parse_ssa_operands (fn
, stmt
);
967 finalize_ssa_stmt_operands (fn
, stmt
);
970 /* Verifies SSA statement operands. */
973 verify_ssa_operands (struct function
*fn
, gimple stmt
)
980 bool volatile_p
= gimple_has_volatile_ops (stmt
);
982 /* build_ssa_operands w/o finalizing them. */
983 gimple_set_has_volatile_ops (stmt
, false);
984 start_ssa_stmt_operands ();
985 parse_ssa_operands (fn
, stmt
);
987 /* Now verify the built operands are the same as present in STMT. */
988 def
= gimple_vdef (stmt
);
990 && TREE_CODE (def
) == SSA_NAME
)
991 def
= SSA_NAME_VAR (def
);
992 if (build_vdef
!= def
)
994 error ("virtual definition of statement not up-to-date");
997 if (gimple_vdef (stmt
)
998 && ((def_p
= gimple_vdef_op (stmt
)) == NULL_DEF_OPERAND_P
999 || DEF_FROM_PTR (def_p
) != gimple_vdef (stmt
)))
1001 error ("virtual def operand missing for stmt");
1005 use
= gimple_vuse (stmt
);
1007 && TREE_CODE (use
) == SSA_NAME
)
1008 use
= SSA_NAME_VAR (use
);
1009 if (build_vuse
!= use
)
1011 error ("virtual use of statement not up-to-date");
1014 if (gimple_vuse (stmt
)
1015 && ((use_p
= gimple_vuse_op (stmt
)) == NULL_USE_OPERAND_P
1016 || USE_FROM_PTR (use_p
) != gimple_vuse (stmt
)))
1018 error ("virtual use operand missing for stmt");
1022 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
1024 FOR_EACH_VEC_ELT (build_uses
, i
, use
)
1026 if (use_p
->use
== (tree
*)use
)
1028 build_uses
[i
] = NULL_TREE
;
1032 if (i
== build_uses
.length ())
1034 error ("excess use operand for stmt");
1035 debug_generic_expr (USE_FROM_PTR (use_p
));
1039 FOR_EACH_VEC_ELT (build_uses
, i
, use
)
1040 if (use
!= NULL_TREE
)
1042 error ("use operand missing for stmt");
1043 debug_generic_expr (*(tree
*)use
);
1047 if (gimple_has_volatile_ops (stmt
) != volatile_p
)
1049 error ("stmt volatile flag not up-to-date");
1053 cleanup_build_arrays ();
1058 /* Releases the operands of STMT back to their freelists, and clears
1059 the stmt operand lists. */
1062 free_stmt_operands (struct function
*fn
, gimple stmt
)
1064 use_optype_p uses
= gimple_use_ops (stmt
), last_use
;
1068 for (last_use
= uses
; last_use
->next
; last_use
= last_use
->next
)
1069 delink_imm_use (USE_OP_PTR (last_use
));
1070 delink_imm_use (USE_OP_PTR (last_use
));
1071 last_use
->next
= gimple_ssa_operands (fn
)->free_uses
;
1072 gimple_ssa_operands (fn
)->free_uses
= uses
;
1073 gimple_set_use_ops (stmt
, NULL
);
1076 if (gimple_has_mem_ops (stmt
))
1078 gimple_set_vuse (stmt
, NULL_TREE
);
1079 gimple_set_vdef (stmt
, NULL_TREE
);
1084 /* Get the operands of statement STMT. */
1087 update_stmt_operands (struct function
*fn
, gimple stmt
)
1089 /* If update_stmt_operands is called before SSA is initialized, do
1091 if (!ssa_operands_active (fn
))
1094 timevar_push (TV_TREE_OPS
);
1096 gcc_assert (gimple_modified_p (stmt
));
1097 build_ssa_operands (fn
, stmt
);
1098 gimple_set_modified (stmt
, false);
1100 timevar_pop (TV_TREE_OPS
);
1104 /* Swap operands EXP0 and EXP1 in statement STMT. No attempt is done
1105 to test the validity of the swap operation. */
1108 swap_ssa_operands (gimple stmt
, tree
*exp0
, tree
*exp1
)
1116 /* Attempt to preserve the relative positions of these two operands in
1117 their * respective immediate use lists by adjusting their use pointer
1118 to point to the new operand position. */
1119 use_optype_p use0
, use1
, ptr
;
1122 /* Find the 2 operands in the cache, if they are there. */
1123 for (ptr
= gimple_use_ops (stmt
); ptr
; ptr
= ptr
->next
)
1124 if (USE_OP_PTR (ptr
)->use
== exp0
)
1130 for (ptr
= gimple_use_ops (stmt
); ptr
; ptr
= ptr
->next
)
1131 if (USE_OP_PTR (ptr
)->use
== exp1
)
1137 /* And adjust their location to point to the new position of the
1140 USE_OP_PTR (use0
)->use
= exp1
;
1142 USE_OP_PTR (use1
)->use
= exp0
;
1144 /* Now swap the data. */
1151 /* Scan the immediate_use list for VAR making sure its linked properly.
1152 Return TRUE if there is a problem and emit an error message to F. */
1155 verify_imm_links (FILE *f
, tree var
)
1157 use_operand_p ptr
, prev
, list
;
1160 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
1162 list
= &(SSA_NAME_IMM_USE_NODE (var
));
1163 gcc_assert (list
->use
== NULL
);
1165 if (list
->prev
== NULL
)
1167 gcc_assert (list
->next
== NULL
);
1173 for (ptr
= list
->next
; ptr
!= list
; )
1175 if (prev
!= ptr
->prev
)
1178 if (ptr
->use
== NULL
)
1179 goto error
; /* 2 roots, or SAFE guard node. */
1180 else if (*(ptr
->use
) != var
)
1186 /* Avoid infinite loops. 50,000,000 uses probably indicates a
1188 if (count
++ > 50000000)
1192 /* Verify list in the other direction. */
1194 for (ptr
= list
->prev
; ptr
!= list
; )
1196 if (prev
!= ptr
->next
)
1210 if (ptr
->loc
.stmt
&& gimple_modified_p (ptr
->loc
.stmt
))
1212 fprintf (f
, " STMT MODIFIED. - <%p> ", (void *)ptr
->loc
.stmt
);
1213 print_gimple_stmt (f
, ptr
->loc
.stmt
, 0, TDF_SLIM
);
1215 fprintf (f
, " IMM ERROR : (use_p : tree - %p:%p)", (void *)ptr
,
1217 print_generic_expr (f
, USE_FROM_PTR (ptr
), TDF_SLIM
);
1223 /* Dump all the immediate uses to FILE. */
1226 dump_immediate_uses_for (FILE *file
, tree var
)
1228 imm_use_iterator iter
;
1229 use_operand_p use_p
;
1231 gcc_assert (var
&& TREE_CODE (var
) == SSA_NAME
);
1233 print_generic_expr (file
, var
, TDF_SLIM
);
1234 fprintf (file
, " : -->");
1235 if (has_zero_uses (var
))
1236 fprintf (file
, " no uses.\n");
1238 if (has_single_use (var
))
1239 fprintf (file
, " single use.\n");
1241 fprintf (file
, "%d uses.\n", num_imm_uses (var
));
1243 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
1245 if (use_p
->loc
.stmt
== NULL
&& use_p
->use
== NULL
)
1246 fprintf (file
, "***end of stmt iterator marker***\n");
1248 if (!is_gimple_reg (USE_FROM_PTR (use_p
)))
1249 print_gimple_stmt (file
, USE_STMT (use_p
), 0, TDF_VOPS
|TDF_MEMSYMS
);
1251 print_gimple_stmt (file
, USE_STMT (use_p
), 0, TDF_SLIM
);
1253 fprintf (file
, "\n");
1257 /* Dump all the immediate uses to FILE. */
1260 dump_immediate_uses (FILE *file
)
1265 fprintf (file
, "Immediate_uses: \n\n");
1266 for (x
= 1; x
< num_ssa_names
; x
++)
1271 dump_immediate_uses_for (file
, var
);
1276 /* Dump def-use edges on stderr. */
1279 debug_immediate_uses (void)
1281 dump_immediate_uses (stderr
);
1285 /* Dump def-use edges on stderr. */
1288 debug_immediate_uses_for (tree var
)
1290 dump_immediate_uses_for (stderr
, var
);
1294 /* Unlink STMTs virtual definition from the IL by propagating its use. */
1297 unlink_stmt_vdef (gimple stmt
)
1299 use_operand_p use_p
;
1300 imm_use_iterator iter
;
1302 tree vdef
= gimple_vdef (stmt
);
1303 tree vuse
= gimple_vuse (stmt
);
1306 || TREE_CODE (vdef
) != SSA_NAME
)
1309 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, vdef
)
1311 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
1312 SET_USE (use_p
, vuse
);
1315 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vdef
))
1316 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vuse
) = 1;
1320 /* Return true if the var whose chain of uses starts at PTR has no
1323 has_zero_uses_1 (const ssa_use_operand_t
*head
)
1325 const ssa_use_operand_t
*ptr
;
1327 for (ptr
= head
->next
; ptr
!= head
; ptr
= ptr
->next
)
1328 if (!is_gimple_debug (USE_STMT (ptr
)))
1335 /* Return true if the var whose chain of uses starts at PTR has a
1336 single nondebug use. Set USE_P and STMT to that single nondebug
1337 use, if so, or to NULL otherwise. */
1339 single_imm_use_1 (const ssa_use_operand_t
*head
,
1340 use_operand_p
*use_p
, gimple
*stmt
)
1342 ssa_use_operand_t
*ptr
, *single_use
= 0;
1344 for (ptr
= head
->next
; ptr
!= head
; ptr
= ptr
->next
)
1345 if (!is_gimple_debug (USE_STMT (ptr
)))
1356 *use_p
= single_use
;
1359 *stmt
= single_use
? single_use
->loc
.stmt
: NULL
;