* Mainline merge as of 2006-02-16 (@111136).
[official-gcc.git] / gcc / tree-ssa-operands.c
blob70b4ea1097239320f244259b2917c40c5f2889a7
1 /* SSA operands management for trees.
2 Copyright (C) 2003, 2004, 2005, 2006 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 2, 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 COPYING. If not, write to
18 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "function.h"
28 #include "diagnostic.h"
29 #include "tree-flow.h"
30 #include "tree-inline.h"
31 #include "tree-pass.h"
32 #include "ggc.h"
33 #include "timevar.h"
34 #include "toplev.h"
35 #include "langhooks.h"
36 #include "ipa-reference.h"
38 /* This file contains the code required to manage the operands cache of the
39 SSA optimizer. For every stmt, we maintain an operand cache in the stmt
40 annotation. This cache contains operands that will be of interest to
41 optimizers and other passes wishing to manipulate the IL.
43 The operand type are broken up into REAL and VIRTUAL operands. The real
44 operands are represented as pointers into the stmt's operand tree. Thus
45 any manipulation of the real operands will be reflected in the actual tree.
46 Virtual operands are represented solely in the cache, although the base
47 variable for the SSA_NAME may, or may not occur in the stmt's tree.
48 Manipulation of the virtual operands will not be reflected in the stmt tree.
50 The routines in this file are concerned with creating this operand cache
51 from a stmt tree.
53 The operand tree is the parsed by the various get_* routines which look
54 through the stmt tree for the occurrence of operands which may be of
55 interest, and calls are made to the append_* routines whenever one is
56 found. There are 5 of these routines, each representing one of the
57 5 types of operands. Defs, Uses, Virtual Uses, Virtual May Defs, and
58 Virtual Must Defs.
60 The append_* routines check for duplication, and simply keep a list of
61 unique objects for each operand type in the build_* extendable vectors.
63 Once the stmt tree is completely parsed, the finalize_ssa_operands()
64 routine is called, which proceeds to perform the finalization routine
65 on each of the 5 operand vectors which have been built up.
67 If the stmt had a previous operand cache, the finalization routines
68 attempt to match up the new operands with the old ones. If it's a perfect
69 match, the old vector is simply reused. If it isn't a perfect match, then
70 a new vector is created and the new operands are placed there. For
71 virtual operands, if the previous cache had SSA_NAME version of a
72 variable, and that same variable occurs in the same operands cache, then
73 the new cache vector will also get the same SSA_NAME.
75 i.e., if a stmt had a VUSE of 'a_5', and 'a' occurs in the new operand
76 vector for VUSE, then the new vector will also be modified such that
77 it contains 'a_5' rather than 'a'.
82 /* Flags to describe operand properties in helpers. */
84 /* By default, operands are loaded. */
85 #define opf_none 0
87 /* Operand is the target of an assignment expression or a
88 call-clobbered variable */
89 #define opf_is_def (1 << 0)
91 /* Operand is the target of an assignment expression. */
92 #define opf_kill_def (1 << 1)
94 /* No virtual operands should be created in the expression. This is used
95 when traversing ADDR_EXPR nodes which have different semantics than
96 other expressions. Inside an ADDR_EXPR node, the only operands that we
97 need to consider are indices into arrays. For instance, &a.b[i] should
98 generate a USE of 'i' but it should not generate a VUSE for 'a' nor a
99 VUSE for 'b'. */
100 #define opf_no_vops (1 << 2)
102 /* Operand is a "non-specific" kill for call-clobbers and such. This is used
103 to distinguish "reset the world" events from explicit MODIFY_EXPRs. */
104 #define opf_non_specific (1 << 3)
107 /* Array for building all the def operands. */
108 static VEC(tree,heap) *build_defs;
110 /* Array for building all the use operands. */
111 static VEC(tree,heap) *build_uses;
113 /* Array for building all the v_may_def operands. */
114 static VEC(tree,heap) *build_v_may_defs;
116 /* Array for building all the vuse operands. */
117 static VEC(tree,heap) *build_vuses;
119 /* Array for building all the v_must_def operands. */
120 static VEC(tree,heap) *build_v_must_defs;
123 /* These arrays are the cached operand vectors for call clobbered calls. */
124 static bool ops_active = false;
126 static GTY (()) struct ssa_operand_memory_d *operand_memory = NULL;
127 static unsigned operand_memory_index;
129 static void get_expr_operands (tree, tree *, int);
130 static void get_asm_expr_operands (tree);
131 static void get_indirect_ref_operands (tree, tree, int, tree, HOST_WIDE_INT,
132 HOST_WIDE_INT, bool);
133 static void get_tmr_operands (tree, tree, int);
134 static void get_call_expr_operands (tree, tree);
135 static inline void append_def (tree *);
136 static inline void append_use (tree *);
137 static void append_v_may_def (tree);
138 static void append_v_must_def (tree);
139 static void add_call_clobber_ops (tree, tree);
140 static void add_call_read_ops (tree, tree);
141 static void add_stmt_operand (tree *, stmt_ann_t, int);
142 static void add_virtual_operand (tree, stmt_ann_t, int, tree,
143 HOST_WIDE_INT, HOST_WIDE_INT,
144 bool);
145 static void build_ssa_operands (tree stmt);
147 static def_optype_p free_defs = NULL;
148 static use_optype_p free_uses = NULL;
149 static vuse_optype_p free_vuses = NULL;
150 static maydef_optype_p free_maydefs = NULL;
151 static mustdef_optype_p free_mustdefs = NULL;
154 /* Return the DECL_UID of the base variable of T. */
156 static inline unsigned
157 get_name_decl (tree t)
159 if (TREE_CODE (t) != SSA_NAME)
160 return DECL_UID (t);
161 else
162 return DECL_UID (SSA_NAME_VAR (t));
165 /* Comparison function for qsort used in operand_build_sort_virtual. */
167 static int
168 operand_build_cmp (const void *p, const void *q)
170 tree e1 = *((const tree *)p);
171 tree e2 = *((const tree *)q);
172 unsigned int u1,u2;
174 u1 = get_name_decl (e1);
175 u2 = get_name_decl (e2);
177 /* We want to sort in ascending order. They can never be equal. */
178 #ifdef ENABLE_CHECKING
179 gcc_assert (u1 != u2);
180 #endif
181 return (u1 > u2 ? 1 : -1);
184 /* Sort the virtual operands in LIST from lowest DECL_UID to highest. */
186 static inline void
187 operand_build_sort_virtual (VEC(tree,heap) *list)
189 int num = VEC_length (tree, list);
190 if (num < 2)
191 return;
192 if (num == 2)
194 if (get_name_decl (VEC_index (tree, list, 0))
195 > get_name_decl (VEC_index (tree, list, 1)))
197 /* Swap elements if in the wrong order. */
198 tree tmp = VEC_index (tree, list, 0);
199 VEC_replace (tree, list, 0, VEC_index (tree, list, 1));
200 VEC_replace (tree, list, 1, tmp);
202 return;
204 /* There are 3 or more elements, call qsort. */
205 qsort (VEC_address (tree, list),
206 VEC_length (tree, list),
207 sizeof (tree),
208 operand_build_cmp);
213 /* Return true if the ssa operands cache is active. */
215 bool
216 ssa_operands_active (void)
218 return ops_active;
221 /* Structure storing statistics on how many call clobbers we have, and
222 how many where avoided. */
223 static struct
225 /* Number of call-clobbered ops we attempt to add to calls in
226 add_call_clobber_ops. */
227 unsigned int clobbered_vars;
229 /* Number of write-clobbers (v_may_defs) avoided by using
230 not_written information. */
231 unsigned int static_write_clobbers_avoided;
233 /* Number of reads (vuses) avoided by using not_read
234 information. */
235 unsigned int static_read_clobbers_avoided;
237 /* Number of write-clobbers avoided because the variable can't escape to
238 this call. */
239 unsigned int unescapable_clobbers_avoided;
241 /* Number of readonly uses we attempt to add to calls in
242 add_call_read_ops. */
243 unsigned int readonly_clobbers;
245 /* Number of readonly uses we avoid using not_read information. */
246 unsigned int static_readonly_clobbers_avoided;
247 } clobber_stats;
249 /* Initialize the operand cache routines. */
251 void
252 init_ssa_operands (void)
254 build_defs = VEC_alloc (tree, heap, 5);
255 build_uses = VEC_alloc (tree, heap, 10);
256 build_vuses = VEC_alloc (tree, heap, 25);
257 build_v_may_defs = VEC_alloc (tree, heap, 25);
258 build_v_must_defs = VEC_alloc (tree, heap, 25);
260 gcc_assert (operand_memory == NULL);
261 operand_memory_index = SSA_OPERAND_MEMORY_SIZE;
262 ops_active = true;
263 memset (&clobber_stats, 0, sizeof (clobber_stats));
268 /* Dispose of anything required by the operand routines. */
270 void
271 fini_ssa_operands (void)
273 struct ssa_operand_memory_d *ptr;
274 VEC_free (tree, heap, build_defs);
275 VEC_free (tree, heap, build_uses);
276 VEC_free (tree, heap, build_v_must_defs);
277 VEC_free (tree, heap, build_v_may_defs);
278 VEC_free (tree, heap, build_vuses);
279 free_defs = NULL;
280 free_uses = NULL;
281 free_vuses = NULL;
282 free_maydefs = NULL;
283 free_mustdefs = NULL;
284 while ((ptr = operand_memory) != NULL)
286 operand_memory = operand_memory->next;
287 ggc_free (ptr);
290 ops_active = false;
292 if (dump_file && (dump_flags & TDF_STATS))
294 fprintf (dump_file, "Original clobbered vars:%d\n", clobber_stats.clobbered_vars);
295 fprintf (dump_file, "Static write clobbers avoided:%d\n", clobber_stats.static_write_clobbers_avoided);
296 fprintf (dump_file, "Static read clobbers avoided:%d\n", clobber_stats.static_read_clobbers_avoided);
297 fprintf (dump_file, "Unescapable clobbers avoided:%d\n", clobber_stats.unescapable_clobbers_avoided);
298 fprintf (dump_file, "Original readonly clobbers:%d\n", clobber_stats.readonly_clobbers);
299 fprintf (dump_file, "Static readonly clobbers avoided:%d\n", clobber_stats.static_readonly_clobbers_avoided);
304 /* Return memory for operands of SIZE chunks. */
306 static inline void *
307 ssa_operand_alloc (unsigned size)
309 char *ptr;
310 if (operand_memory_index + size >= SSA_OPERAND_MEMORY_SIZE)
312 struct ssa_operand_memory_d *ptr;
313 ptr = GGC_NEW (struct ssa_operand_memory_d);
314 ptr->next = operand_memory;
315 operand_memory = ptr;
316 operand_memory_index = 0;
318 ptr = &(operand_memory->mem[operand_memory_index]);
319 operand_memory_index += size;
320 return ptr;
324 /* Make sure PTR is in the correct immediate use list. Since uses are simply
325 pointers into the stmt TREE, there is no way of telling if anyone has
326 changed what this pointer points to via TREE_OPERANDS (exp, 0) = <...>.
327 The contents are different, but the pointer is still the same. This
328 routine will check to make sure PTR is in the correct list, and if it isn't
329 put it in the correct list. We cannot simply check the previous node
330 because all nodes in the same stmt might have be changed. */
332 static inline void
333 correct_use_link (use_operand_p ptr, tree stmt)
335 use_operand_p prev;
336 tree root;
338 /* Fold_stmt () may have changed the stmt pointers. */
339 if (ptr->stmt != stmt)
340 ptr->stmt = stmt;
342 prev = ptr->prev;
343 if (prev)
345 /* Find the root element, making sure we skip any safe iterators. */
346 while (prev->use != NULL || prev->stmt == NULL)
347 prev = prev->prev;
349 /* Get the ssa_name of the list the node is in. */
350 root = prev->stmt;
351 /* If it's the right list, simply return. */
352 if (root == *(ptr->use))
353 return;
355 /* Its in the wrong list if we reach here. */
356 delink_imm_use (ptr);
357 link_imm_use (ptr, *(ptr->use));
361 /* This routine makes sure that PTR is in an immediate use list, and makes
362 sure the stmt pointer is set to the current stmt. Virtual uses do not need
363 the overhead of correct_use_link since they cannot be directly manipulated
364 like a real use can be. (They don't exist in the TREE_OPERAND nodes.) */
365 static inline void
366 set_virtual_use_link (use_operand_p ptr, tree stmt)
368 /* Fold_stmt () may have changed the stmt pointers. */
369 if (ptr->stmt != stmt)
370 ptr->stmt = stmt;
372 /* If this use isn't in a list, add it to the correct list. */
373 if (!ptr->prev)
374 link_imm_use (ptr, *(ptr->use));
379 #define FINALIZE_OPBUILD build_defs
380 #define FINALIZE_OPBUILD_BASE(I) (tree *)VEC_index (tree, \
381 build_defs, (I))
382 #define FINALIZE_OPBUILD_ELEM(I) (tree *)VEC_index (tree, \
383 build_defs, (I))
384 #define FINALIZE_FUNC finalize_ssa_def_ops
385 #define FINALIZE_ALLOC alloc_def
386 #define FINALIZE_FREE free_defs
387 #define FINALIZE_TYPE struct def_optype_d
388 #define FINALIZE_ELEM(PTR) ((PTR)->def_ptr)
389 #define FINALIZE_OPS DEF_OPS
390 #define FINALIZE_BASE(VAR) VAR
391 #define FINALIZE_BASE_TYPE tree *
392 #define FINALIZE_BASE_ZERO NULL
393 #define FINALIZE_INITIALIZE(PTR, VAL, STMT) FINALIZE_ELEM (PTR) = (VAL)
394 #include "tree-ssa-opfinalize.h"
397 /* This routine will create stmt operands for STMT from the def build list. */
399 static void
400 finalize_ssa_defs (tree stmt)
402 unsigned int num = VEC_length (tree, build_defs);
403 /* There should only be a single real definition per assignment. */
404 gcc_assert ((stmt && TREE_CODE (stmt) != MODIFY_EXPR) || num <= 1);
406 /* If there is an old list, often the new list is identical, or close, so
407 find the elements at the beginning that are the same as the vector. */
409 finalize_ssa_def_ops (stmt);
410 VEC_truncate (tree, build_defs, 0);
413 #define FINALIZE_OPBUILD build_uses
414 #define FINALIZE_OPBUILD_BASE(I) (tree *)VEC_index (tree, \
415 build_uses, (I))
416 #define FINALIZE_OPBUILD_ELEM(I) (tree *)VEC_index (tree, \
417 build_uses, (I))
418 #define FINALIZE_FUNC finalize_ssa_use_ops
419 #define FINALIZE_ALLOC alloc_use
420 #define FINALIZE_FREE free_uses
421 #define FINALIZE_TYPE struct use_optype_d
422 #define FINALIZE_ELEM(PTR) ((PTR)->use_ptr.use)
423 #define FINALIZE_OPS USE_OPS
424 #define FINALIZE_USE_PTR(PTR) USE_OP_PTR (PTR)
425 #define FINALIZE_CORRECT_USE correct_use_link
426 #define FINALIZE_BASE(VAR) VAR
427 #define FINALIZE_BASE_TYPE tree *
428 #define FINALIZE_BASE_ZERO NULL
429 #define FINALIZE_INITIALIZE(PTR, VAL, STMT) \
430 (PTR)->use_ptr.use = (VAL); \
431 link_imm_use_stmt (&((PTR)->use_ptr), \
432 *(VAL), (STMT))
433 #include "tree-ssa-opfinalize.h"
435 /* Return a new use operand vector for STMT, comparing to OLD_OPS_P. */
437 static void
438 finalize_ssa_uses (tree stmt)
440 #ifdef ENABLE_CHECKING
442 unsigned x;
443 unsigned num = VEC_length (tree, build_uses);
445 /* If the pointer to the operand is the statement itself, something is
446 wrong. It means that we are pointing to a local variable (the
447 initial call to get_stmt_operands does not pass a pointer to a
448 statement). */
449 for (x = 0; x < num; x++)
450 gcc_assert (*((tree *)VEC_index (tree, build_uses, x)) != stmt);
452 #endif
453 finalize_ssa_use_ops (stmt);
454 VEC_truncate (tree, build_uses, 0);
458 /* Return a new v_may_def operand vector for STMT, comparing to OLD_OPS_P. */
459 #define FINALIZE_OPBUILD build_v_may_defs
460 #define FINALIZE_OPBUILD_ELEM(I) VEC_index (tree, build_v_may_defs, (I))
461 #define FINALIZE_OPBUILD_BASE(I) get_name_decl (VEC_index (tree, \
462 build_v_may_defs, (I)))
463 #define FINALIZE_FUNC finalize_ssa_v_may_def_ops
464 #define FINALIZE_ALLOC alloc_maydef
465 #define FINALIZE_FREE free_maydefs
466 #define FINALIZE_TYPE struct maydef_optype_d
467 #define FINALIZE_ELEM(PTR) MAYDEF_RESULT (PTR)
468 #define FINALIZE_OPS MAYDEF_OPS
469 #define FINALIZE_USE_PTR(PTR) MAYDEF_OP_PTR (PTR)
470 #define FINALIZE_CORRECT_USE set_virtual_use_link
471 #define FINALIZE_BASE_ZERO 0
472 #define FINALIZE_BASE(VAR) get_name_decl (VAR)
473 #define FINALIZE_BASE_TYPE unsigned
474 #define FINALIZE_INITIALIZE(PTR, VAL, STMT) \
475 (PTR)->def_var = (VAL); \
476 (PTR)->use_var = (VAL); \
477 (PTR)->use_ptr.use = &((PTR)->use_var); \
478 link_imm_use_stmt (&((PTR)->use_ptr), \
479 (VAL), (STMT))
480 #include "tree-ssa-opfinalize.h"
483 static void
484 finalize_ssa_v_may_defs (tree stmt)
486 finalize_ssa_v_may_def_ops (stmt);
490 /* Clear the in_list bits and empty the build array for v_may_defs. */
492 static inline void
493 cleanup_v_may_defs (void)
495 unsigned x, num;
496 num = VEC_length (tree, build_v_may_defs);
498 for (x = 0; x < num; x++)
500 tree t = VEC_index (tree, build_v_may_defs, x);
501 if (TREE_CODE (t) != SSA_NAME)
503 var_ann_t ann = var_ann (t);
504 ann->in_v_may_def_list = 0;
507 VEC_truncate (tree, build_v_may_defs, 0);
511 #define FINALIZE_OPBUILD build_vuses
512 #define FINALIZE_OPBUILD_ELEM(I) VEC_index (tree, build_vuses, (I))
513 #define FINALIZE_OPBUILD_BASE(I) get_name_decl (VEC_index (tree, \
514 build_vuses, (I)))
515 #define FINALIZE_FUNC finalize_ssa_vuse_ops
516 #define FINALIZE_ALLOC alloc_vuse
517 #define FINALIZE_FREE free_vuses
518 #define FINALIZE_TYPE struct vuse_optype_d
519 #define FINALIZE_ELEM(PTR) VUSE_OP (PTR)
520 #define FINALIZE_OPS VUSE_OPS
521 #define FINALIZE_USE_PTR(PTR) VUSE_OP_PTR (PTR)
522 #define FINALIZE_CORRECT_USE set_virtual_use_link
523 #define FINALIZE_BASE_ZERO 0
524 #define FINALIZE_BASE(VAR) get_name_decl (VAR)
525 #define FINALIZE_BASE_TYPE unsigned
526 #define FINALIZE_INITIALIZE(PTR, VAL, STMT) \
527 (PTR)->use_var = (VAL); \
528 (PTR)->use_ptr.use = &((PTR)->use_var); \
529 link_imm_use_stmt (&((PTR)->use_ptr), \
530 (VAL), (STMT))
531 #include "tree-ssa-opfinalize.h"
534 /* Return a new vuse operand vector, comparing to OLD_OPS_P. */
536 static void
537 finalize_ssa_vuses (tree stmt)
539 unsigned num, num_v_may_defs;
540 unsigned vuse_index;
542 /* Remove superfluous VUSE operands. If the statement already has a
543 V_MAY_DEF operation for a variable 'a', then a VUSE for 'a' is not
544 needed because V_MAY_DEFs imply a VUSE of the variable. For instance,
545 suppose that variable 'a' is aliased:
547 # VUSE <a_2>
548 # a_3 = V_MAY_DEF <a_2>
549 a = a + 1;
551 The VUSE <a_2> is superfluous because it is implied by the V_MAY_DEF
552 operation. */
554 num = VEC_length (tree, build_vuses);
555 num_v_may_defs = VEC_length (tree, build_v_may_defs);
557 if (num > 0 && num_v_may_defs > 0)
559 for (vuse_index = 0; vuse_index < VEC_length (tree, build_vuses); )
561 tree vuse;
562 vuse = VEC_index (tree, build_vuses, vuse_index);
563 if (TREE_CODE (vuse) != SSA_NAME)
565 var_ann_t ann = var_ann (vuse);
566 ann->in_vuse_list = 0;
567 if (ann->in_v_may_def_list)
569 VEC_ordered_remove (tree, build_vuses, vuse_index);
570 continue;
573 vuse_index++;
576 else
577 /* Clear out the in_list bits. */
578 for (vuse_index = 0;
579 vuse_index < VEC_length (tree, build_vuses);
580 vuse_index++)
582 tree t = VEC_index (tree, build_vuses, vuse_index);
583 if (TREE_CODE (t) != SSA_NAME)
585 var_ann_t ann = var_ann (t);
586 ann->in_vuse_list = 0;
590 finalize_ssa_vuse_ops (stmt);
591 /* The v_may_def build vector wasn't cleaned up because we needed it. */
592 cleanup_v_may_defs ();
594 /* Free the vuses build vector. */
595 VEC_truncate (tree, build_vuses, 0);
599 /* Return a new v_must_def operand vector for STMT, comparing to OLD_OPS_P. */
601 #define FINALIZE_OPBUILD build_v_must_defs
602 #define FINALIZE_OPBUILD_ELEM(I) VEC_index (tree, build_v_must_defs, (I))
603 #define FINALIZE_OPBUILD_BASE(I) get_name_decl (VEC_index (tree, \
604 build_v_must_defs, (I)))
605 #define FINALIZE_FUNC finalize_ssa_v_must_def_ops
606 #define FINALIZE_ALLOC alloc_mustdef
607 #define FINALIZE_FREE free_mustdefs
608 #define FINALIZE_TYPE struct mustdef_optype_d
609 #define FINALIZE_ELEM(PTR) MUSTDEF_RESULT (PTR)
610 #define FINALIZE_OPS MUSTDEF_OPS
611 #define FINALIZE_USE_PTR(PTR) MUSTDEF_KILL_PTR (PTR)
612 #define FINALIZE_CORRECT_USE set_virtual_use_link
613 #define FINALIZE_BASE_ZERO 0
614 #define FINALIZE_BASE(VAR) get_name_decl (VAR)
615 #define FINALIZE_BASE_TYPE unsigned
616 #define FINALIZE_INITIALIZE(PTR, VAL, STMT) \
617 (PTR)->def_var = (VAL); \
618 (PTR)->kill_var = (VAL); \
619 (PTR)->use_ptr.use = &((PTR)->kill_var);\
620 link_imm_use_stmt (&((PTR)->use_ptr), \
621 (VAL), (STMT))
622 #include "tree-ssa-opfinalize.h"
625 static void
626 finalize_ssa_v_must_defs (tree stmt)
628 /* In the presence of subvars, there may be more than one V_MUST_DEF per
629 statement (one for each subvar). It is a bit expensive to verify that
630 all must-defs in a statement belong to subvars if there is more than one
631 MUST-def, so we don't do it. Suffice to say, if you reach here without
632 having subvars, and have num >1, you have hit a bug. */
634 finalize_ssa_v_must_def_ops (stmt);
635 VEC_truncate (tree, build_v_must_defs, 0);
639 /* Finalize all the build vectors, fill the new ones into INFO. */
641 static inline void
642 finalize_ssa_stmt_operands (tree stmt)
644 finalize_ssa_defs (stmt);
645 finalize_ssa_uses (stmt);
646 finalize_ssa_v_must_defs (stmt);
647 finalize_ssa_v_may_defs (stmt);
648 finalize_ssa_vuses (stmt);
652 /* Start the process of building up operands vectors in INFO. */
654 static inline void
655 start_ssa_stmt_operands (void)
657 gcc_assert (VEC_length (tree, build_defs) == 0);
658 gcc_assert (VEC_length (tree, build_uses) == 0);
659 gcc_assert (VEC_length (tree, build_vuses) == 0);
660 gcc_assert (VEC_length (tree, build_v_may_defs) == 0);
661 gcc_assert (VEC_length (tree, build_v_must_defs) == 0);
665 /* Add DEF_P to the list of pointers to operands. */
667 static inline void
668 append_def (tree *def_p)
670 VEC_safe_push (tree, heap, build_defs, (tree)def_p);
674 /* Add USE_P to the list of pointers to operands. */
676 static inline void
677 append_use (tree *use_p)
679 VEC_safe_push (tree, heap, build_uses, (tree)use_p);
683 /* Add a new virtual may def for variable VAR to the build array. */
685 static inline void
686 append_v_may_def (tree var)
688 if (TREE_CODE (var) != SSA_NAME)
690 var_ann_t ann = get_var_ann (var);
692 /* Don't allow duplicate entries. */
693 if (ann->in_v_may_def_list)
694 return;
695 ann->in_v_may_def_list = 1;
698 VEC_safe_push (tree, heap, build_v_may_defs, (tree)var);
702 /* Add VAR to the list of virtual uses. */
704 static inline void
705 append_vuse (tree var)
708 /* Don't allow duplicate entries. */
709 if (TREE_CODE (var) != SSA_NAME)
711 var_ann_t ann = get_var_ann (var);
713 if (ann->in_vuse_list || ann->in_v_may_def_list)
714 return;
715 ann->in_vuse_list = 1;
718 VEC_safe_push (tree, heap, build_vuses, (tree)var);
722 /* Add VAR to the list of virtual must definitions for INFO. */
724 static inline void
725 append_v_must_def (tree var)
727 unsigned i;
729 /* Don't allow duplicate entries. */
730 for (i = 0; i < VEC_length (tree, build_v_must_defs); i++)
731 if (var == VEC_index (tree, build_v_must_defs, i))
732 return;
734 VEC_safe_push (tree, heap, build_v_must_defs, (tree)var);
738 /* Parse STMT looking for operands. OLD_OPS is the original stmt operand
739 cache for STMT, if it existed before. When finished, the various build_*
740 operand vectors will have potential operands. in them. */
742 static void
743 parse_ssa_operands (tree stmt)
745 enum tree_code code;
747 code = TREE_CODE (stmt);
748 switch (code)
750 case MODIFY_EXPR:
751 /* First get operands from the RHS. For the LHS, we use a V_MAY_DEF if
752 either only part of LHS is modified or if the RHS might throw,
753 otherwise, use V_MUST_DEF.
755 ??? If it might throw, we should represent somehow that it is killed
756 on the fallthrough path. */
758 tree lhs = TREE_OPERAND (stmt, 0);
759 int lhs_flags = opf_is_def;
761 get_expr_operands (stmt, &TREE_OPERAND (stmt, 1), opf_none);
763 /* If the LHS is a VIEW_CONVERT_EXPR, it isn't changing whether
764 or not the entire LHS is modified; that depends on what's
765 inside the VIEW_CONVERT_EXPR. */
766 if (TREE_CODE (lhs) == VIEW_CONVERT_EXPR)
767 lhs = TREE_OPERAND (lhs, 0);
769 if (TREE_CODE (lhs) != ARRAY_RANGE_REF
770 && TREE_CODE (lhs) != BIT_FIELD_REF)
771 lhs_flags |= opf_kill_def;
773 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), lhs_flags);
775 break;
777 case COND_EXPR:
778 get_expr_operands (stmt, &COND_EXPR_COND (stmt), opf_none);
779 break;
781 case SWITCH_EXPR:
782 get_expr_operands (stmt, &SWITCH_COND (stmt), opf_none);
783 break;
785 case ASM_EXPR:
786 get_asm_expr_operands (stmt);
787 break;
789 case RETURN_EXPR:
790 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), opf_none);
791 break;
793 case GOTO_EXPR:
794 get_expr_operands (stmt, &GOTO_DESTINATION (stmt), opf_none);
795 break;
797 case LABEL_EXPR:
798 get_expr_operands (stmt, &LABEL_EXPR_LABEL (stmt), opf_none);
799 break;
801 /* These nodes contain no variable references. */
802 case BIND_EXPR:
803 case CASE_LABEL_EXPR:
804 case TRY_CATCH_EXPR:
805 case TRY_FINALLY_EXPR:
806 case EH_FILTER_EXPR:
807 case CATCH_EXPR:
808 case RESX_EXPR:
809 break;
811 default:
812 /* Notice that if get_expr_operands tries to use &STMT as the operand
813 pointer (which may only happen for USE operands), we will fail in
814 append_use. This default will handle statements like empty
815 statements, or CALL_EXPRs that may appear on the RHS of a statement
816 or as statements themselves. */
817 get_expr_operands (stmt, &stmt, opf_none);
818 break;
822 /* Create an operands cache for STMT. */
824 static void
825 build_ssa_operands (tree stmt)
827 stmt_ann_t ann = get_stmt_ann (stmt);
829 /* Initially assume that the statement has no volatile operands. */
830 if (ann)
831 ann->has_volatile_ops = false;
833 start_ssa_stmt_operands ();
835 parse_ssa_operands (stmt);
836 operand_build_sort_virtual (build_vuses);
837 operand_build_sort_virtual (build_v_may_defs);
838 operand_build_sort_virtual (build_v_must_defs);
840 finalize_ssa_stmt_operands (stmt);
844 /* Free any operands vectors in OPS. */
845 void
846 free_ssa_operands (stmt_operands_p ops)
848 ops->def_ops = NULL;
849 ops->use_ops = NULL;
850 ops->maydef_ops = NULL;
851 ops->mustdef_ops = NULL;
852 ops->vuse_ops = NULL;
856 /* Get the operands of statement STMT. Note that repeated calls to
857 get_stmt_operands for the same statement will do nothing until the
858 statement is marked modified by a call to mark_stmt_modified(). */
860 void
861 update_stmt_operands (tree stmt)
863 stmt_ann_t ann = get_stmt_ann (stmt);
864 /* If get_stmt_operands is called before SSA is initialized, dont
865 do anything. */
866 if (!ssa_operands_active ())
867 return;
868 /* The optimizers cannot handle statements that are nothing but a
869 _DECL. This indicates a bug in the gimplifier. */
870 gcc_assert (!SSA_VAR_P (stmt));
872 gcc_assert (ann->modified);
874 timevar_push (TV_TREE_OPS);
876 build_ssa_operands (stmt);
878 /* Clear the modified bit for STMT. Subsequent calls to
879 get_stmt_operands for this statement will do nothing until the
880 statement is marked modified by a call to mark_stmt_modified(). */
881 ann->modified = 0;
883 timevar_pop (TV_TREE_OPS);
887 /* Copies virtual operands from SRC to DST. */
889 void
890 copy_virtual_operands (tree dest, tree src)
892 tree t;
893 ssa_op_iter iter, old_iter;
894 use_operand_p use_p, u2;
895 def_operand_p def_p, d2;
897 build_ssa_operands (dest);
899 /* Copy all the virtual fields. */
900 FOR_EACH_SSA_TREE_OPERAND (t, src, iter, SSA_OP_VUSE)
901 append_vuse (t);
902 FOR_EACH_SSA_TREE_OPERAND (t, src, iter, SSA_OP_VMAYDEF)
903 append_v_may_def (t);
904 FOR_EACH_SSA_TREE_OPERAND (t, src, iter, SSA_OP_VMUSTDEF)
905 append_v_must_def (t);
907 if (VEC_length (tree, build_vuses) == 0
908 && VEC_length (tree, build_v_may_defs) == 0
909 && VEC_length (tree, build_v_must_defs) == 0)
910 return;
912 /* Now commit the virtual operands to this stmt. */
913 finalize_ssa_v_must_defs (dest);
914 finalize_ssa_v_may_defs (dest);
915 finalize_ssa_vuses (dest);
917 /* Finally, set the field to the same values as then originals. */
920 t = op_iter_init_tree (&old_iter, src, SSA_OP_VUSE);
921 FOR_EACH_SSA_USE_OPERAND (use_p, dest, iter, SSA_OP_VUSE)
923 gcc_assert (!op_iter_done (&old_iter));
924 SET_USE (use_p, t);
925 t = op_iter_next_tree (&old_iter);
927 gcc_assert (op_iter_done (&old_iter));
929 op_iter_init_maydef (&old_iter, src, &u2, &d2);
930 FOR_EACH_SSA_MAYDEF_OPERAND (def_p, use_p, dest, iter)
932 gcc_assert (!op_iter_done (&old_iter));
933 SET_USE (use_p, USE_FROM_PTR (u2));
934 SET_DEF (def_p, DEF_FROM_PTR (d2));
935 op_iter_next_maymustdef (&u2, &d2, &old_iter);
937 gcc_assert (op_iter_done (&old_iter));
939 op_iter_init_mustdef (&old_iter, src, &u2, &d2);
940 FOR_EACH_SSA_MUSTDEF_OPERAND (def_p, use_p, dest, iter)
942 gcc_assert (!op_iter_done (&old_iter));
943 SET_USE (use_p, USE_FROM_PTR (u2));
944 SET_DEF (def_p, DEF_FROM_PTR (d2));
945 op_iter_next_maymustdef (&u2, &d2, &old_iter);
947 gcc_assert (op_iter_done (&old_iter));
952 /* Specifically for use in DOM's expression analysis. Given a store, we
953 create an artificial stmt which looks like a load from the store, this can
954 be used to eliminate redundant loads. OLD_OPS are the operands from the
955 store stmt, and NEW_STMT is the new load which represents a load of the
956 values stored. */
958 void
959 create_ssa_artficial_load_stmt (tree new_stmt, tree old_stmt)
961 stmt_ann_t ann;
962 tree op;
963 ssa_op_iter iter;
964 use_operand_p use_p;
965 unsigned x;
967 ann = get_stmt_ann (new_stmt);
969 /* process the stmt looking for operands. */
970 start_ssa_stmt_operands ();
971 parse_ssa_operands (new_stmt);
973 for (x = 0; x < VEC_length (tree, build_vuses); x++)
975 tree t = VEC_index (tree, build_vuses, x);
976 if (TREE_CODE (t) != SSA_NAME)
978 var_ann_t ann = var_ann (t);
979 ann->in_vuse_list = 0;
983 for (x = 0; x < VEC_length (tree, build_v_may_defs); x++)
985 tree t = VEC_index (tree, build_v_may_defs, x);
986 if (TREE_CODE (t) != SSA_NAME)
988 var_ann_t ann = var_ann (t);
989 ann->in_v_may_def_list = 0;
992 /* Remove any virtual operands that were found. */
993 VEC_truncate (tree, build_v_may_defs, 0);
994 VEC_truncate (tree, build_v_must_defs, 0);
995 VEC_truncate (tree, build_vuses, 0);
997 /* For each VDEF on the original statement, we want to create a
998 VUSE of the V_MAY_DEF result or V_MUST_DEF op on the new
999 statement. */
1000 FOR_EACH_SSA_TREE_OPERAND (op, old_stmt, iter,
1001 (SSA_OP_VMAYDEF | SSA_OP_VMUSTDEF))
1002 append_vuse (op);
1004 /* Now build the operands for this new stmt. */
1005 finalize_ssa_stmt_operands (new_stmt);
1007 /* All uses in this fake stmt must not be in the immediate use lists. */
1008 FOR_EACH_SSA_USE_OPERAND (use_p, new_stmt, iter, SSA_OP_ALL_USES)
1009 delink_imm_use (use_p);
1012 void
1013 swap_tree_operands (tree stmt, tree *exp0, tree *exp1)
1015 tree op0, op1;
1016 op0 = *exp0;
1017 op1 = *exp1;
1019 /* If the operand cache is active, attempt to preserve the relative positions
1020 of these two operands in their respective immediate use lists. */
1021 if (ssa_operands_active () && op0 != op1)
1023 use_optype_p use0, use1, ptr;
1024 use0 = use1 = NULL;
1025 /* Find the 2 operands in the cache, if they are there. */
1026 for (ptr = USE_OPS (stmt); ptr; ptr = ptr->next)
1027 if (USE_OP_PTR (ptr)->use == exp0)
1029 use0 = ptr;
1030 break;
1032 for (ptr = USE_OPS (stmt); ptr; ptr = ptr->next)
1033 if (USE_OP_PTR (ptr)->use == exp1)
1035 use1 = ptr;
1036 break;
1038 /* If both uses don't have operand entries, there isn't much we can do
1039 at this point. Presumably we dont need to worry about it. */
1040 if (use0 && use1)
1042 tree *tmp = USE_OP_PTR (use1)->use;
1043 USE_OP_PTR (use1)->use = USE_OP_PTR (use0)->use;
1044 USE_OP_PTR (use0)->use = tmp;
1048 /* Now swap the data. */
1049 *exp0 = op1;
1050 *exp1 = op0;
1053 /* Recursively scan the expression pointed to by EXPR_P in statement referred
1054 to by INFO. FLAGS is one of the OPF_* constants modifying how to interpret
1055 the operands found. */
1057 static void
1058 get_expr_operands (tree stmt, tree *expr_p, int flags)
1060 enum tree_code code;
1061 enum tree_code_class class;
1062 tree expr = *expr_p;
1063 stmt_ann_t s_ann = stmt_ann (stmt);
1065 if (expr == NULL)
1066 return;
1068 code = TREE_CODE (expr);
1069 class = TREE_CODE_CLASS (code);
1071 switch (code)
1073 case ADDR_EXPR:
1074 /* Taking the address of a variable does not represent a
1075 reference to it, but the fact that the stmt takes its address will be
1076 of interest to some passes (e.g. alias resolution). */
1077 add_to_addressable_set (TREE_OPERAND (expr, 0),
1078 &s_ann->addresses_taken);
1080 /* If the address is invariant, there may be no interesting variable
1081 references inside. */
1082 if (is_gimple_min_invariant (expr))
1083 return;
1085 /* There should be no VUSEs created, since the referenced objects are
1086 not really accessed. The only operands that we should find here
1087 are ARRAY_REF indices which will always be real operands (GIMPLE
1088 does not allow non-registers as array indices). */
1089 flags |= opf_no_vops;
1091 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1092 return;
1094 case SSA_NAME:
1095 case STRUCT_FIELD_TAG:
1096 case TYPE_MEMORY_TAG:
1097 case NAME_MEMORY_TAG:
1099 add_stmt_operand (expr_p, s_ann, flags);
1100 return;
1102 case VAR_DECL:
1103 case PARM_DECL:
1104 case RESULT_DECL:
1106 subvar_t svars;
1108 /* Add the subvars for a variable if it has subvars, to DEFS or USES.
1109 Otherwise, add the variable itself.
1110 Whether it goes to USES or DEFS depends on the operand flags. */
1111 if (var_can_have_subvars (expr)
1112 && (svars = get_subvars_for_var (expr)))
1114 subvar_t sv;
1115 for (sv = svars; sv; sv = sv->next)
1116 add_stmt_operand (&sv->var, s_ann, flags);
1118 else
1120 add_stmt_operand (expr_p, s_ann, flags);
1122 return;
1124 case MISALIGNED_INDIRECT_REF:
1125 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1126 /* fall through */
1128 case ALIGN_INDIRECT_REF:
1129 case INDIRECT_REF:
1130 get_indirect_ref_operands (stmt, expr, flags, NULL_TREE,
1131 0, -1, true);
1132 return;
1134 case TARGET_MEM_REF:
1135 get_tmr_operands (stmt, expr, flags);
1136 return;
1138 case ARRAY_RANGE_REF:
1139 /* Treat array references as references to the virtual variable
1140 representing the array. The virtual variable for an ARRAY_REF
1141 is the VAR_DECL for the array. */
1143 /* Add the virtual variable for the ARRAY_REF to VDEFS or VUSES
1144 according to the value of IS_DEF. */
1145 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1146 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1147 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1148 get_expr_operands (stmt, &TREE_OPERAND (expr, 3), opf_none);
1149 return;
1151 case ARRAY_REF:
1152 case COMPONENT_REF:
1153 case REALPART_EXPR:
1154 case IMAGPART_EXPR:
1156 tree ref;
1157 HOST_WIDE_INT offset, size, maxsize;
1158 bool none = true;
1159 /* This component ref becomes an access to all of the subvariables
1160 it can touch, if we can determine that, but *NOT* the real one.
1161 If we can't determine which fields we could touch, the recursion
1162 will eventually get to a variable and add *all* of its subvars, or
1163 whatever is the minimum correct subset. */
1165 ref = get_ref_base_and_extent (expr, &offset, &size, &maxsize);
1166 if (SSA_VAR_P (ref) && get_subvars_for_var (ref))
1168 subvar_t svars = get_subvars_for_var (ref);
1169 subvar_t sv;
1170 for (sv = svars; sv; sv = sv->next)
1172 bool exact;
1173 if (overlap_subvar (offset, maxsize, sv->var, &exact))
1175 int subvar_flags = flags;
1176 none = false;
1177 if (!exact
1178 || size != maxsize)
1179 subvar_flags &= ~opf_kill_def;
1180 add_stmt_operand (&sv->var, s_ann, subvar_flags);
1183 if (!none)
1184 flags |= opf_no_vops;
1186 else if (TREE_CODE (ref) == INDIRECT_REF)
1188 get_indirect_ref_operands (stmt, ref, flags, expr,
1189 offset, maxsize, false);
1190 flags |= opf_no_vops;
1193 /* Even if we found subvars above we need to ensure to see
1194 immediate uses for d in s.a[d]. In case of s.a having
1195 a subvar we'd miss it otherwise. */
1196 get_expr_operands (stmt, &TREE_OPERAND (expr, 0),
1197 flags & ~opf_kill_def);
1199 if (code == COMPONENT_REF)
1201 if (s_ann && TREE_THIS_VOLATILE (TREE_OPERAND (expr, 1)))
1202 s_ann->has_volatile_ops = true;
1203 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1205 else if (code == ARRAY_REF)
1207 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1208 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1209 get_expr_operands (stmt, &TREE_OPERAND (expr, 3), opf_none);
1211 return;
1213 case WITH_SIZE_EXPR:
1214 /* WITH_SIZE_EXPR is a pass-through reference to its first argument,
1215 and an rvalue reference to its second argument. */
1216 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1217 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1218 return;
1220 case CALL_EXPR:
1221 get_call_expr_operands (stmt, expr);
1222 return;
1224 case COND_EXPR:
1225 case VEC_COND_EXPR:
1226 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_none);
1227 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1228 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1229 return;
1231 case MODIFY_EXPR:
1233 int subflags;
1234 tree op;
1236 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1238 op = TREE_OPERAND (expr, 0);
1239 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1240 op = TREE_OPERAND (expr, 0);
1241 if (TREE_CODE (op) == ARRAY_RANGE_REF
1242 || TREE_CODE (op) == REALPART_EXPR
1243 || TREE_CODE (op) == IMAGPART_EXPR)
1244 subflags = opf_is_def;
1245 else
1246 subflags = opf_is_def | opf_kill_def;
1248 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), subflags);
1249 return;
1252 case CONSTRUCTOR:
1254 /* General aggregate CONSTRUCTORs have been decomposed, but they
1255 are still in use as the COMPLEX_EXPR equivalent for vectors. */
1256 constructor_elt *ce;
1257 unsigned HOST_WIDE_INT idx;
1259 for (idx = 0;
1260 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (expr), idx, ce);
1261 idx++)
1262 get_expr_operands (stmt, &ce->value, opf_none);
1264 return;
1267 case TRUTH_NOT_EXPR:
1268 case BIT_FIELD_REF:
1269 case VIEW_CONVERT_EXPR:
1270 do_unary:
1271 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1272 return;
1274 case TRUTH_AND_EXPR:
1275 case TRUTH_OR_EXPR:
1276 case TRUTH_XOR_EXPR:
1277 case COMPOUND_EXPR:
1278 case OBJ_TYPE_REF:
1279 case ASSERT_EXPR:
1280 do_binary:
1282 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1283 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1284 return;
1287 case DOT_PROD_EXPR:
1288 case REALIGN_LOAD_EXPR:
1290 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1291 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1292 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), flags);
1293 return;
1296 case BLOCK:
1297 case FUNCTION_DECL:
1298 case EXC_PTR_EXPR:
1299 case FILTER_EXPR:
1300 case LABEL_DECL:
1301 case CONST_DECL:
1302 case OMP_PARALLEL:
1303 case OMP_SECTIONS:
1304 case OMP_FOR:
1305 case OMP_RETURN_EXPR:
1306 case OMP_SINGLE:
1307 case OMP_MASTER:
1308 case OMP_ORDERED:
1309 case OMP_CRITICAL:
1310 /* Expressions that make no memory references. */
1311 return;
1313 default:
1314 if (class == tcc_unary)
1315 goto do_unary;
1316 if (class == tcc_binary || class == tcc_comparison)
1317 goto do_binary;
1318 if (class == tcc_constant || class == tcc_type)
1319 return;
1322 /* If we get here, something has gone wrong. */
1323 #ifdef ENABLE_CHECKING
1324 fprintf (stderr, "unhandled expression in get_expr_operands():\n");
1325 debug_tree (expr);
1326 fputs ("\n", stderr);
1327 internal_error ("internal error");
1328 #endif
1329 gcc_unreachable ();
1333 /* Scan operands in the ASM_EXPR stmt referred to in INFO. */
1335 static void
1336 get_asm_expr_operands (tree stmt)
1338 stmt_ann_t s_ann = stmt_ann (stmt);
1339 int noutputs = list_length (ASM_OUTPUTS (stmt));
1340 const char **oconstraints
1341 = (const char **) alloca ((noutputs) * sizeof (const char *));
1342 int i;
1343 tree link;
1344 const char *constraint;
1345 bool allows_mem, allows_reg, is_inout;
1347 for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
1349 oconstraints[i] = constraint
1350 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1351 parse_output_constraint (&constraint, i, 0, 0,
1352 &allows_mem, &allows_reg, &is_inout);
1354 /* This should have been split in gimplify_asm_expr. */
1355 gcc_assert (!allows_reg || !is_inout);
1357 /* Memory operands are addressable. Note that STMT needs the
1358 address of this operand. */
1359 if (!allows_reg && allows_mem)
1361 tree t = get_base_address (TREE_VALUE (link));
1362 if (t && DECL_P (t) && s_ann)
1363 add_to_addressable_set (t, &s_ann->addresses_taken);
1366 get_expr_operands (stmt, &TREE_VALUE (link), opf_is_def);
1369 for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
1371 constraint
1372 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1373 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1374 oconstraints, &allows_mem, &allows_reg);
1376 /* Memory operands are addressable. Note that STMT needs the
1377 address of this operand. */
1378 if (!allows_reg && allows_mem)
1380 tree t = get_base_address (TREE_VALUE (link));
1381 if (t && DECL_P (t) && s_ann)
1382 add_to_addressable_set (t, &s_ann->addresses_taken);
1385 get_expr_operands (stmt, &TREE_VALUE (link), 0);
1389 /* Clobber memory for asm ("" : : : "memory"); */
1390 for (link = ASM_CLOBBERS (stmt); link; link = TREE_CHAIN (link))
1391 if (strcmp (TREE_STRING_POINTER (TREE_VALUE (link)), "memory") == 0)
1393 unsigned i;
1394 bitmap_iterator bi;
1396 /* Clobber all call-clobbered variables (or .GLOBAL_VAR if we
1397 decided to group them). */
1398 if (global_var)
1399 add_stmt_operand (&global_var, s_ann, opf_is_def);
1400 else
1401 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1403 tree var = referenced_var (i);
1404 add_stmt_operand (&var, s_ann, opf_is_def | opf_non_specific);
1407 /* Now clobber all addressables. */
1408 EXECUTE_IF_SET_IN_BITMAP (addressable_vars, 0, i, bi)
1410 tree var = referenced_var (i);
1412 /* Subvars are explicitly represented in this list, so
1413 we don't need the original to be added to the clobber
1414 ops, but the original *will* be in this list because
1415 we keep the addressability of the original
1416 variable up-to-date so we don't screw up the rest of
1417 the backend. */
1418 if (var_can_have_subvars (var)
1419 && get_subvars_for_var (var) != NULL)
1420 continue;
1422 add_stmt_operand (&var, s_ann, opf_is_def | opf_non_specific);
1425 break;
1429 /* A subroutine of get_expr_operands to handle INDIRECT_REF,
1430 ALIGN_INDIRECT_REF and MISALIGNED_INDIRECT_REF.
1431 STMT is the statement being processed, EXPR is the INDIRECT_REF
1432 that got us here. FLAGS is as in get_expr_operands.
1433 FULL_REF contains the full pointer dereference expression, if we
1434 have it, or NULL otherwise.
1435 OFFSET and SIZE are the location of the access inside the
1436 dereferenced pointer, if known.
1437 RECURSE_ON_BASE should be set to true if we want to continue
1438 calling get_expr_operands on the base pointer, and false if
1439 something else will do it for us.
1443 static void
1444 get_indirect_ref_operands (tree stmt, tree expr, int flags,
1445 tree full_ref,
1446 HOST_WIDE_INT offset, HOST_WIDE_INT size,
1447 bool recurse_on_base)
1449 tree *pptr = &TREE_OPERAND (expr, 0);
1450 tree ptr = *pptr;
1451 stmt_ann_t s_ann = stmt_ann (stmt);
1453 /* Stores into INDIRECT_REF operands are never killing definitions. */
1454 flags &= ~opf_kill_def;
1456 if (SSA_VAR_P (ptr))
1458 struct ptr_info_def *pi = NULL;
1460 /* If PTR has flow-sensitive points-to information, use it. */
1461 if (TREE_CODE (ptr) == SSA_NAME
1462 && (pi = SSA_NAME_PTR_INFO (ptr)) != NULL
1463 && pi->name_mem_tag)
1465 /* PTR has its own memory tag. Use it. */
1466 add_virtual_operand (pi->name_mem_tag, s_ann, flags,
1467 full_ref, offset, size, false);
1469 else
1471 /* If PTR is not an SSA_NAME or it doesn't have a name
1472 tag, use its type memory tag. */
1473 var_ann_t v_ann;
1475 /* If we are emitting debugging dumps, display a warning if
1476 PTR is an SSA_NAME with no flow-sensitive alias
1477 information. That means that we may need to compute
1478 aliasing again. */
1479 if (dump_file
1480 && TREE_CODE (ptr) == SSA_NAME
1481 && pi == NULL)
1483 fprintf (dump_file,
1484 "NOTE: no flow-sensitive alias info for ");
1485 print_generic_expr (dump_file, ptr, dump_flags);
1486 fprintf (dump_file, " in ");
1487 print_generic_stmt (dump_file, stmt, dump_flags);
1490 if (TREE_CODE (ptr) == SSA_NAME)
1491 ptr = SSA_NAME_VAR (ptr);
1492 v_ann = var_ann (ptr);
1494 if (v_ann->type_mem_tag)
1495 add_virtual_operand (v_ann->type_mem_tag, s_ann, flags,
1496 full_ref, offset, size, false);
1500 /* If a constant is used as a pointer, we can't generate a real
1501 operand for it but we mark the statement volatile to prevent
1502 optimizations from messing things up. */
1503 else if (TREE_CODE (ptr) == INTEGER_CST)
1505 if (s_ann)
1506 s_ann->has_volatile_ops = true;
1507 return;
1509 /* Ok, this isn't even is_gimple_min_invariant. Something's broke. */
1510 else
1511 gcc_unreachable ();
1513 /* Add a USE operand for the base pointer. */
1514 if (recurse_on_base)
1515 get_expr_operands (stmt, pptr, opf_none);
1518 /* A subroutine of get_expr_operands to handle TARGET_MEM_REF. */
1520 static void
1521 get_tmr_operands (tree stmt, tree expr, int flags)
1523 tree tag = TMR_TAG (expr), ref;
1524 HOST_WIDE_INT offset, size, maxsize;
1525 subvar_t svars, sv;
1526 stmt_ann_t s_ann = stmt_ann (stmt);
1528 /* First record the real operands. */
1529 get_expr_operands (stmt, &TMR_BASE (expr), opf_none);
1530 get_expr_operands (stmt, &TMR_INDEX (expr), opf_none);
1532 /* MEM_REFs should never be killing. */
1533 flags &= ~opf_kill_def;
1535 if (TMR_SYMBOL (expr))
1537 stmt_ann_t ann = stmt_ann (stmt);
1538 add_to_addressable_set (TMR_SYMBOL (expr), &ann->addresses_taken);
1541 if (!tag)
1543 /* Something weird, so ensure that we will be careful. */
1544 stmt_ann (stmt)->has_volatile_ops = true;
1545 return;
1548 if (DECL_P (tag))
1550 get_expr_operands (stmt, &tag, flags);
1551 return;
1554 ref = get_ref_base_and_extent (tag, &offset, &size, &maxsize);
1555 gcc_assert (ref != NULL_TREE);
1556 svars = get_subvars_for_var (ref);
1557 for (sv = svars; sv; sv = sv->next)
1559 bool exact;
1560 if (overlap_subvar (offset, maxsize, sv->var, &exact))
1562 int subvar_flags = flags;
1563 if (!exact || size != maxsize)
1564 subvar_flags &= ~opf_kill_def;
1565 add_stmt_operand (&sv->var, s_ann, subvar_flags);
1570 /* A subroutine of get_expr_operands to handle CALL_EXPR. */
1572 static void
1573 get_call_expr_operands (tree stmt, tree expr)
1575 tree op;
1576 int call_flags = call_expr_flags (expr);
1578 /* If aliases have been computed already, add V_MAY_DEF or V_USE
1579 operands for all the symbols that have been found to be
1580 call-clobbered.
1582 Note that if aliases have not been computed, the global effects
1583 of calls will not be included in the SSA web. This is fine
1584 because no optimizer should run before aliases have been
1585 computed. By not bothering with virtual operands for CALL_EXPRs
1586 we avoid adding superfluous virtual operands, which can be a
1587 significant compile time sink (See PR 15855). */
1588 if (aliases_computed_p
1589 && !bitmap_empty_p (call_clobbered_vars)
1590 && !(call_flags & ECF_NOVOPS))
1592 /* A 'pure' or a 'const' function never call-clobbers anything.
1593 A 'noreturn' function might, but since we don't return anyway
1594 there is no point in recording that. */
1595 if (TREE_SIDE_EFFECTS (expr)
1596 && !(call_flags & (ECF_PURE | ECF_CONST | ECF_NORETURN)))
1597 add_call_clobber_ops (stmt, get_callee_fndecl (expr));
1598 else if (!(call_flags & ECF_CONST))
1599 add_call_read_ops (stmt, get_callee_fndecl (expr));
1602 /* Find uses in the called function. */
1603 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_none);
1605 for (op = TREE_OPERAND (expr, 1); op; op = TREE_CHAIN (op))
1606 get_expr_operands (stmt, &TREE_VALUE (op), opf_none);
1608 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1612 /* REF is a tree that contains the entire pointer dereference
1613 expression, if available, or NULL otherwise. ALIAS is the variable
1614 we are asking if REF can access. OFFSET and SIZE come from the
1615 memory access expression that generated this virtual operand.
1616 FOR_CLOBBER is true is this is adding a virtual operand for a call
1617 clobber. */
1619 static bool
1620 access_can_touch_variable (tree ref, tree alias, HOST_WIDE_INT offset,
1621 HOST_WIDE_INT size)
1623 bool offsetgtz = offset > 0;
1624 unsigned HOST_WIDE_INT uoffset = (unsigned HOST_WIDE_INT) offset;
1625 tree base = ref ? get_base_address (ref) : NULL;
1627 /* If ALIAS is an SFT, it can't be touched if the offset
1628 and size of the access is not overlapping with the SFT offset and
1629 size. This is only true if we are accessing through a pointer
1630 to a type that is the same as SFT_PARENT_VAR. Otherwise, we may
1631 be accessing through a pointer to some substruct of the
1632 structure, and if we try to prune there, we will have the wrong
1633 offset, and get the wrong answer.
1634 i.e., we can't prune without more work if we have something like
1635 struct gcc_target
1637 struct asm_out
1639 const char *byte_op;
1640 struct asm_int_op
1642 const char *hi;
1643 } aligned_op;
1644 } asm_out;
1645 } targetm;
1647 foo = &targetm.asm_out.aligned_op;
1648 return foo->hi;
1650 SFT.1, which represents hi, will have SFT_OFFSET=32 because in
1651 terms of SFT_PARENT_VAR, that is where it is.
1652 However, the access through the foo pointer will be at offset 0.
1655 if (size != -1
1656 && TREE_CODE (alias) == STRUCT_FIELD_TAG
1657 && base
1658 && TREE_TYPE (base) == TREE_TYPE (SFT_PARENT_VAR (alias))
1659 && !overlap_subvar (offset, size, alias, NULL))
1661 #ifdef ACCESS_DEBUGGING
1662 fprintf (stderr, "Access to ");
1663 print_generic_expr (stderr, ref, 0);
1664 fprintf (stderr, " may not touch ");
1665 print_generic_expr (stderr, alias, 0);
1666 fprintf (stderr, " in function %s\n", get_name (current_function_decl));
1667 #endif
1668 return false;
1671 /* Without strict aliasing, it is impossible for a component access
1672 through a pointer to touch a random variable, unless that
1673 variable *is* a structure or a pointer.
1676 IE given p->c, and some random global variable b,
1677 there is no legal way that p->c could be an access to b.
1679 Without strict aliasing on, we consider it legal to do something
1680 like:
1681 struct foos { int l; };
1682 int foo;
1683 static struct foos *getfoo(void);
1684 int main (void)
1686 struct foos *f = getfoo();
1687 f->l = 1;
1688 foo = 2;
1689 if (f->l == 1)
1690 abort();
1691 exit(0);
1693 static struct foos *getfoo(void)
1694 { return (struct foos *)&foo; }
1696 (taken from 20000623-1.c)
1699 else if (ref
1700 && flag_strict_aliasing
1701 && TREE_CODE (ref) != INDIRECT_REF
1702 && !MTAG_P (alias)
1703 && !AGGREGATE_TYPE_P (TREE_TYPE (alias))
1704 && !TREE_CODE (TREE_TYPE (alias)) == COMPLEX_TYPE
1705 && !POINTER_TYPE_P (TREE_TYPE (alias)))
1707 #ifdef ACCESS_DEBUGGING
1708 fprintf (stderr, "Access to ");
1709 print_generic_expr (stderr, ref, 0);
1710 fprintf (stderr, " may not touch ");
1711 print_generic_expr (stderr, alias, 0);
1712 fprintf (stderr, " in function %s\n", get_name (current_function_decl));
1713 #endif
1714 return false;
1717 /* If the offset of the access is greater than the size of one of
1718 the possible aliases, it can't be touching that alias, because it
1719 would be past the end of the structure. */
1721 else if (ref
1722 && flag_strict_aliasing
1723 && TREE_CODE (ref) != INDIRECT_REF
1724 && !MTAG_P (alias)
1725 && !POINTER_TYPE_P (TREE_TYPE (alias))
1726 && offsetgtz
1727 && DECL_SIZE (alias)
1728 && TREE_CODE (DECL_SIZE (alias)) == INTEGER_CST
1729 && uoffset > TREE_INT_CST_LOW (DECL_SIZE (alias)))
1731 #ifdef ACCESS_DEBUGGING
1732 fprintf (stderr, "Access to ");
1733 print_generic_expr (stderr, ref, 0);
1734 fprintf (stderr, " may not touch ");
1735 print_generic_expr (stderr, alias, 0);
1736 fprintf (stderr, " in function %s\n", get_name (current_function_decl));
1737 #endif
1738 return false;
1740 return true;
1743 /* Add VAR to the virtual operands array. FLAGS is as in
1744 get_expr_operands. FULL_REF is a tree that contains the entire
1745 pointer dereference expression, if available, or NULL otherwise.
1746 OFFSET and SIZE come from the memory access expression that
1747 generated this virtual operand. FOR_CLOBBER is true is this is
1748 adding a virtual operand for a call clobber. */
1750 static void
1751 add_virtual_operand (tree var, stmt_ann_t s_ann, int flags,
1752 tree full_ref, HOST_WIDE_INT offset,
1753 HOST_WIDE_INT size, bool for_clobber)
1755 VEC(tree,gc) *aliases;
1756 tree sym;
1757 var_ann_t v_ann;
1759 sym = (TREE_CODE (var) == SSA_NAME ? SSA_NAME_VAR (var) : var);
1760 v_ann = var_ann (sym);
1762 /* Mark statements with volatile operands. Optimizers should back
1763 off from statements having volatile operands. */
1764 if (TREE_THIS_VOLATILE (sym) && s_ann)
1765 s_ann->has_volatile_ops = true;
1767 /* If the variable cannot be modified and this is a V_MAY_DEF change
1768 it into a VUSE. This happens when read-only variables are marked
1769 call-clobbered and/or aliased to writable variables. So we only
1770 check that this only happens on non-specific stores.
1772 Note that if this is a specific store, i.e. associated with a
1773 modify_expr, then we can't suppress the V_DEF, lest we run into
1774 validation problems.
1776 This can happen when programs cast away const, leaving us with a
1777 store to read-only memory. If the statement is actually executed
1778 at runtime, then the program is ill formed. If the statement is
1779 not executed then all is well. At the very least, we cannot ICE. */
1780 if ((flags & opf_non_specific) && unmodifiable_var_p (var))
1781 flags &= ~(opf_is_def | opf_kill_def);
1784 /* The variable is not a GIMPLE register. Add it (or its aliases) to
1785 virtual operands, unless the caller has specifically requested
1786 not to add virtual operands (used when adding operands inside an
1787 ADDR_EXPR expression). */
1788 if (flags & opf_no_vops)
1789 return;
1791 aliases = v_ann->may_aliases;
1792 if (aliases == NULL)
1794 /* The variable is not aliased or it is an alias tag. */
1795 if (flags & opf_is_def)
1797 if (flags & opf_kill_def)
1799 /* Only regular variables or struct fields may get a
1800 V_MUST_DEF operand. */
1801 gcc_assert (!MTAG_P (var)
1802 || TREE_CODE (var) == STRUCT_FIELD_TAG);
1803 /* V_MUST_DEF for non-aliased, non-GIMPLE register
1804 variable definitions. */
1805 append_v_must_def (var);
1807 else
1809 /* Add a V_MAY_DEF for call-clobbered variables and
1810 memory tags. */
1811 append_v_may_def (var);
1814 else
1815 append_vuse (var);
1817 else
1819 unsigned i;
1820 tree al;
1822 /* The variable is aliased. Add its aliases to the virtual
1823 operands. */
1824 gcc_assert (VEC_length (tree, aliases) != 0);
1826 if (flags & opf_is_def)
1829 bool none_added = true;
1831 for (i = 0; VEC_iterate (tree, aliases, i, al); i++)
1833 if (!access_can_touch_variable (full_ref, al, offset, size))
1834 continue;
1836 none_added = false;
1837 append_v_may_def (al);
1840 /* If the variable is also an alias tag, add a virtual
1841 operand for it, otherwise we will miss representing
1842 references to the members of the variable's alias set.
1843 This fixes the bug in gcc.c-torture/execute/20020503-1.c.
1845 It is also necessary to add bare defs on clobbers for
1846 TMT's, so that bare TMT uses caused by pruning all the
1847 aliases will link up properly with calls. */
1848 if (v_ann->is_alias_tag || none_added
1849 || (TREE_CODE (var) == TYPE_MEMORY_TAG && for_clobber))
1851 /* We should never end up with adding no aliases of an
1852 NMT, as that would imply we got the set wrong. */
1853 gcc_assert (!(none_added && TREE_CODE (var) == NAME_MEMORY_TAG));
1855 append_v_may_def (var);
1858 else
1860 bool none_added = true;
1861 for (i = 0; VEC_iterate (tree, aliases, i, al); i++)
1863 if (!access_can_touch_variable (full_ref, al, offset, size))
1864 continue;
1865 none_added = false;
1866 append_vuse (al);
1869 /* Similarly, append a virtual uses for VAR itself, when
1870 it is an alias tag. */
1871 if (v_ann->is_alias_tag || none_added)
1873 gcc_assert (!(none_added && TREE_CODE (var) == NAME_MEMORY_TAG));
1875 append_vuse (var);
1881 /* Add *VAR_P to the appropriate operand array for INFO. FLAGS is as in
1882 get_expr_operands. If *VAR_P is a GIMPLE register, it will be added to
1883 the statement's real operands, otherwise it is added to virtual
1884 operands. */
1886 static void
1887 add_stmt_operand (tree *var_p, stmt_ann_t s_ann, int flags)
1889 bool is_real_op;
1890 tree var, sym;
1891 var_ann_t v_ann;
1893 var = *var_p;
1894 gcc_assert (SSA_VAR_P (var));
1896 is_real_op = is_gimple_reg (var);
1897 /* If this is a real operand, the operand is either ssa name or decl.
1898 Virtual operands may only be decls. */
1899 gcc_assert (is_real_op || DECL_P (var));
1901 sym = (TREE_CODE (var) == SSA_NAME ? SSA_NAME_VAR (var) : var);
1902 v_ann = var_ann (sym);
1904 /* Mark statements with volatile operands. Optimizers should back
1905 off from statements having volatile operands. */
1906 if (TREE_THIS_VOLATILE (sym) && s_ann)
1907 s_ann->has_volatile_ops = true;
1909 if (is_real_op)
1911 /* The variable is a GIMPLE register. Add it to real operands. */
1912 if (flags & opf_is_def)
1913 append_def (var_p);
1914 else
1915 append_use (var_p);
1917 else
1918 add_virtual_operand (var, s_ann, flags, NULL_TREE, 0, -1, false);
1921 /* Add the base address of REF to the set *ADDRESSES_TAKEN. If
1922 *ADDRESSES_TAKEN is NULL, a new set is created. REF may be
1923 a single variable whose address has been taken or any other valid
1924 GIMPLE memory reference (structure reference, array, etc). If the
1925 base address of REF is a decl that has sub-variables, also add all
1926 of its sub-variables. */
1928 void
1929 add_to_addressable_set (tree ref, bitmap *addresses_taken)
1931 tree var;
1932 subvar_t svars;
1934 gcc_assert (addresses_taken);
1936 /* Note that it is *NOT OKAY* to use the target of a COMPONENT_REF
1937 as the only thing we take the address of. If VAR is a structure,
1938 taking the address of a field means that the whole structure may
1939 be referenced using pointer arithmetic. See PR 21407 and the
1940 ensuing mailing list discussion. */
1941 var = get_base_address (ref);
1942 if (var && SSA_VAR_P (var))
1944 if (*addresses_taken == NULL)
1945 *addresses_taken = BITMAP_GGC_ALLOC ();
1947 if (var_can_have_subvars (var)
1948 && (svars = get_subvars_for_var (var)))
1950 subvar_t sv;
1951 for (sv = svars; sv; sv = sv->next)
1953 bitmap_set_bit (*addresses_taken, DECL_UID (sv->var));
1954 TREE_ADDRESSABLE (sv->var) = 1;
1957 else
1959 bitmap_set_bit (*addresses_taken, DECL_UID (var));
1960 TREE_ADDRESSABLE (var) = 1;
1965 /* Add clobbering definitions for .GLOBAL_VAR or for each of the call
1966 clobbered variables in the function. */
1968 static void
1969 add_call_clobber_ops (tree stmt, tree callee)
1971 unsigned u;
1972 bitmap_iterator bi;
1973 stmt_ann_t s_ann = stmt_ann (stmt);
1974 bitmap not_read_b, not_written_b;
1976 /* Functions that are not const, pure or never return may clobber
1977 call-clobbered variables. */
1978 if (s_ann)
1979 s_ann->makes_clobbering_call = true;
1981 /* If we created .GLOBAL_VAR earlier, just use it. See compute_may_aliases
1982 for the heuristic used to decide whether to create .GLOBAL_VAR or not. */
1983 if (global_var)
1985 add_stmt_operand (&global_var, s_ann, opf_is_def);
1986 return;
1989 /* Get info for local and module level statics. There is a bit
1990 set for each static if the call being processed does not read
1991 or write that variable. */
1993 not_read_b = callee ? ipa_reference_get_not_read_global (callee) : NULL;
1994 not_written_b = callee ? ipa_reference_get_not_written_global (callee) : NULL;
1995 /* Add a V_MAY_DEF operand for every call clobbered variable. */
1996 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, u, bi)
1998 tree var = referenced_var_lookup (u);
1999 unsigned int escape_mask = var_ann (var)->escape_mask;
2000 tree real_var = var;
2001 bool not_read;
2002 bool not_written;
2004 /* Not read and not written are computed on regular vars, not
2005 subvars, so look at the parent var if this is an SFT. */
2007 if (TREE_CODE (var) == STRUCT_FIELD_TAG)
2008 real_var = SFT_PARENT_VAR (var);
2010 not_read = not_read_b ? bitmap_bit_p (not_read_b,
2011 DECL_UID (real_var)) : false;
2012 not_written = not_written_b ? bitmap_bit_p (not_written_b,
2013 DECL_UID (real_var)) : false;
2014 gcc_assert (!unmodifiable_var_p (var));
2016 clobber_stats.clobbered_vars++;
2018 /* See if this variable is really clobbered by this function. */
2020 /* Trivial case: Things escaping only to pure/const are not
2021 clobbered by non-pure-const, and only read by pure/const. */
2022 if ((escape_mask & ~(ESCAPE_TO_PURE_CONST)) == 0)
2024 tree call = get_call_expr_in (stmt);
2025 if (call_expr_flags (call) & (ECF_CONST | ECF_PURE))
2027 add_stmt_operand (&var, s_ann, opf_none);
2028 clobber_stats.unescapable_clobbers_avoided++;
2029 continue;
2031 else
2033 clobber_stats.unescapable_clobbers_avoided++;
2034 continue;
2038 if (not_written)
2040 clobber_stats.static_write_clobbers_avoided++;
2041 if (!not_read)
2042 add_stmt_operand (&var, s_ann, opf_none);
2043 else
2044 clobber_stats.static_read_clobbers_avoided++;
2046 else
2047 add_virtual_operand (var, s_ann, opf_is_def,
2048 NULL, 0, -1, true);
2054 /* Add VUSE operands for .GLOBAL_VAR or all call clobbered variables in the
2055 function. */
2057 static void
2058 add_call_read_ops (tree stmt, tree callee)
2060 unsigned u;
2061 bitmap_iterator bi;
2062 stmt_ann_t s_ann = stmt_ann (stmt);
2063 bitmap not_read_b;
2065 /* if the function is not pure, it may reference memory. Add
2066 a VUSE for .GLOBAL_VAR if it has been created. See add_referenced_var
2067 for the heuristic used to decide whether to create .GLOBAL_VAR. */
2068 if (global_var)
2070 add_stmt_operand (&global_var, s_ann, opf_none);
2071 return;
2074 not_read_b = callee ? ipa_reference_get_not_read_global (callee) : NULL;
2076 /* Add a VUSE for each call-clobbered variable. */
2077 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, u, bi)
2079 tree var = referenced_var (u);
2080 tree real_var = var;
2081 bool not_read;
2083 clobber_stats.readonly_clobbers++;
2085 /* Not read and not written are computed on regular vars, not
2086 subvars, so look at the parent var if this is an SFT. */
2088 if (TREE_CODE (var) == STRUCT_FIELD_TAG)
2089 real_var = SFT_PARENT_VAR (var);
2091 not_read = not_read_b ? bitmap_bit_p (not_read_b,
2092 DECL_UID (real_var)) : false;
2094 if (not_read)
2096 clobber_stats.static_readonly_clobbers_avoided++;
2097 continue;
2100 add_stmt_operand (&var, s_ann, opf_none | opf_non_specific);
2105 /* Scan the immediate_use list for VAR making sure its linked properly.
2106 return RTUE iof there is a problem. */
2108 bool
2109 verify_imm_links (FILE *f, tree var)
2111 use_operand_p ptr, prev, list;
2112 int count;
2114 gcc_assert (TREE_CODE (var) == SSA_NAME);
2116 list = &(SSA_NAME_IMM_USE_NODE (var));
2117 gcc_assert (list->use == NULL);
2119 if (list->prev == NULL)
2121 gcc_assert (list->next == NULL);
2122 return false;
2125 prev = list;
2126 count = 0;
2127 for (ptr = list->next; ptr != list; )
2129 if (prev != ptr->prev)
2130 goto error;
2132 if (ptr->use == NULL)
2133 goto error; /* 2 roots, or SAFE guard node. */
2134 else if (*(ptr->use) != var)
2135 goto error;
2137 prev = ptr;
2138 ptr = ptr->next;
2139 /* Avoid infinite loops. 50,000,000 uses probably indicates a problem. */
2140 if (count++ > 50000000)
2141 goto error;
2144 /* Verify list in the other direction. */
2145 prev = list;
2146 for (ptr = list->prev; ptr != list; )
2148 if (prev != ptr->next)
2149 goto error;
2150 prev = ptr;
2151 ptr = ptr->prev;
2152 if (count-- < 0)
2153 goto error;
2156 if (count != 0)
2157 goto error;
2159 return false;
2161 error:
2162 if (ptr->stmt && stmt_modified_p (ptr->stmt))
2164 fprintf (f, " STMT MODIFIED. - <%p> ", (void *)ptr->stmt);
2165 print_generic_stmt (f, ptr->stmt, TDF_SLIM);
2167 fprintf (f, " IMM ERROR : (use_p : tree - %p:%p)", (void *)ptr,
2168 (void *)ptr->use);
2169 print_generic_expr (f, USE_FROM_PTR (ptr), TDF_SLIM);
2170 fprintf(f, "\n");
2171 return true;
2175 /* Dump all the immediate uses to FILE. */
2177 void
2178 dump_immediate_uses_for (FILE *file, tree var)
2180 imm_use_iterator iter;
2181 use_operand_p use_p;
2183 gcc_assert (var && TREE_CODE (var) == SSA_NAME);
2185 print_generic_expr (file, var, TDF_SLIM);
2186 fprintf (file, " : -->");
2187 if (has_zero_uses (var))
2188 fprintf (file, " no uses.\n");
2189 else
2190 if (has_single_use (var))
2191 fprintf (file, " single use.\n");
2192 else
2193 fprintf (file, "%d uses.\n", num_imm_uses (var));
2195 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
2197 if (!is_gimple_reg (USE_FROM_PTR (use_p)))
2198 print_generic_stmt (file, USE_STMT (use_p), TDF_VOPS);
2199 else
2200 print_generic_stmt (file, USE_STMT (use_p), TDF_SLIM);
2202 fprintf(file, "\n");
2205 /* Dump all the immediate uses to FILE. */
2207 void
2208 dump_immediate_uses (FILE *file)
2210 tree var;
2211 unsigned int x;
2213 fprintf (file, "Immediate_uses: \n\n");
2214 for (x = 1; x < num_ssa_names; x++)
2216 var = ssa_name(x);
2217 if (!var)
2218 continue;
2219 dump_immediate_uses_for (file, var);
2224 /* Dump def-use edges on stderr. */
2226 void
2227 debug_immediate_uses (void)
2229 dump_immediate_uses (stderr);
2232 /* Dump def-use edges on stderr. */
2234 void
2235 debug_immediate_uses_for (tree var)
2237 dump_immediate_uses_for (stderr, var);
2239 #include "gt-tree-ssa-operands.h"