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)
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. */
23 #include "coretypes.h"
28 #include "diagnostic.h"
29 #include "tree-flow.h"
30 #include "tree-inline.h"
31 #include "tree-pass.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
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
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'. */
79 /* Flags to describe operand properties in helpers. */
81 /* By default, operands are loaded. */
84 /* Operand is the target of an assignment expression or a
85 call-clobbered variable. */
86 #define opf_is_def (1 << 0)
88 /* Operand is the target of an assignment expression. */
89 #define opf_kill_def (1 << 1)
91 /* No virtual operands should be created in the expression. This is used
92 when traversing ADDR_EXPR nodes which have different semantics than
93 other expressions. Inside an ADDR_EXPR node, the only operands that we
94 need to consider are indices into arrays. For instance, &a.b[i] should
95 generate a USE of 'i' but it should not generate a VUSE for 'a' nor a
97 #define opf_no_vops (1 << 2)
99 /* Operand is a "non-specific" kill for call-clobbers and such. This
100 is used to distinguish "reset the world" events from explicit
101 GIMPLE_MODIFY_STMTs. */
102 #define opf_non_specific (1 << 3)
104 /* Array for building all the def operands. */
105 static VEC(tree,heap) *build_defs;
107 /* Array for building all the use operands. */
108 static VEC(tree,heap) *build_uses;
110 /* Array for building all the V_MAY_DEF operands. */
111 static VEC(tree,heap) *build_v_may_defs;
113 /* Array for building all the VUSE operands. */
114 static VEC(tree,heap) *build_vuses;
116 /* Array for building all the V_MUST_DEF operands. */
117 static VEC(tree,heap) *build_v_must_defs;
119 static void get_expr_operands (tree, tree *, int);
121 /* Number of functions with initialized ssa_operands. */
122 static int n_initialized = 0;
124 /* Statement change buffer. Data structure used to record state
125 information for statements. This is used to determine what needs
126 to be done in order to update the SSA web after a statement is
127 modified by a pass. If STMT is a statement that has just been
128 created, or needs to be folded via fold_stmt, or anything that
129 changes its physical structure then the pass should:
131 1- Call push_stmt_changes (&stmt) to record the current state of
132 STMT before any modifications are made.
134 2- Make all appropriate modifications to the statement.
136 3- Call pop_stmt_changes (&stmt) to find new symbols that
137 need to be put in SSA form, SSA name mappings for names that
138 have disappeared, recompute invariantness for address
139 expressions, cleanup EH information, etc.
141 If it is possible to determine that the statement was not modified,
142 instead of calling pop_stmt_changes it is quicker to call
143 discard_stmt_changes to avoid the expensive and unnecessary operand
144 re-scan and change comparison. */
148 /* Pointer to the statement being modified. */
151 /* If the statement references memory these are the sets of symbols
152 loaded and stored by the statement. */
157 typedef struct scb_d *scb_t;
159 DEF_VEC_ALLOC_P(scb_t,heap);
161 /* Stack of statement change buffers (SCB). Every call to
162 push_stmt_changes pushes a new buffer onto the stack. Calls to
163 pop_stmt_changes pop a buffer off of the stack and compute the set
164 of changes for the popped statement. */
165 static VEC(scb_t,heap) *scb_stack;
167 /* Allocates operand OP of given TYPE from the appropriate free list,
168 or of the new value if the list is empty. */
170 #define ALLOC_OPTYPE(OP, TYPE) \
173 TYPE##_optype_p ret \
174 = gimple_ssa_operands (cfun)->free_##TYPE##s; \
176 gimple_ssa_operands (cfun)->free_##TYPE##s \
179 ret = ssa_operand_alloc (sizeof (*ret)); \
183 /* Return the DECL_UID of the base variable of T. */
185 static inline unsigned
186 get_name_decl (tree t)
188 if (TREE_CODE (t) != SSA_NAME)
191 return DECL_UID (SSA_NAME_VAR (t));
195 /* Comparison function for qsort used in operand_build_sort_virtual. */
198 operand_build_cmp (const void *p, const void *q)
200 tree e1 = *((const tree *)p);
201 tree e2 = *((const tree *)q);
204 u1 = get_name_decl (e1);
205 u2 = get_name_decl (e2);
207 /* We want to sort in ascending order. They can never be equal. */
208 #ifdef ENABLE_CHECKING
209 gcc_assert (u1 != u2);
211 return (u1 > u2 ? 1 : -1);
215 /* Sort the virtual operands in LIST from lowest DECL_UID to highest. */
218 operand_build_sort_virtual (VEC(tree,heap) *list)
220 int num = VEC_length (tree, list);
227 if (get_name_decl (VEC_index (tree, list, 0))
228 > get_name_decl (VEC_index (tree, list, 1)))
230 /* Swap elements if in the wrong order. */
231 tree tmp = VEC_index (tree, list, 0);
232 VEC_replace (tree, list, 0, VEC_index (tree, list, 1));
233 VEC_replace (tree, list, 1, tmp);
238 /* There are 3 or more elements, call qsort. */
239 qsort (VEC_address (tree, list),
240 VEC_length (tree, list),
246 /* Return true if the SSA operands cache is active. */
249 ssa_operands_active (void)
251 return cfun->gimple_df && gimple_ssa_operands (cfun)->ops_active;
255 /* Structure storing statistics on how many call clobbers we have, and
256 how many where avoided. */
260 /* Number of call-clobbered ops we attempt to add to calls in
261 add_call_clobber_ops. */
262 unsigned int clobbered_vars;
264 /* Number of write-clobbers (V_MAY_DEFs) avoided by using
265 not_written information. */
266 unsigned int static_write_clobbers_avoided;
268 /* Number of reads (VUSEs) avoided by using not_read information. */
269 unsigned int static_read_clobbers_avoided;
271 /* Number of write-clobbers avoided because the variable can't escape to
273 unsigned int unescapable_clobbers_avoided;
275 /* Number of read-only uses we attempt to add to calls in
276 add_call_read_ops. */
277 unsigned int readonly_clobbers;
279 /* Number of read-only uses we avoid using not_read information. */
280 unsigned int static_readonly_clobbers_avoided;
284 /* Initialize the operand cache routines. */
287 init_ssa_operands (void)
289 if (!n_initialized++)
291 build_defs = VEC_alloc (tree, heap, 5);
292 build_uses = VEC_alloc (tree, heap, 10);
293 build_vuses = VEC_alloc (tree, heap, 25);
294 build_v_may_defs = VEC_alloc (tree, heap, 25);
295 build_v_must_defs = VEC_alloc (tree, heap, 25);
298 gcc_assert (gimple_ssa_operands (cfun)->operand_memory == NULL);
299 gimple_ssa_operands (cfun)->operand_memory_index = SSA_OPERAND_MEMORY_SIZE;
300 gimple_ssa_operands (cfun)->ops_active = true;
301 memset (&clobber_stats, 0, sizeof (clobber_stats));
305 /* Dispose of anything required by the operand routines. */
308 fini_ssa_operands (void)
310 struct ssa_operand_memory_d *ptr;
311 if (!--n_initialized)
313 VEC_free (tree, heap, build_defs);
314 VEC_free (tree, heap, build_uses);
315 VEC_free (tree, heap, build_v_must_defs);
316 VEC_free (tree, heap, build_v_may_defs);
317 VEC_free (tree, heap, build_vuses);
319 gimple_ssa_operands (cfun)->free_defs = NULL;
320 gimple_ssa_operands (cfun)->free_uses = NULL;
321 gimple_ssa_operands (cfun)->free_vuses = NULL;
322 gimple_ssa_operands (cfun)->free_maydefs = NULL;
323 gimple_ssa_operands (cfun)->free_mustdefs = NULL;
324 while ((ptr = gimple_ssa_operands (cfun)->operand_memory) != NULL)
326 gimple_ssa_operands (cfun)->operand_memory
327 = gimple_ssa_operands (cfun)->operand_memory->next;
331 gimple_ssa_operands (cfun)->ops_active = false;
333 if (dump_file && (dump_flags & TDF_STATS))
335 fprintf (dump_file, "Original clobbered vars:%d\n",
336 clobber_stats.clobbered_vars);
337 fprintf (dump_file, "Static write clobbers avoided:%d\n",
338 clobber_stats.static_write_clobbers_avoided);
339 fprintf (dump_file, "Static read clobbers avoided:%d\n",
340 clobber_stats.static_read_clobbers_avoided);
341 fprintf (dump_file, "Unescapable clobbers avoided:%d\n",
342 clobber_stats.unescapable_clobbers_avoided);
343 fprintf (dump_file, "Original read-only clobbers:%d\n",
344 clobber_stats.readonly_clobbers);
345 fprintf (dump_file, "Static read-only clobbers avoided:%d\n",
346 clobber_stats.static_readonly_clobbers_avoided);
351 /* Return memory for operands of SIZE chunks. */
354 ssa_operand_alloc (unsigned size)
357 if (gimple_ssa_operands (cfun)->operand_memory_index + size
358 >= SSA_OPERAND_MEMORY_SIZE)
360 struct ssa_operand_memory_d *ptr;
361 ptr = GGC_NEW (struct ssa_operand_memory_d);
362 ptr->next = gimple_ssa_operands (cfun)->operand_memory;
363 gimple_ssa_operands (cfun)->operand_memory = ptr;
364 gimple_ssa_operands (cfun)->operand_memory_index = 0;
366 ptr = &(gimple_ssa_operands (cfun)->operand_memory
367 ->mem[gimple_ssa_operands (cfun)->operand_memory_index]);
368 gimple_ssa_operands (cfun)->operand_memory_index += size;
374 /* This routine makes sure that PTR is in an immediate use list, and makes
375 sure the stmt pointer is set to the current stmt. */
378 set_virtual_use_link (use_operand_p ptr, tree stmt)
380 /* fold_stmt may have changed the stmt pointers. */
381 if (ptr->stmt != stmt)
384 /* If this use isn't in a list, add it to the correct list. */
386 link_imm_use (ptr, *(ptr->use));
389 /* Appends ELT after TO, and moves the TO pointer to ELT. */
391 #define APPEND_OP_AFTER(ELT, TO) \
394 (TO)->next = (ELT); \
398 /* Appends head of list FROM after TO, and move both pointers
399 to their successors. */
401 #define MOVE_HEAD_AFTER(FROM, TO) \
404 APPEND_OP_AFTER (FROM, TO); \
405 (FROM) = (FROM)->next; \
408 /* Moves OP to appropriate freelist. OP is set to its successor. */
410 #define MOVE_HEAD_TO_FREELIST(OP, TYPE) \
413 TYPE##_optype_p next = (OP)->next; \
415 = gimple_ssa_operands (cfun)->free_##TYPE##s; \
416 gimple_ssa_operands (cfun)->free_##TYPE##s = (OP);\
420 /* Initializes immediate use at USE_PTR to value VAL, and links it to the list
421 of immediate uses. STMT is the current statement. */
423 #define INITIALIZE_USE(USE_PTR, VAL, STMT) \
426 (USE_PTR)->use = (VAL); \
427 link_imm_use_stmt ((USE_PTR), *(VAL), (STMT)); \
430 /* Adds OP to the list of defs after LAST, and moves
431 LAST to the new element. */
434 add_def_op (tree *op, def_optype_p *last)
438 ALLOC_OPTYPE (new, def);
439 DEF_OP_PTR (new) = op;
440 APPEND_OP_AFTER (new, *last);
443 /* Adds OP to the list of uses of statement STMT after LAST, and moves
444 LAST to the new element. */
447 add_use_op (tree stmt, tree *op, use_optype_p *last)
451 ALLOC_OPTYPE (new, use);
452 INITIALIZE_USE (USE_OP_PTR (new), op, stmt);
453 APPEND_OP_AFTER (new, *last);
456 /* Adds OP to the list of vuses of statement STMT after LAST, and moves
457 LAST to the new element. */
460 add_vuse_op (tree stmt, tree op, vuse_optype_p *last)
464 ALLOC_OPTYPE (new, vuse);
466 INITIALIZE_USE (VUSE_OP_PTR (new), &VUSE_OP (new), stmt);
467 APPEND_OP_AFTER (new, *last);
470 /* Adds OP to the list of maydefs of statement STMT after LAST, and moves
471 LAST to the new element. */
474 add_maydef_op (tree stmt, tree op, maydef_optype_p *last)
478 ALLOC_OPTYPE (new, maydef);
479 MAYDEF_RESULT (new) = op;
480 MAYDEF_OP (new) = op;
481 INITIALIZE_USE (MAYDEF_OP_PTR (new), &MAYDEF_OP (new), stmt);
482 APPEND_OP_AFTER (new, *last);
485 /* Adds OP to the list of mustdefs of statement STMT after LAST, and moves
486 LAST to the new element. */
489 add_mustdef_op (tree stmt, tree op, mustdef_optype_p *last)
491 mustdef_optype_p new;
493 ALLOC_OPTYPE (new, mustdef);
494 MUSTDEF_RESULT (new) = op;
495 MUSTDEF_KILL (new) = op;
496 INITIALIZE_USE (MUSTDEF_KILL_PTR (new), &MUSTDEF_KILL (new), stmt);
497 APPEND_OP_AFTER (new, *last);
500 /* Takes elements from build_defs and turns them into def operands of STMT.
501 TODO -- Given that def operands list is not necessarily sorted, merging
502 the operands this way does not make much sense.
503 -- Make build_defs VEC of tree *. */
506 finalize_ssa_def_ops (tree stmt)
509 struct def_optype_d new_list;
510 def_optype_p old_ops, last;
513 new_list.next = NULL;
516 old_ops = DEF_OPS (stmt);
519 while (old_ops && new_i < VEC_length (tree, build_defs))
521 tree *new_base = (tree *) VEC_index (tree, build_defs, new_i);
522 old_base = DEF_OP_PTR (old_ops);
524 if (old_base == new_base)
526 /* if variables are the same, reuse this node. */
527 MOVE_HEAD_AFTER (old_ops, last);
530 else if (old_base < new_base)
532 /* if old is less than new, old goes to the free list. */
533 MOVE_HEAD_TO_FREELIST (old_ops, def);
537 /* This is a new operand. */
538 add_def_op (new_base, &last);
543 /* If there is anything remaining in the build_defs list, simply emit it. */
544 for ( ; new_i < VEC_length (tree, build_defs); new_i++)
545 add_def_op ((tree *) VEC_index (tree, build_defs, new_i), &last);
549 /* If there is anything in the old list, free it. */
552 old_ops->next = gimple_ssa_operands (cfun)->free_defs;
553 gimple_ssa_operands (cfun)->free_defs = old_ops;
556 /* Now set the stmt's operands. */
557 DEF_OPS (stmt) = new_list.next;
559 #ifdef ENABLE_CHECKING
563 for (ptr = DEF_OPS (stmt); ptr; ptr = ptr->next)
566 gcc_assert (x == VEC_length (tree, build_defs));
571 /* This routine will create stmt operands for STMT from the def build list. */
574 finalize_ssa_defs (tree stmt)
576 unsigned int num = VEC_length (tree, build_defs);
578 /* There should only be a single real definition per assignment. */
579 gcc_assert ((stmt && TREE_CODE (stmt) != GIMPLE_MODIFY_STMT) || num <= 1);
581 /* If there is an old list, often the new list is identical, or close, so
582 find the elements at the beginning that are the same as the vector. */
583 finalize_ssa_def_ops (stmt);
584 VEC_truncate (tree, build_defs, 0);
587 /* Takes elements from build_uses and turns them into use operands of STMT.
588 TODO -- Make build_uses VEC of tree *. */
591 finalize_ssa_use_ops (tree stmt)
594 struct use_optype_d new_list;
595 use_optype_p old_ops, ptr, last;
597 new_list.next = NULL;
600 old_ops = USE_OPS (stmt);
602 /* If there is anything in the old list, free it. */
605 for (ptr = old_ops; ptr; ptr = ptr->next)
606 delink_imm_use (USE_OP_PTR (ptr));
607 old_ops->next = gimple_ssa_operands (cfun)->free_uses;
608 gimple_ssa_operands (cfun)->free_uses = old_ops;
611 /* Now create nodes for all the new nodes. */
612 for (new_i = 0; new_i < VEC_length (tree, build_uses); new_i++)
613 add_use_op (stmt, (tree *) VEC_index (tree, build_uses, new_i), &last);
617 /* Now set the stmt's operands. */
618 USE_OPS (stmt) = new_list.next;
620 #ifdef ENABLE_CHECKING
623 for (ptr = USE_OPS (stmt); ptr; ptr = ptr->next)
626 gcc_assert (x == VEC_length (tree, build_uses));
631 /* Return a new use operand vector for STMT, comparing to OLD_OPS_P. */
634 finalize_ssa_uses (tree stmt)
636 #ifdef ENABLE_CHECKING
639 unsigned num = VEC_length (tree, build_uses);
641 /* If the pointer to the operand is the statement itself, something is
642 wrong. It means that we are pointing to a local variable (the
643 initial call to update_stmt_operands does not pass a pointer to a
645 for (x = 0; x < num; x++)
646 gcc_assert (*((tree *)VEC_index (tree, build_uses, x)) != stmt);
649 finalize_ssa_use_ops (stmt);
650 VEC_truncate (tree, build_uses, 0);
654 /* Takes elements from build_v_may_defs and turns them into maydef operands of
658 finalize_ssa_v_may_def_ops (tree stmt)
661 struct maydef_optype_d new_list;
662 maydef_optype_p old_ops, ptr, last;
664 unsigned old_base, new_base;
666 new_list.next = NULL;
669 old_ops = MAYDEF_OPS (stmt);
672 while (old_ops && new_i < VEC_length (tree, build_v_may_defs))
674 act = VEC_index (tree, build_v_may_defs, new_i);
675 new_base = get_name_decl (act);
676 old_base = get_name_decl (MAYDEF_OP (old_ops));
678 if (old_base == new_base)
680 /* if variables are the same, reuse this node. */
681 MOVE_HEAD_AFTER (old_ops, last);
682 set_virtual_use_link (MAYDEF_OP_PTR (last), stmt);
685 else if (old_base < new_base)
687 /* if old is less than new, old goes to the free list. */
688 delink_imm_use (MAYDEF_OP_PTR (old_ops));
689 MOVE_HEAD_TO_FREELIST (old_ops, maydef);
693 /* This is a new operand. */
694 add_maydef_op (stmt, act, &last);
699 /* If there is anything remaining in the build_v_may_defs list, simply emit it. */
700 for ( ; new_i < VEC_length (tree, build_v_may_defs); new_i++)
701 add_maydef_op (stmt, VEC_index (tree, build_v_may_defs, new_i), &last);
705 /* If there is anything in the old list, free it. */
708 for (ptr = old_ops; ptr; ptr = ptr->next)
709 delink_imm_use (MAYDEF_OP_PTR (ptr));
710 old_ops->next = gimple_ssa_operands (cfun)->free_maydefs;
711 gimple_ssa_operands (cfun)->free_maydefs = old_ops;
714 /* Now set the stmt's operands. */
715 MAYDEF_OPS (stmt) = new_list.next;
717 #ifdef ENABLE_CHECKING
720 for (ptr = MAYDEF_OPS (stmt); ptr; ptr = ptr->next)
723 gcc_assert (x == VEC_length (tree, build_v_may_defs));
729 finalize_ssa_v_may_defs (tree stmt)
731 finalize_ssa_v_may_def_ops (stmt);
735 /* Clear the in_list bits and empty the build array for V_MAY_DEFs. */
738 cleanup_v_may_defs (void)
741 num = VEC_length (tree, build_v_may_defs);
743 for (x = 0; x < num; x++)
745 tree t = VEC_index (tree, build_v_may_defs, x);
746 if (TREE_CODE (t) != SSA_NAME)
748 var_ann_t ann = var_ann (t);
749 ann->in_v_may_def_list = 0;
752 VEC_truncate (tree, build_v_may_defs, 0);
756 /* Takes elements from build_vuses and turns them into vuse operands of
760 finalize_ssa_vuse_ops (tree stmt)
763 struct vuse_optype_d new_list;
764 vuse_optype_p old_ops, ptr, last;
766 unsigned old_base, new_base;
768 new_list.next = NULL;
771 old_ops = VUSE_OPS (stmt);
774 while (old_ops && new_i < VEC_length (tree, build_vuses))
776 act = VEC_index (tree, build_vuses, new_i);
777 new_base = get_name_decl (act);
778 old_base = get_name_decl (VUSE_OP (old_ops));
780 if (old_base == new_base)
782 /* if variables are the same, reuse this node. */
783 MOVE_HEAD_AFTER (old_ops, last);
784 set_virtual_use_link (VUSE_OP_PTR (last), stmt);
787 else if (old_base < new_base)
789 /* if old is less than new, old goes to the free list. */
790 delink_imm_use (USE_OP_PTR (old_ops));
791 MOVE_HEAD_TO_FREELIST (old_ops, vuse);
795 /* This is a new operand. */
796 add_vuse_op (stmt, act, &last);
801 /* If there is anything remaining in the build_vuses list, simply emit it. */
802 for ( ; new_i < VEC_length (tree, build_vuses); new_i++)
803 add_vuse_op (stmt, VEC_index (tree, build_vuses, new_i), &last);
807 /* If there is anything in the old list, free it. */
810 for (ptr = old_ops; ptr; ptr = ptr->next)
811 delink_imm_use (VUSE_OP_PTR (ptr));
812 old_ops->next = gimple_ssa_operands (cfun)->free_vuses;
813 gimple_ssa_operands (cfun)->free_vuses = old_ops;
816 /* Now set the stmt's operands. */
817 VUSE_OPS (stmt) = new_list.next;
819 #ifdef ENABLE_CHECKING
822 for (ptr = VUSE_OPS (stmt); ptr; ptr = ptr->next)
825 gcc_assert (x == VEC_length (tree, build_vuses));
830 /* Return a new VUSE operand vector, comparing to OLD_OPS_P. */
833 finalize_ssa_vuses (tree stmt)
835 unsigned num, num_v_may_defs;
838 /* Remove superfluous VUSE operands. If the statement already has a
839 V_MAY_DEF operation for a variable 'a', then a VUSE for 'a' is
840 not needed because V_MAY_DEFs imply a VUSE of the variable. For
841 instance, suppose that variable 'a' is aliased:
844 # a_3 = V_MAY_DEF <a_2>
847 The VUSE <a_2> is superfluous because it is implied by the
848 V_MAY_DEF operation. */
849 num = VEC_length (tree, build_vuses);
850 num_v_may_defs = VEC_length (tree, build_v_may_defs);
852 if (num > 0 && num_v_may_defs > 0)
854 for (vuse_index = 0; vuse_index < VEC_length (tree, build_vuses); )
857 vuse = VEC_index (tree, build_vuses, vuse_index);
858 if (TREE_CODE (vuse) != SSA_NAME)
860 var_ann_t ann = var_ann (vuse);
861 ann->in_vuse_list = 0;
862 if (ann->in_v_may_def_list)
864 VEC_ordered_remove (tree, build_vuses, vuse_index);
873 /* Clear out the in_list bits. */
875 vuse_index < VEC_length (tree, build_vuses);
878 tree t = VEC_index (tree, build_vuses, vuse_index);
879 if (TREE_CODE (t) != SSA_NAME)
881 var_ann_t ann = var_ann (t);
882 ann->in_vuse_list = 0;
887 finalize_ssa_vuse_ops (stmt);
889 /* The V_MAY_DEF build vector wasn't cleaned up because we needed it. */
890 cleanup_v_may_defs ();
892 /* Free the VUSEs build vector. */
893 VEC_truncate (tree, build_vuses, 0);
897 /* Takes elements from build_v_must_defs and turns them into mustdef operands of
901 finalize_ssa_v_must_def_ops (tree stmt)
904 struct mustdef_optype_d new_list;
905 mustdef_optype_p old_ops, ptr, last;
907 unsigned old_base, new_base;
909 new_list.next = NULL;
912 old_ops = MUSTDEF_OPS (stmt);
915 while (old_ops && new_i < VEC_length (tree, build_v_must_defs))
917 act = VEC_index (tree, build_v_must_defs, new_i);
918 new_base = get_name_decl (act);
919 old_base = get_name_decl (MUSTDEF_KILL (old_ops));
921 if (old_base == new_base)
923 /* If variables are the same, reuse this node. */
924 MOVE_HEAD_AFTER (old_ops, last);
925 set_virtual_use_link (MUSTDEF_KILL_PTR (last), stmt);
928 else if (old_base < new_base)
930 /* If old is less than new, old goes to the free list. */
931 delink_imm_use (MUSTDEF_KILL_PTR (old_ops));
932 MOVE_HEAD_TO_FREELIST (old_ops, mustdef);
936 /* This is a new operand. */
937 add_mustdef_op (stmt, act, &last);
942 /* If there is anything remaining in the build_v_must_defs list, simply emit it. */
943 for ( ; new_i < VEC_length (tree, build_v_must_defs); new_i++)
944 add_mustdef_op (stmt, VEC_index (tree, build_v_must_defs, new_i), &last);
948 /* If there is anything in the old list, free it. */
951 for (ptr = old_ops; ptr; ptr = ptr->next)
952 delink_imm_use (MUSTDEF_KILL_PTR (ptr));
953 old_ops->next = gimple_ssa_operands (cfun)->free_mustdefs;
954 gimple_ssa_operands (cfun)->free_mustdefs = old_ops;
957 /* Now set the stmt's operands. */
958 MUSTDEF_OPS (stmt) = new_list.next;
960 #ifdef ENABLE_CHECKING
963 for (ptr = MUSTDEF_OPS (stmt); ptr; ptr = ptr->next)
966 gcc_assert (x == VEC_length (tree, build_v_must_defs));
972 finalize_ssa_v_must_defs (tree stmt)
974 /* In the presence of subvars, there may be more than one V_MUST_DEF
975 per statement (one for each subvar). It is a bit expensive to
976 verify that all must-defs in a statement belong to subvars if
977 there is more than one must-def, so we don't do it. Suffice to
978 say, if you reach here without having subvars, and have num >1,
979 you have hit a bug. */
980 finalize_ssa_v_must_def_ops (stmt);
981 VEC_truncate (tree, build_v_must_defs, 0);
985 /* Finalize all the build vectors, fill the new ones into INFO. */
988 finalize_ssa_stmt_operands (tree stmt)
990 finalize_ssa_defs (stmt);
991 finalize_ssa_uses (stmt);
992 finalize_ssa_v_must_defs (stmt);
993 finalize_ssa_v_may_defs (stmt);
994 finalize_ssa_vuses (stmt);
998 /* Start the process of building up operands vectors in INFO. */
1001 start_ssa_stmt_operands (void)
1003 gcc_assert (VEC_length (tree, build_defs) == 0);
1004 gcc_assert (VEC_length (tree, build_uses) == 0);
1005 gcc_assert (VEC_length (tree, build_vuses) == 0);
1006 gcc_assert (VEC_length (tree, build_v_may_defs) == 0);
1007 gcc_assert (VEC_length (tree, build_v_must_defs) == 0);
1011 /* Add DEF_P to the list of pointers to operands. */
1014 append_def (tree *def_p)
1016 VEC_safe_push (tree, heap, build_defs, (tree)def_p);
1020 /* Add USE_P to the list of pointers to operands. */
1023 append_use (tree *use_p)
1025 VEC_safe_push (tree, heap, build_uses, (tree)use_p);
1029 /* Add a new virtual may def for variable VAR to the build array. */
1032 append_v_may_def (tree var)
1034 if (TREE_CODE (var) != SSA_NAME)
1036 var_ann_t ann = get_var_ann (var);
1038 /* Don't allow duplicate entries. */
1039 if (ann->in_v_may_def_list)
1041 ann->in_v_may_def_list = 1;
1044 VEC_safe_push (tree, heap, build_v_may_defs, (tree)var);
1048 /* Add VAR to the list of virtual uses. */
1051 append_vuse (tree var)
1053 /* Don't allow duplicate entries. */
1054 if (TREE_CODE (var) != SSA_NAME)
1056 var_ann_t ann = get_var_ann (var);
1058 if (ann->in_vuse_list || ann->in_v_may_def_list)
1060 ann->in_vuse_list = 1;
1063 VEC_safe_push (tree, heap, build_vuses, (tree)var);
1067 /* Add VAR to the list of virtual must definitions for INFO. */
1070 append_v_must_def (tree var)
1074 /* Don't allow duplicate entries. */
1075 for (i = 0; i < VEC_length (tree, build_v_must_defs); i++)
1076 if (var == VEC_index (tree, build_v_must_defs, i))
1079 VEC_safe_push (tree, heap, build_v_must_defs, (tree)var);
1083 /* REF is a tree that contains the entire pointer dereference
1084 expression, if available, or NULL otherwise. ALIAS is the variable
1085 we are asking if REF can access. OFFSET and SIZE come from the
1086 memory access expression that generated this virtual operand. */
1089 access_can_touch_variable (tree ref, tree alias, HOST_WIDE_INT offset,
1092 bool offsetgtz = offset > 0;
1093 unsigned HOST_WIDE_INT uoffset = (unsigned HOST_WIDE_INT) offset;
1094 tree base = ref ? get_base_address (ref) : NULL;
1096 /* If ALIAS is .GLOBAL_VAR then the memory reference REF must be
1097 using a call-clobbered memory tag. By definition, call-clobbered
1098 memory tags can always touch .GLOBAL_VAR. */
1099 if (alias == gimple_global_var (cfun))
1102 /* If ALIAS is an SFT, it can't be touched if the offset
1103 and size of the access is not overlapping with the SFT offset and
1104 size. This is only true if we are accessing through a pointer
1105 to a type that is the same as SFT_PARENT_VAR. Otherwise, we may
1106 be accessing through a pointer to some substruct of the
1107 structure, and if we try to prune there, we will have the wrong
1108 offset, and get the wrong answer.
1109 i.e., we can't prune without more work if we have something like
1115 const char *byte_op;
1123 foo = &targetm.asm_out.aligned_op;
1126 SFT.1, which represents hi, will have SFT_OFFSET=32 because in
1127 terms of SFT_PARENT_VAR, that is where it is.
1128 However, the access through the foo pointer will be at offset 0. */
1130 && TREE_CODE (alias) == STRUCT_FIELD_TAG
1132 && TREE_TYPE (base) == TREE_TYPE (SFT_PARENT_VAR (alias))
1133 && !overlap_subvar (offset, size, alias, NULL))
1135 #ifdef ACCESS_DEBUGGING
1136 fprintf (stderr, "Access to ");
1137 print_generic_expr (stderr, ref, 0);
1138 fprintf (stderr, " may not touch ");
1139 print_generic_expr (stderr, alias, 0);
1140 fprintf (stderr, " in function %s\n", get_name (current_function_decl));
1145 /* Without strict aliasing, it is impossible for a component access
1146 through a pointer to touch a random variable, unless that
1147 variable *is* a structure or a pointer.
1149 That is, given p->c, and some random global variable b,
1150 there is no legal way that p->c could be an access to b.
1152 Without strict aliasing on, we consider it legal to do something
1155 struct foos { int l; };
1157 static struct foos *getfoo(void);
1160 struct foos *f = getfoo();
1167 static struct foos *getfoo(void)
1168 { return (struct foos *)&foo; }
1170 (taken from 20000623-1.c)
1172 The docs also say/imply that access through union pointers
1173 is legal (but *not* if you take the address of the union member,
1174 i.e. the inverse), such that you can do
1184 U *pretmp = (U*)&rv;
1188 To implement this, we just punt on accesses through union
1192 && flag_strict_aliasing
1193 && TREE_CODE (ref) != INDIRECT_REF
1195 && (TREE_CODE (base) != INDIRECT_REF
1196 || TREE_CODE (TREE_TYPE (base)) != UNION_TYPE)
1197 && !AGGREGATE_TYPE_P (TREE_TYPE (alias))
1198 && TREE_CODE (TREE_TYPE (alias)) != COMPLEX_TYPE
1199 && !var_ann (alias)->is_heapvar
1200 /* When the struct has may_alias attached to it, we need not to
1202 && get_alias_set (base))
1204 #ifdef ACCESS_DEBUGGING
1205 fprintf (stderr, "Access to ");
1206 print_generic_expr (stderr, ref, 0);
1207 fprintf (stderr, " may not touch ");
1208 print_generic_expr (stderr, alias, 0);
1209 fprintf (stderr, " in function %s\n", get_name (current_function_decl));
1214 /* If the offset of the access is greater than the size of one of
1215 the possible aliases, it can't be touching that alias, because it
1216 would be past the end of the structure. */
1218 && flag_strict_aliasing
1219 && TREE_CODE (ref) != INDIRECT_REF
1221 && !POINTER_TYPE_P (TREE_TYPE (alias))
1223 && DECL_SIZE (alias)
1224 && TREE_CODE (DECL_SIZE (alias)) == INTEGER_CST
1225 && uoffset > TREE_INT_CST_LOW (DECL_SIZE (alias)))
1227 #ifdef ACCESS_DEBUGGING
1228 fprintf (stderr, "Access to ");
1229 print_generic_expr (stderr, ref, 0);
1230 fprintf (stderr, " may not touch ");
1231 print_generic_expr (stderr, alias, 0);
1232 fprintf (stderr, " in function %s\n", get_name (current_function_decl));
1241 /* Add VAR to the virtual operands array. FLAGS is as in
1242 get_expr_operands. FULL_REF is a tree that contains the entire
1243 pointer dereference expression, if available, or NULL otherwise.
1244 OFFSET and SIZE come from the memory access expression that
1245 generated this virtual operand. FOR_CLOBBER is true is this is
1246 adding a virtual operand for a call clobber. */
1249 add_virtual_operand (tree var, stmt_ann_t s_ann, int flags,
1250 tree full_ref, HOST_WIDE_INT offset,
1251 HOST_WIDE_INT size, bool for_clobber)
1253 VEC(tree,gc) *aliases;
1257 sym = (TREE_CODE (var) == SSA_NAME ? SSA_NAME_VAR (var) : var);
1258 v_ann = var_ann (sym);
1260 /* Mark statements with volatile operands. Optimizers should back
1261 off from statements having volatile operands. */
1262 if (TREE_THIS_VOLATILE (sym) && s_ann)
1263 s_ann->has_volatile_ops = true;
1265 /* If the variable cannot be modified and this is a V_MAY_DEF change
1266 it into a VUSE. This happens when read-only variables are marked
1267 call-clobbered and/or aliased to writable variables. So we only
1268 check that this only happens on non-specific stores.
1270 Note that if this is a specific store, i.e. associated with a
1271 gimple_modify_stmt, then we can't suppress the V_MAY_DEF, lest we run
1272 into validation problems.
1274 This can happen when programs cast away const, leaving us with a
1275 store to read-only memory. If the statement is actually executed
1276 at runtime, then the program is ill formed. If the statement is
1277 not executed then all is well. At the very least, we cannot ICE. */
1278 if ((flags & opf_non_specific) && unmodifiable_var_p (var))
1279 flags &= ~(opf_is_def | opf_kill_def);
1281 /* The variable is not a GIMPLE register. Add it (or its aliases) to
1282 virtual operands, unless the caller has specifically requested
1283 not to add virtual operands (used when adding operands inside an
1284 ADDR_EXPR expression). */
1285 if (flags & opf_no_vops)
1288 aliases = v_ann->may_aliases;
1289 if (aliases == NULL)
1291 /* The variable is not aliased or it is an alias tag. */
1292 if (flags & opf_is_def)
1294 if (flags & opf_kill_def)
1296 /* V_MUST_DEF for non-aliased, non-GIMPLE register
1297 variable definitions. */
1298 gcc_assert (!MTAG_P (var)
1299 || TREE_CODE (var) == STRUCT_FIELD_TAG);
1300 append_v_must_def (var);
1304 /* Add a V_MAY_DEF for call-clobbered variables and
1306 append_v_may_def (var);
1317 /* The variable is aliased. Add its aliases to the virtual
1319 gcc_assert (VEC_length (tree, aliases) != 0);
1321 if (flags & opf_is_def)
1324 bool none_added = true;
1326 for (i = 0; VEC_iterate (tree, aliases, i, al); i++)
1328 if (!access_can_touch_variable (full_ref, al, offset, size))
1332 append_v_may_def (al);
1335 /* If the variable is also an alias tag, add a virtual
1336 operand for it, otherwise we will miss representing
1337 references to the members of the variable's alias set.
1338 This fixes the bug in gcc.c-torture/execute/20020503-1.c.
1340 It is also necessary to add bare defs on clobbers for
1341 SMT's, so that bare SMT uses caused by pruning all the
1342 aliases will link up properly with calls. In order to
1343 keep the number of these bare defs we add down to the
1344 minimum necessary, we keep track of which SMT's were used
1345 alone in statement vdefs or VUSEs. */
1346 if (v_ann->is_aliased
1348 || (TREE_CODE (var) == SYMBOL_MEMORY_TAG
1351 append_v_may_def (var);
1356 bool none_added = true;
1357 for (i = 0; VEC_iterate (tree, aliases, i, al); i++)
1359 if (!access_can_touch_variable (full_ref, al, offset, size))
1365 /* Similarly, append a virtual uses for VAR itself, when
1366 it is an alias tag. */
1367 if (v_ann->is_aliased || none_added)
1374 /* Add *VAR_P to the appropriate operand array for S_ANN. FLAGS is as in
1375 get_expr_operands. If *VAR_P is a GIMPLE register, it will be added to
1376 the statement's real operands, otherwise it is added to virtual
1380 add_stmt_operand (tree *var_p, stmt_ann_t s_ann, int flags)
1387 gcc_assert (SSA_VAR_P (var));
1389 is_real_op = is_gimple_reg (var);
1391 /* If this is a real operand, the operand is either an SSA name or a
1392 decl. Virtual operands may only be decls. */
1393 gcc_assert (is_real_op || DECL_P (var));
1395 sym = (TREE_CODE (var) == SSA_NAME ? SSA_NAME_VAR (var) : var);
1396 v_ann = var_ann (sym);
1398 /* Mark statements with volatile operands. Optimizers should back
1399 off from statements having volatile operands. */
1400 if (TREE_THIS_VOLATILE (sym) && s_ann)
1401 s_ann->has_volatile_ops = true;
1405 /* The variable is a GIMPLE register. Add it to real operands. */
1406 if (flags & opf_is_def)
1412 add_virtual_operand (var, s_ann, flags, NULL_TREE, 0, -1, false);
1416 /* A subroutine of get_expr_operands to handle INDIRECT_REF,
1417 ALIGN_INDIRECT_REF and MISALIGNED_INDIRECT_REF.
1419 STMT is the statement being processed, EXPR is the INDIRECT_REF
1422 FLAGS is as in get_expr_operands.
1424 FULL_REF contains the full pointer dereference expression, if we
1425 have it, or NULL otherwise.
1427 OFFSET and SIZE are the location of the access inside the
1428 dereferenced pointer, if known.
1430 RECURSE_ON_BASE should be set to true if we want to continue
1431 calling get_expr_operands on the base pointer, and false if
1432 something else will do it for us. */
1435 get_indirect_ref_operands (tree stmt, tree expr, int flags,
1437 HOST_WIDE_INT offset, HOST_WIDE_INT size,
1438 bool recurse_on_base)
1440 tree *pptr = &TREE_OPERAND (expr, 0);
1442 stmt_ann_t s_ann = stmt_ann (stmt);
1444 /* Stores into INDIRECT_REF operands are never killing definitions. */
1445 flags &= ~opf_kill_def;
1447 if (SSA_VAR_P (ptr))
1449 struct ptr_info_def *pi = NULL;
1451 /* If PTR has flow-sensitive points-to information, use it. */
1452 if (TREE_CODE (ptr) == SSA_NAME
1453 && (pi = SSA_NAME_PTR_INFO (ptr)) != NULL
1454 && pi->name_mem_tag)
1456 /* PTR has its own memory tag. Use it. */
1457 add_virtual_operand (pi->name_mem_tag, s_ann, flags,
1458 full_ref, offset, size, false);
1462 /* If PTR is not an SSA_NAME or it doesn't have a name
1463 tag, use its symbol memory tag. */
1466 /* If we are emitting debugging dumps, display a warning if
1467 PTR is an SSA_NAME with no flow-sensitive alias
1468 information. That means that we may need to compute
1471 && TREE_CODE (ptr) == SSA_NAME
1475 "NOTE: no flow-sensitive alias info for ");
1476 print_generic_expr (dump_file, ptr, dump_flags);
1477 fprintf (dump_file, " in ");
1478 print_generic_stmt (dump_file, stmt, dump_flags);
1481 if (TREE_CODE (ptr) == SSA_NAME)
1482 ptr = SSA_NAME_VAR (ptr);
1483 v_ann = var_ann (ptr);
1485 if (v_ann->symbol_mem_tag)
1486 add_virtual_operand (v_ann->symbol_mem_tag, s_ann, flags,
1487 full_ref, offset, size, false);
1490 else if (TREE_CODE (ptr) == INTEGER_CST)
1492 /* If a constant is used as a pointer, we can't generate a real
1493 operand for it but we mark the statement volatile to prevent
1494 optimizations from messing things up. */
1496 s_ann->has_volatile_ops = true;
1501 /* Ok, this isn't even is_gimple_min_invariant. Something's broke. */
1505 /* If requested, add a USE operand for the base pointer. */
1506 if (recurse_on_base)
1507 get_expr_operands (stmt, pptr, opf_none);
1511 /* A subroutine of get_expr_operands to handle TARGET_MEM_REF. */
1514 get_tmr_operands (tree stmt, tree expr, int flags)
1516 tree tag = TMR_TAG (expr), ref;
1517 HOST_WIDE_INT offset, size, maxsize;
1519 stmt_ann_t s_ann = stmt_ann (stmt);
1521 /* First record the real operands. */
1522 get_expr_operands (stmt, &TMR_BASE (expr), opf_none);
1523 get_expr_operands (stmt, &TMR_INDEX (expr), opf_none);
1525 /* MEM_REFs should never be killing. */
1526 flags &= ~opf_kill_def;
1528 if (TMR_SYMBOL (expr))
1530 stmt_ann_t ann = stmt_ann (stmt);
1531 add_to_addressable_set (TMR_SYMBOL (expr), &ann->addresses_taken);
1536 /* Something weird, so ensure that we will be careful. */
1537 stmt_ann (stmt)->has_volatile_ops = true;
1543 get_expr_operands (stmt, &tag, flags);
1547 ref = get_ref_base_and_extent (tag, &offset, &size, &maxsize);
1548 gcc_assert (ref != NULL_TREE);
1549 svars = get_subvars_for_var (ref);
1550 for (sv = svars; sv; sv = sv->next)
1553 if (overlap_subvar (offset, maxsize, sv->var, &exact))
1555 int subvar_flags = flags;
1556 if (!exact || size != maxsize)
1557 subvar_flags &= ~opf_kill_def;
1558 add_stmt_operand (&sv->var, s_ann, subvar_flags);
1564 /* Add clobbering definitions for .GLOBAL_VAR or for each of the call
1565 clobbered variables in the function. */
1568 add_call_clobber_ops (tree stmt, tree callee)
1572 stmt_ann_t s_ann = stmt_ann (stmt);
1573 bitmap not_read_b, not_written_b;
1575 /* Functions that are not const, pure or never return may clobber
1576 call-clobbered variables. */
1578 s_ann->makes_clobbering_call = true;
1580 /* If we created .GLOBAL_VAR earlier, just use it. See compute_may_aliases
1581 for the heuristic used to decide whether to create .GLOBAL_VAR or not. */
1582 if (gimple_global_var (cfun))
1584 tree var = gimple_global_var (cfun);
1585 add_stmt_operand (&var, s_ann, opf_is_def);
1589 /* Get info for local and module level statics. There is a bit
1590 set for each static if the call being processed does not read
1591 or write that variable. */
1592 not_read_b = callee ? ipa_reference_get_not_read_global (callee) : NULL;
1593 not_written_b = callee ? ipa_reference_get_not_written_global (callee) : NULL;
1594 /* Add a V_MAY_DEF operand for every call clobbered variable. */
1595 EXECUTE_IF_SET_IN_BITMAP (gimple_call_clobbered_vars (cfun), 0, u, bi)
1597 tree var = referenced_var_lookup (u);
1598 unsigned int escape_mask = var_ann (var)->escape_mask;
1599 tree real_var = var;
1603 /* Not read and not written are computed on regular vars, not
1604 subvars, so look at the parent var if this is an SFT. */
1605 if (TREE_CODE (var) == STRUCT_FIELD_TAG)
1606 real_var = SFT_PARENT_VAR (var);
1608 not_read = not_read_b ? bitmap_bit_p (not_read_b,
1609 DECL_UID (real_var)) : false;
1610 not_written = not_written_b ? bitmap_bit_p (not_written_b,
1611 DECL_UID (real_var)) : false;
1612 gcc_assert (!unmodifiable_var_p (var));
1614 clobber_stats.clobbered_vars++;
1616 /* See if this variable is really clobbered by this function. */
1618 /* Trivial case: Things escaping only to pure/const are not
1619 clobbered by non-pure-const, and only read by pure/const. */
1620 if ((escape_mask & ~(ESCAPE_TO_PURE_CONST)) == 0)
1622 tree call = get_call_expr_in (stmt);
1623 if (call_expr_flags (call) & (ECF_CONST | ECF_PURE))
1625 add_stmt_operand (&var, s_ann, opf_none);
1626 clobber_stats.unescapable_clobbers_avoided++;
1631 clobber_stats.unescapable_clobbers_avoided++;
1638 clobber_stats.static_write_clobbers_avoided++;
1640 add_stmt_operand (&var, s_ann, opf_none);
1642 clobber_stats.static_read_clobbers_avoided++;
1645 add_virtual_operand (var, s_ann, opf_is_def, NULL, 0, -1, true);
1650 /* Add VUSE operands for .GLOBAL_VAR or all call clobbered variables in the
1654 add_call_read_ops (tree stmt, tree callee)
1658 stmt_ann_t s_ann = stmt_ann (stmt);
1661 /* if the function is not pure, it may reference memory. Add
1662 a VUSE for .GLOBAL_VAR if it has been created. See add_referenced_var
1663 for the heuristic used to decide whether to create .GLOBAL_VAR. */
1664 if (gimple_global_var (cfun))
1666 tree var = gimple_global_var (cfun);
1667 add_stmt_operand (&var, s_ann, opf_none);
1671 not_read_b = callee ? ipa_reference_get_not_read_global (callee) : NULL;
1673 /* Add a VUSE for each call-clobbered variable. */
1674 EXECUTE_IF_SET_IN_BITMAP (gimple_call_clobbered_vars (cfun), 0, u, bi)
1676 tree var = referenced_var (u);
1677 tree real_var = var;
1680 clobber_stats.readonly_clobbers++;
1682 /* Not read and not written are computed on regular vars, not
1683 subvars, so look at the parent var if this is an SFT. */
1685 if (TREE_CODE (var) == STRUCT_FIELD_TAG)
1686 real_var = SFT_PARENT_VAR (var);
1688 not_read = not_read_b ? bitmap_bit_p (not_read_b, DECL_UID (real_var))
1693 clobber_stats.static_readonly_clobbers_avoided++;
1697 add_stmt_operand (&var, s_ann, opf_none | opf_non_specific);
1702 /* A subroutine of get_expr_operands to handle CALL_EXPR. */
1705 get_call_expr_operands (tree stmt, tree expr)
1708 int call_flags = call_expr_flags (expr);
1710 /* If aliases have been computed already, add V_MAY_DEF or V_USE
1711 operands for all the symbols that have been found to be
1714 Note that if aliases have not been computed, the global effects
1715 of calls will not be included in the SSA web. This is fine
1716 because no optimizer should run before aliases have been
1717 computed. By not bothering with virtual operands for CALL_EXPRs
1718 we avoid adding superfluous virtual operands, which can be a
1719 significant compile time sink (See PR 15855). */
1720 if (gimple_aliases_computed_p (cfun)
1721 && !bitmap_empty_p (gimple_call_clobbered_vars (cfun))
1722 && !(call_flags & ECF_NOVOPS))
1724 /* A 'pure' or a 'const' function never call-clobbers anything.
1725 A 'noreturn' function might, but since we don't return anyway
1726 there is no point in recording that. */
1727 if (TREE_SIDE_EFFECTS (expr)
1728 && !(call_flags & (ECF_PURE | ECF_CONST | ECF_NORETURN)))
1729 add_call_clobber_ops (stmt, get_callee_fndecl (expr));
1730 else if (!(call_flags & ECF_CONST))
1731 add_call_read_ops (stmt, get_callee_fndecl (expr));
1734 /* Find uses in the called function. */
1735 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_none);
1737 for (op = TREE_OPERAND (expr, 1); op; op = TREE_CHAIN (op))
1738 get_expr_operands (stmt, &TREE_VALUE (op), opf_none);
1740 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1744 /* Scan operands in the ASM_EXPR stmt referred to in INFO. */
1747 get_asm_expr_operands (tree stmt)
1749 stmt_ann_t s_ann = stmt_ann (stmt);
1750 int noutputs = list_length (ASM_OUTPUTS (stmt));
1751 const char **oconstraints
1752 = (const char **) alloca ((noutputs) * sizeof (const char *));
1755 const char *constraint;
1756 bool allows_mem, allows_reg, is_inout;
1758 for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
1760 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1761 oconstraints[i] = constraint;
1762 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
1763 &allows_reg, &is_inout);
1765 /* This should have been split in gimplify_asm_expr. */
1766 gcc_assert (!allows_reg || !is_inout);
1768 /* Memory operands are addressable. Note that STMT needs the
1769 address of this operand. */
1770 if (!allows_reg && allows_mem)
1772 tree t = get_base_address (TREE_VALUE (link));
1773 if (t && DECL_P (t) && s_ann)
1774 add_to_addressable_set (t, &s_ann->addresses_taken);
1777 get_expr_operands (stmt, &TREE_VALUE (link), opf_is_def);
1780 for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
1782 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1783 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1784 oconstraints, &allows_mem, &allows_reg);
1786 /* Memory operands are addressable. Note that STMT needs the
1787 address of this operand. */
1788 if (!allows_reg && allows_mem)
1790 tree t = get_base_address (TREE_VALUE (link));
1791 if (t && DECL_P (t) && s_ann)
1792 add_to_addressable_set (t, &s_ann->addresses_taken);
1795 get_expr_operands (stmt, &TREE_VALUE (link), 0);
1799 /* Clobber memory for asm ("" : : : "memory"); */
1800 for (link = ASM_CLOBBERS (stmt); link; link = TREE_CHAIN (link))
1801 if (strcmp (TREE_STRING_POINTER (TREE_VALUE (link)), "memory") == 0)
1806 /* Clobber all call-clobbered variables (or .GLOBAL_VAR if we
1807 decided to group them). */
1808 if (gimple_global_var (cfun))
1810 tree var = gimple_global_var (cfun);
1811 add_stmt_operand (&var, s_ann, opf_is_def);
1814 EXECUTE_IF_SET_IN_BITMAP (gimple_call_clobbered_vars (cfun), 0, i, bi)
1816 tree var = referenced_var (i);
1817 add_stmt_operand (&var, s_ann, opf_is_def | opf_non_specific);
1820 /* Now clobber all addressables. */
1821 EXECUTE_IF_SET_IN_BITMAP (gimple_addressable_vars (cfun), 0, i, bi)
1823 tree var = referenced_var (i);
1825 /* Subvars are explicitly represented in this list, so
1826 we don't need the original to be added to the clobber
1827 ops, but the original *will* be in this list because
1828 we keep the addressability of the original
1829 variable up-to-date so we don't screw up the rest of
1831 if (var_can_have_subvars (var)
1832 && get_subvars_for_var (var) != NULL)
1835 add_stmt_operand (&var, s_ann, opf_is_def | opf_non_specific);
1843 /* Scan operands for the assignment expression EXPR in statement STMT. */
1846 get_modify_stmt_operands (tree stmt, tree expr)
1848 /* First get operands from the RHS. */
1849 get_expr_operands (stmt, &GIMPLE_STMT_OPERAND (expr, 1), opf_none);
1851 /* For the LHS, use a regular definition (OPF_IS_DEF) for GIMPLE
1852 registers. If the LHS is a store to memory, we will either need
1853 a preserving definition (V_MAY_DEF) or a killing definition
1856 Preserving definitions are those that modify a part of an
1857 aggregate object for which no subvars have been computed (or the
1858 reference does not correspond exactly to one of them). Stores
1859 through a pointer are also represented with V_MAY_DEF operators.
1861 The determination of whether to use a preserving or a killing
1862 definition is done while scanning the LHS of the assignment. By
1863 default, assume that we will emit a V_MUST_DEF. */
1864 get_expr_operands (stmt, &GIMPLE_STMT_OPERAND (expr, 0),
1865 opf_is_def|opf_kill_def);
1869 /* Recursively scan the expression pointed to by EXPR_P in statement
1870 STMT. FLAGS is one of the OPF_* constants modifying how to
1871 interpret the operands found. */
1874 get_expr_operands (tree stmt, tree *expr_p, int flags)
1876 enum tree_code code;
1877 enum tree_code_class class;
1878 tree expr = *expr_p;
1879 stmt_ann_t s_ann = stmt_ann (stmt);
1884 code = TREE_CODE (expr);
1885 class = TREE_CODE_CLASS (code);
1890 /* Taking the address of a variable does not represent a
1891 reference to it, but the fact that the statement takes its
1892 address will be of interest to some passes (e.g. alias
1894 add_to_addressable_set (TREE_OPERAND (expr, 0), &s_ann->addresses_taken);
1896 /* If the address is invariant, there may be no interesting
1897 variable references inside. */
1898 if (is_gimple_min_invariant (expr))
1901 /* Otherwise, there may be variables referenced inside but there
1902 should be no VUSEs created, since the referenced objects are
1903 not really accessed. The only operands that we should find
1904 here are ARRAY_REF indices which will always be real operands
1905 (GIMPLE does not allow non-registers as array indices). */
1906 flags |= opf_no_vops;
1907 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1911 case STRUCT_FIELD_TAG:
1912 case SYMBOL_MEMORY_TAG:
1913 case NAME_MEMORY_TAG:
1914 add_stmt_operand (expr_p, s_ann, flags);
1923 /* Add the subvars for a variable, if it has subvars, to DEFS
1924 or USES. Otherwise, add the variable itself. Whether it
1925 goes to USES or DEFS depends on the operand flags. */
1926 if (var_can_have_subvars (expr)
1927 && (svars = get_subvars_for_var (expr)))
1930 for (sv = svars; sv; sv = sv->next)
1931 add_stmt_operand (&sv->var, s_ann, flags);
1934 add_stmt_operand (expr_p, s_ann, flags);
1939 case MISALIGNED_INDIRECT_REF:
1940 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1943 case ALIGN_INDIRECT_REF:
1945 get_indirect_ref_operands (stmt, expr, flags, NULL_TREE, 0, -1, true);
1948 case TARGET_MEM_REF:
1949 get_tmr_operands (stmt, expr, flags);
1953 case ARRAY_RANGE_REF:
1959 HOST_WIDE_INT offset, size, maxsize;
1962 /* This component reference becomes an access to all of the
1963 subvariables it can touch, if we can determine that, but
1964 *NOT* the real one. If we can't determine which fields we
1965 could touch, the recursion will eventually get to a
1966 variable and add *all* of its subvars, or whatever is the
1967 minimum correct subset. */
1968 ref = get_ref_base_and_extent (expr, &offset, &size, &maxsize);
1969 if (SSA_VAR_P (ref) && get_subvars_for_var (ref))
1972 subvar_t svars = get_subvars_for_var (ref);
1974 for (sv = svars; sv; sv = sv->next)
1978 if (overlap_subvar (offset, maxsize, sv->var, &exact))
1980 int subvar_flags = flags;
1982 if (!exact || size != maxsize)
1983 subvar_flags &= ~opf_kill_def;
1984 add_stmt_operand (&sv->var, s_ann, subvar_flags);
1989 flags |= opf_no_vops;
1991 else if (TREE_CODE (ref) == INDIRECT_REF)
1993 get_indirect_ref_operands (stmt, ref, flags, expr, offset,
1995 flags |= opf_no_vops;
1998 /* Even if we found subvars above we need to ensure to see
1999 immediate uses for d in s.a[d]. In case of s.a having
2000 a subvar or we would miss it otherwise. */
2001 get_expr_operands (stmt, &TREE_OPERAND (expr, 0),
2002 flags & ~opf_kill_def);
2004 if (code == COMPONENT_REF)
2006 if (s_ann && TREE_THIS_VOLATILE (TREE_OPERAND (expr, 1)))
2007 s_ann->has_volatile_ops = true;
2008 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
2010 else if (code == ARRAY_REF || code == ARRAY_RANGE_REF)
2012 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
2013 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
2014 get_expr_operands (stmt, &TREE_OPERAND (expr, 3), opf_none);
2020 case WITH_SIZE_EXPR:
2021 /* WITH_SIZE_EXPR is a pass-through reference to its first argument,
2022 and an rvalue reference to its second argument. */
2023 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
2024 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
2028 get_call_expr_operands (stmt, expr);
2033 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_none);
2034 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
2035 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
2038 case GIMPLE_MODIFY_STMT:
2039 get_modify_stmt_operands (stmt, expr);
2044 /* General aggregate CONSTRUCTORs have been decomposed, but they
2045 are still in use as the COMPLEX_EXPR equivalent for vectors. */
2046 constructor_elt *ce;
2047 unsigned HOST_WIDE_INT idx;
2050 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (expr), idx, ce);
2052 get_expr_operands (stmt, &ce->value, opf_none);
2058 /* Stores using BIT_FIELD_REF are always preserving definitions. */
2059 flags &= ~opf_kill_def;
2063 case TRUTH_NOT_EXPR:
2064 case VIEW_CONVERT_EXPR:
2066 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
2069 case TRUTH_AND_EXPR:
2071 case TRUTH_XOR_EXPR:
2077 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
2078 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
2083 case REALIGN_LOAD_EXPR:
2085 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
2086 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
2087 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), flags);
2106 /* Expressions that make no memory references. */
2110 if (class == tcc_unary)
2112 if (class == tcc_binary || class == tcc_comparison)
2114 if (class == tcc_constant || class == tcc_type)
2118 /* If we get here, something has gone wrong. */
2119 #ifdef ENABLE_CHECKING
2120 fprintf (stderr, "unhandled expression in get_expr_operands():\n");
2122 fputs ("\n", stderr);
2128 /* Parse STMT looking for operands. When finished, the various
2129 build_* operand vectors will have potential operands in them. */
2132 parse_ssa_operands (tree stmt)
2134 enum tree_code code;
2136 code = TREE_CODE (stmt);
2139 case GIMPLE_MODIFY_STMT:
2140 get_modify_stmt_operands (stmt, stmt);
2144 get_expr_operands (stmt, &COND_EXPR_COND (stmt), opf_none);
2148 get_expr_operands (stmt, &SWITCH_COND (stmt), opf_none);
2152 get_asm_expr_operands (stmt);
2156 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), opf_none);
2160 get_expr_operands (stmt, &GOTO_DESTINATION (stmt), opf_none);
2164 get_expr_operands (stmt, &LABEL_EXPR_LABEL (stmt), opf_none);
2168 case CASE_LABEL_EXPR:
2169 case TRY_CATCH_EXPR:
2170 case TRY_FINALLY_EXPR:
2171 case EH_FILTER_EXPR:
2174 /* These nodes contain no variable references. */
2178 /* Notice that if get_expr_operands tries to use &STMT as the
2179 operand pointer (which may only happen for USE operands), we
2180 will fail in add_stmt_operand. This default will handle
2181 statements like empty statements, or CALL_EXPRs that may
2182 appear on the RHS of a statement or as statements themselves. */
2183 get_expr_operands (stmt, &stmt, opf_none);
2189 /* Create an operands cache for STMT. */
2192 build_ssa_operands (tree stmt)
2194 stmt_ann_t ann = get_stmt_ann (stmt);
2196 /* Initially assume that the statement has no volatile operands. */
2198 ann->has_volatile_ops = false;
2200 start_ssa_stmt_operands ();
2202 parse_ssa_operands (stmt);
2203 operand_build_sort_virtual (build_vuses);
2204 operand_build_sort_virtual (build_v_may_defs);
2205 operand_build_sort_virtual (build_v_must_defs);
2207 finalize_ssa_stmt_operands (stmt);
2211 /* Free any operands vectors in OPS. */
2214 free_ssa_operands (stmt_operands_p ops)
2216 ops->def_ops = NULL;
2217 ops->use_ops = NULL;
2218 ops->maydef_ops = NULL;
2219 ops->mustdef_ops = NULL;
2220 ops->vuse_ops = NULL;
2224 /* Get the operands of statement STMT. */
2227 update_stmt_operands (tree stmt)
2229 stmt_ann_t ann = get_stmt_ann (stmt);
2231 /* If update_stmt_operands is called before SSA is initialized, do
2233 if (!ssa_operands_active ())
2236 /* The optimizers cannot handle statements that are nothing but a
2237 _DECL. This indicates a bug in the gimplifier. */
2238 gcc_assert (!SSA_VAR_P (stmt));
2240 gcc_assert (ann->modified);
2242 timevar_push (TV_TREE_OPS);
2244 build_ssa_operands (stmt);
2246 /* Clear the modified bit for STMT. */
2249 timevar_pop (TV_TREE_OPS);
2253 /* Copies virtual operands from SRC to DST. */
2256 copy_virtual_operands (tree dest, tree src)
2259 ssa_op_iter iter, old_iter;
2260 use_operand_p use_p, u2;
2261 def_operand_p def_p, d2;
2263 build_ssa_operands (dest);
2265 /* Copy all the virtual fields. */
2266 FOR_EACH_SSA_TREE_OPERAND (t, src, iter, SSA_OP_VUSE)
2268 FOR_EACH_SSA_TREE_OPERAND (t, src, iter, SSA_OP_VMAYDEF)
2269 append_v_may_def (t);
2270 FOR_EACH_SSA_TREE_OPERAND (t, src, iter, SSA_OP_VMUSTDEF)
2271 append_v_must_def (t);
2273 if (VEC_length (tree, build_vuses) == 0
2274 && VEC_length (tree, build_v_may_defs) == 0
2275 && VEC_length (tree, build_v_must_defs) == 0)
2278 /* Now commit the virtual operands to this stmt. */
2279 finalize_ssa_v_must_defs (dest);
2280 finalize_ssa_v_may_defs (dest);
2281 finalize_ssa_vuses (dest);
2283 /* Finally, set the field to the same values as then originals. */
2284 t = op_iter_init_tree (&old_iter, src, SSA_OP_VUSE);
2285 FOR_EACH_SSA_USE_OPERAND (use_p, dest, iter, SSA_OP_VUSE)
2287 gcc_assert (!op_iter_done (&old_iter));
2289 t = op_iter_next_tree (&old_iter);
2291 gcc_assert (op_iter_done (&old_iter));
2293 op_iter_init_maydef (&old_iter, src, &u2, &d2);
2294 FOR_EACH_SSA_MAYDEF_OPERAND (def_p, use_p, dest, iter)
2296 gcc_assert (!op_iter_done (&old_iter));
2297 SET_USE (use_p, USE_FROM_PTR (u2));
2298 SET_DEF (def_p, DEF_FROM_PTR (d2));
2299 op_iter_next_maymustdef (&u2, &d2, &old_iter);
2301 gcc_assert (op_iter_done (&old_iter));
2303 op_iter_init_mustdef (&old_iter, src, &u2, &d2);
2304 FOR_EACH_SSA_MUSTDEF_OPERAND (def_p, use_p, dest, iter)
2306 gcc_assert (!op_iter_done (&old_iter));
2307 SET_USE (use_p, USE_FROM_PTR (u2));
2308 SET_DEF (def_p, DEF_FROM_PTR (d2));
2309 op_iter_next_maymustdef (&u2, &d2, &old_iter);
2311 gcc_assert (op_iter_done (&old_iter));
2316 /* Specifically for use in DOM's expression analysis. Given a store, we
2317 create an artificial stmt which looks like a load from the store, this can
2318 be used to eliminate redundant loads. OLD_OPS are the operands from the
2319 store stmt, and NEW_STMT is the new load which represents a load of the
2323 create_ssa_artificial_load_stmt (tree new_stmt, tree old_stmt)
2328 use_operand_p use_p;
2331 ann = get_stmt_ann (new_stmt);
2333 /* Process the stmt looking for operands. */
2334 start_ssa_stmt_operands ();
2335 parse_ssa_operands (new_stmt);
2337 for (x = 0; x < VEC_length (tree, build_vuses); x++)
2339 tree t = VEC_index (tree, build_vuses, x);
2340 if (TREE_CODE (t) != SSA_NAME)
2342 var_ann_t ann = var_ann (t);
2343 ann->in_vuse_list = 0;
2347 for (x = 0; x < VEC_length (tree, build_v_may_defs); x++)
2349 tree t = VEC_index (tree, build_v_may_defs, x);
2350 if (TREE_CODE (t) != SSA_NAME)
2352 var_ann_t ann = var_ann (t);
2353 ann->in_v_may_def_list = 0;
2357 /* Remove any virtual operands that were found. */
2358 VEC_truncate (tree, build_v_may_defs, 0);
2359 VEC_truncate (tree, build_v_must_defs, 0);
2360 VEC_truncate (tree, build_vuses, 0);
2362 /* For each VDEF on the original statement, we want to create a
2363 VUSE of the V_MAY_DEF result or V_MUST_DEF op on the new
2365 FOR_EACH_SSA_TREE_OPERAND (op, old_stmt, iter,
2366 (SSA_OP_VMAYDEF | SSA_OP_VMUSTDEF))
2369 /* Now build the operands for this new stmt. */
2370 finalize_ssa_stmt_operands (new_stmt);
2372 /* All uses in this fake stmt must not be in the immediate use lists. */
2373 FOR_EACH_SSA_USE_OPERAND (use_p, new_stmt, iter, SSA_OP_ALL_USES)
2374 delink_imm_use (use_p);
2378 /* Swap operands EXP0 and EXP1 in statement STMT. No attempt is done
2379 to test the validity of the swap operation. */
2382 swap_tree_operands (tree stmt, tree *exp0, tree *exp1)
2388 /* If the operand cache is active, attempt to preserve the relative
2389 positions of these two operands in their respective immediate use
2391 if (ssa_operands_active () && op0 != op1)
2393 use_optype_p use0, use1, ptr;
2396 /* Find the 2 operands in the cache, if they are there. */
2397 for (ptr = USE_OPS (stmt); ptr; ptr = ptr->next)
2398 if (USE_OP_PTR (ptr)->use == exp0)
2404 for (ptr = USE_OPS (stmt); ptr; ptr = ptr->next)
2405 if (USE_OP_PTR (ptr)->use == exp1)
2411 /* If both uses don't have operand entries, there isn't much we can do
2412 at this point. Presumably we don't need to worry about it. */
2415 tree *tmp = USE_OP_PTR (use1)->use;
2416 USE_OP_PTR (use1)->use = USE_OP_PTR (use0)->use;
2417 USE_OP_PTR (use0)->use = tmp;
2421 /* Now swap the data. */
2427 /* Add the base address of REF to the set *ADDRESSES_TAKEN. If
2428 *ADDRESSES_TAKEN is NULL, a new set is created. REF may be
2429 a single variable whose address has been taken or any other valid
2430 GIMPLE memory reference (structure reference, array, etc). If the
2431 base address of REF is a decl that has sub-variables, also add all
2432 of its sub-variables. */
2435 add_to_addressable_set (tree ref, bitmap *addresses_taken)
2440 gcc_assert (addresses_taken);
2442 /* Note that it is *NOT OKAY* to use the target of a COMPONENT_REF
2443 as the only thing we take the address of. If VAR is a structure,
2444 taking the address of a field means that the whole structure may
2445 be referenced using pointer arithmetic. See PR 21407 and the
2446 ensuing mailing list discussion. */
2447 var = get_base_address (ref);
2448 if (var && SSA_VAR_P (var))
2450 if (*addresses_taken == NULL)
2451 *addresses_taken = BITMAP_GGC_ALLOC ();
2453 if (var_can_have_subvars (var)
2454 && (svars = get_subvars_for_var (var)))
2457 for (sv = svars; sv; sv = sv->next)
2459 bitmap_set_bit (*addresses_taken, DECL_UID (sv->var));
2460 TREE_ADDRESSABLE (sv->var) = 1;
2465 bitmap_set_bit (*addresses_taken, DECL_UID (var));
2466 TREE_ADDRESSABLE (var) = 1;
2472 /* Scan the immediate_use list for VAR making sure its linked properly.
2473 Return TRUE if there is a problem and emit an error message to F. */
2476 verify_imm_links (FILE *f, tree var)
2478 use_operand_p ptr, prev, list;
2481 gcc_assert (TREE_CODE (var) == SSA_NAME);
2483 list = &(SSA_NAME_IMM_USE_NODE (var));
2484 gcc_assert (list->use == NULL);
2486 if (list->prev == NULL)
2488 gcc_assert (list->next == NULL);
2494 for (ptr = list->next; ptr != list; )
2496 if (prev != ptr->prev)
2499 if (ptr->use == NULL)
2500 goto error; /* 2 roots, or SAFE guard node. */
2501 else if (*(ptr->use) != var)
2507 /* Avoid infinite loops. 50,000,000 uses probably indicates a
2509 if (count++ > 50000000)
2513 /* Verify list in the other direction. */
2515 for (ptr = list->prev; ptr != list; )
2517 if (prev != ptr->next)
2531 if (ptr->stmt && stmt_modified_p (ptr->stmt))
2533 fprintf (f, " STMT MODIFIED. - <%p> ", (void *)ptr->stmt);
2534 print_generic_stmt (f, ptr->stmt, TDF_SLIM);
2536 fprintf (f, " IMM ERROR : (use_p : tree - %p:%p)", (void *)ptr,
2538 print_generic_expr (f, USE_FROM_PTR (ptr), TDF_SLIM);
2544 /* Dump all the immediate uses to FILE. */
2547 dump_immediate_uses_for (FILE *file, tree var)
2549 imm_use_iterator iter;
2550 use_operand_p use_p;
2552 gcc_assert (var && TREE_CODE (var) == SSA_NAME);
2554 print_generic_expr (file, var, TDF_SLIM);
2555 fprintf (file, " : -->");
2556 if (has_zero_uses (var))
2557 fprintf (file, " no uses.\n");
2559 if (has_single_use (var))
2560 fprintf (file, " single use.\n");
2562 fprintf (file, "%d uses.\n", num_imm_uses (var));
2564 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
2566 if (use_p->stmt == NULL && use_p->use == NULL)
2567 fprintf (file, "***end of stmt iterator marker***\n");
2569 if (!is_gimple_reg (USE_FROM_PTR (use_p)))
2570 print_generic_stmt (file, USE_STMT (use_p), TDF_VOPS);
2572 print_generic_stmt (file, USE_STMT (use_p), TDF_SLIM);
2574 fprintf(file, "\n");
2578 /* Dump all the immediate uses to FILE. */
2581 dump_immediate_uses (FILE *file)
2586 fprintf (file, "Immediate_uses: \n\n");
2587 for (x = 1; x < num_ssa_names; x++)
2592 dump_immediate_uses_for (file, var);
2597 /* Dump def-use edges on stderr. */
2600 debug_immediate_uses (void)
2602 dump_immediate_uses (stderr);
2606 /* Dump def-use edges on stderr. */
2609 debug_immediate_uses_for (tree var)
2611 dump_immediate_uses_for (stderr, var);
2615 /* Create a new change buffer for the statement pointed by STMT_P and
2616 push the buffer into SCB_STACK. Each change buffer
2617 records state information needed to determine what changed in the
2618 statement. Mainly, this keeps track of symbols that may need to be
2619 put into SSA form, SSA name replacements and other information
2620 needed to keep the SSA form up to date. */
2623 push_stmt_changes (tree *stmt_p)
2630 /* It makes no sense to keep track of PHI nodes. */
2631 if (TREE_CODE (stmt) == PHI_NODE)
2634 buf = xmalloc (sizeof *buf);
2635 memset (buf, 0, sizeof *buf);
2637 buf->stmt_p = stmt_p;
2639 if (stmt_references_memory_p (stmt))
2644 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_VUSE)
2646 tree sym = TREE_CODE (op) == SSA_NAME ? SSA_NAME_VAR (op) : op;
2647 if (buf->loads == NULL)
2648 buf->loads = BITMAP_ALLOC (NULL);
2649 bitmap_set_bit (buf->loads, DECL_UID (sym));
2652 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_VIRTUAL_DEFS)
2654 tree sym = TREE_CODE (op) == SSA_NAME ? SSA_NAME_VAR (op) : op;
2655 if (buf->stores == NULL)
2656 buf->stores = BITMAP_ALLOC (NULL);
2657 bitmap_set_bit (buf->stores, DECL_UID (sym));
2661 VEC_safe_push (scb_t, heap, scb_stack, buf);
2665 /* Given two sets S1 and S2, mark the symbols that differ in S1 and S2
2666 for renaming. The set to mark for renaming is (S1 & ~S2) | (S2 & ~S1). */
2669 mark_difference_for_renaming (bitmap s1, bitmap s2)
2671 if (s1 == NULL && s2 == NULL)
2674 if (s1 && s2 == NULL)
2675 mark_set_for_renaming (s1);
2676 else if (s1 == NULL && s2)
2677 mark_set_for_renaming (s2);
2678 else if (!bitmap_equal_p (s1, s2))
2680 bitmap t1 = BITMAP_ALLOC (NULL);
2681 bitmap t2 = BITMAP_ALLOC (NULL);
2683 bitmap_and_compl (t1, s1, s2);
2684 bitmap_and_compl (t2, s2, s1);
2685 bitmap_ior_into (t1, t2);
2686 mark_set_for_renaming (t1);
2694 /* Pop the top SCB from SCB_STACK and act on the differences between
2695 what was recorded by push_stmt_changes and the current state of
2699 pop_stmt_changes (tree *stmt_p)
2703 bitmap loads, stores;
2708 /* It makes no sense to keep track of PHI nodes. */
2709 if (TREE_CODE (stmt) == PHI_NODE)
2712 buf = VEC_pop (scb_t, scb_stack);
2713 gcc_assert (stmt_p == buf->stmt_p);
2715 /* Force an operand re-scan on the statement and mark any newly
2716 exposed variables. */
2719 /* Determine whether any memory symbols need to be renamed. If the
2720 sets of loads and stores are different after the statement is
2721 modified, then the affected symbols need to be renamed.
2723 Note that it may be possible for the statement to not reference
2724 memory anymore, but we still need to act on the differences in
2725 the sets of symbols. */
2726 loads = stores = NULL;
2727 if (stmt_references_memory_p (stmt))
2732 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_VUSE)
2734 tree sym = TREE_CODE (op) == SSA_NAME ? SSA_NAME_VAR (op) : op;
2736 loads = BITMAP_ALLOC (NULL);
2737 bitmap_set_bit (loads, DECL_UID (sym));
2740 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_VIRTUAL_DEFS)
2742 tree sym = TREE_CODE (op) == SSA_NAME ? SSA_NAME_VAR (op) : op;
2744 stores = BITMAP_ALLOC (NULL);
2745 bitmap_set_bit (stores, DECL_UID (sym));
2747 /* If a V_MAY_DEF turned into a V_MUST_DEF, we will keep
2748 referencing the same symbol, but we still need to mark it
2749 for renaming since the operand scanner stripped its
2752 mark_sym_for_renaming (sym);
2756 /* If LOADS is different from BUF->LOADS, the affected
2757 symbols need to be marked for renaming. */
2758 mark_difference_for_renaming (loads, buf->loads);
2760 /* Similarly for STORES and BUF->STORES. */
2761 mark_difference_for_renaming (stores, buf->stores);
2763 /* Mark all the naked GIMPLE register operands for renaming. */
2764 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF|SSA_OP_USE)
2766 mark_sym_for_renaming (op);
2768 /* FIXME, need to add more finalizers here. Cleanup EH info,
2769 recompute invariants for address expressions, add
2770 SSA replacement mappings, etc. For instance, given
2771 testsuite/gcc.c-torture/compile/pr16808.c, we fold a statement of
2774 # SMT.4_20 = VDEF <SMT.4_16>
2777 So, the VDEF will disappear, but instead of marking SMT.4 for
2778 renaming it would be far more efficient to establish a
2779 replacement mapping that would replace every reference of
2780 SMT.4_20 with SMT.4_16. */
2782 /* Free memory used by the buffer. */
2783 BITMAP_FREE (buf->loads);
2784 BITMAP_FREE (buf->stores);
2785 BITMAP_FREE (loads);
2786 BITMAP_FREE (stores);
2792 /* Discard the topmost change buffer from SCB_STACK. This is useful
2793 when the caller realized that it did not actually modified the
2794 statement. It avoids the expensive operand re-scan. */
2797 discard_stmt_changes (tree *stmt_p)
2802 /* It makes no sense to keep track of PHI nodes. */
2804 if (TREE_CODE (stmt) == PHI_NODE)
2807 buf = VEC_pop (scb_t, scb_stack);
2808 gcc_assert (stmt_p == buf->stmt_p);
2810 /* Free memory used by the buffer. */
2811 BITMAP_FREE (buf->loads);
2812 BITMAP_FREE (buf->stores);