re PR tree-optimization/29680 (Misscompilation of spec2006 gcc)
[platform/upstream/gcc.git] / gcc / tree-ssa-operands.c
1 /* SSA operands management for trees.
2    Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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.  */
20
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"
37
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. 
42
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.
49
50    The routines in this file are concerned with creating this operand cache 
51    from a stmt tree.
52
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.
59
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.
62
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.
66
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.
74
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'.  */
78
79 /* Flags to describe operand properties in helpers.  */
80
81 /* By default, operands are loaded.  */
82 #define opf_none        0
83
84 /* Operand is the target of an assignment expression or a 
85    call-clobbered variable.  */
86 #define opf_is_def      (1 << 0)
87
88 /* Operand is the target of an assignment expression.  */
89 #define opf_kill_def    (1 << 1)
90
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
96    VUSE for 'b'.  */
97 #define opf_no_vops     (1 << 2)
98
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    MODIFY_EXPRs.  */
102 #define opf_non_specific  (1 << 3)
103
104 /* Array for building all the def operands.  */
105 static VEC(tree,heap) *build_defs;
106
107 /* Array for building all the use operands.  */
108 static VEC(tree,heap) *build_uses;
109
110 /* Array for building all the V_MAY_DEF operands.  */
111 static VEC(tree,heap) *build_v_may_defs;
112
113 /* Array for building all the VUSE operands.  */
114 static VEC(tree,heap) *build_vuses;
115
116 /* Array for building all the V_MUST_DEF operands.  */
117 static VEC(tree,heap) *build_v_must_defs;
118
119 /* These arrays are the cached operand vectors for call clobbered calls.  */
120 static bool ops_active = false;
121
122 static GTY (()) struct ssa_operand_memory_d *operand_memory = NULL;
123 static unsigned operand_memory_index;
124
125 static void get_expr_operands (tree, tree *, int);
126
127 static def_optype_p free_defs = NULL;
128 static use_optype_p free_uses = NULL;
129 static vuse_optype_p free_vuses = NULL;
130 static maydef_optype_p free_maydefs = NULL;
131 static mustdef_optype_p free_mustdefs = NULL;
132
133 /* Allocates operand OP of given TYPE from the appropriate free list,
134    or of the new value if the list is empty.  */
135
136 #define ALLOC_OPTYPE(OP, TYPE)                          \
137   do                                                    \
138     {                                                   \
139       TYPE##_optype_p ret = free_##TYPE##s;             \
140       if (ret)                                          \
141         free_##TYPE##s = ret->next;                     \
142       else                                              \
143         ret = ssa_operand_alloc (sizeof (*ret));        \
144       (OP) = ret;                                       \
145     } while (0) 
146
147 /* Return the DECL_UID of the base variable of T.  */
148
149 static inline unsigned
150 get_name_decl (tree t)
151 {
152   if (TREE_CODE (t) != SSA_NAME)
153     return DECL_UID (t);
154   else
155     return DECL_UID (SSA_NAME_VAR (t));
156 }
157
158
159 /* Comparison function for qsort used in operand_build_sort_virtual.  */
160
161 static int
162 operand_build_cmp (const void *p, const void *q)
163 {
164   tree e1 = *((const tree *)p);
165   tree e2 = *((const tree *)q);
166   unsigned int u1,u2;
167
168   u1 = get_name_decl (e1);
169   u2 = get_name_decl (e2);
170
171   /* We want to sort in ascending order.  They can never be equal.  */
172 #ifdef ENABLE_CHECKING
173   gcc_assert (u1 != u2);
174 #endif
175   return (u1 > u2 ? 1 : -1);
176 }
177
178
179 /* Sort the virtual operands in LIST from lowest DECL_UID to highest.  */
180
181 static inline void
182 operand_build_sort_virtual (VEC(tree,heap) *list)
183 {
184   int num = VEC_length (tree, list);
185
186   if (num < 2)
187     return;
188
189   if (num == 2)
190     {
191       if (get_name_decl (VEC_index (tree, list, 0)) 
192           > get_name_decl (VEC_index (tree, list, 1)))
193         {  
194           /* Swap elements if in the wrong order.  */
195           tree tmp = VEC_index (tree, list, 0);
196           VEC_replace (tree, list, 0, VEC_index (tree, list, 1));
197           VEC_replace (tree, list, 1, tmp);
198         }
199       return;
200     }
201
202   /* There are 3 or more elements, call qsort.  */
203   qsort (VEC_address (tree, list), 
204          VEC_length (tree, list), 
205          sizeof (tree),
206          operand_build_cmp);
207 }
208
209
210 /*  Return true if the SSA operands cache is active.  */
211
212 bool
213 ssa_operands_active (void)
214 {
215   return ops_active;
216 }
217
218
219 /* Structure storing statistics on how many call clobbers we have, and
220    how many where avoided.  */
221
222 static struct 
223 {
224   /* Number of call-clobbered ops we attempt to add to calls in
225      add_call_clobber_ops.  */
226   unsigned int clobbered_vars;
227
228   /* Number of write-clobbers (V_MAY_DEFs) avoided by using
229      not_written information.  */
230   unsigned int static_write_clobbers_avoided;
231
232   /* Number of reads (VUSEs) avoided by using not_read information.  */
233   unsigned int static_read_clobbers_avoided;
234   
235   /* Number of write-clobbers avoided because the variable can't escape to
236      this call.  */
237   unsigned int unescapable_clobbers_avoided;
238
239   /* Number of read-only uses we attempt to add to calls in
240      add_call_read_ops.  */
241   unsigned int readonly_clobbers;
242
243   /* Number of read-only uses we avoid using not_read information.  */
244   unsigned int static_readonly_clobbers_avoided;
245 } clobber_stats;
246   
247
248 /* Initialize the operand cache routines.  */
249
250 void
251 init_ssa_operands (void)
252 {
253   build_defs = VEC_alloc (tree, heap, 5);
254   build_uses = VEC_alloc (tree, heap, 10);
255   build_vuses = VEC_alloc (tree, heap, 25);
256   build_v_may_defs = VEC_alloc (tree, heap, 25);
257   build_v_must_defs = VEC_alloc (tree, heap, 25);
258
259   gcc_assert (operand_memory == NULL);
260   operand_memory_index = SSA_OPERAND_MEMORY_SIZE;
261   ops_active = true;
262   memset (&clobber_stats, 0, sizeof (clobber_stats));
263 }
264
265
266 /* Dispose of anything required by the operand routines.  */
267
268 void
269 fini_ssa_operands (void)
270 {
271   struct ssa_operand_memory_d *ptr;
272   VEC_free (tree, heap, build_defs);
273   VEC_free (tree, heap, build_uses);
274   VEC_free (tree, heap, build_v_must_defs);
275   VEC_free (tree, heap, build_v_may_defs);
276   VEC_free (tree, heap, build_vuses);
277   free_defs = NULL;
278   free_uses = NULL;
279   free_vuses = NULL;
280   free_maydefs = NULL;
281   free_mustdefs = NULL;
282   while ((ptr = operand_memory) != NULL)
283     {
284       operand_memory = operand_memory->next;
285       ggc_free (ptr);
286     }
287
288   ops_active = false;
289   
290   if (dump_file && (dump_flags & TDF_STATS))
291     {
292       fprintf (dump_file, "Original clobbered vars:%d\n",
293                clobber_stats.clobbered_vars);
294       fprintf (dump_file, "Static write clobbers avoided:%d\n",
295                clobber_stats.static_write_clobbers_avoided);
296       fprintf (dump_file, "Static read clobbers avoided:%d\n",
297                clobber_stats.static_read_clobbers_avoided);
298       fprintf (dump_file, "Unescapable clobbers avoided:%d\n",
299                clobber_stats.unescapable_clobbers_avoided);
300       fprintf (dump_file, "Original read-only clobbers:%d\n",
301                clobber_stats.readonly_clobbers);
302       fprintf (dump_file, "Static read-only clobbers avoided:%d\n",
303                clobber_stats.static_readonly_clobbers_avoided);
304     }
305 }
306
307
308 /* Return memory for operands of SIZE chunks.  */
309                                                                               
310 static inline void *
311 ssa_operand_alloc (unsigned size)
312 {
313   char *ptr;
314   if (operand_memory_index + size >= SSA_OPERAND_MEMORY_SIZE)
315     {
316       struct ssa_operand_memory_d *ptr;
317       ptr = GGC_NEW (struct ssa_operand_memory_d);
318       ptr->next = operand_memory;
319       operand_memory = ptr;
320       operand_memory_index = 0;
321     }
322   ptr = &(operand_memory->mem[operand_memory_index]);
323   operand_memory_index += size;
324   return ptr;
325 }
326
327
328
329 /* This routine makes sure that PTR is in an immediate use list, and makes
330    sure the stmt pointer is set to the current stmt.  */
331
332 static inline void
333 set_virtual_use_link (use_operand_p ptr, tree stmt)
334 {
335   /*  fold_stmt may have changed the stmt pointers.  */
336   if (ptr->stmt != stmt)
337     ptr->stmt = stmt;
338
339   /* If this use isn't in a list, add it to the correct list.  */
340   if (!ptr->prev)
341     link_imm_use (ptr, *(ptr->use));
342 }
343
344 /* Appends ELT after TO, and moves the TO pointer to ELT.  */
345
346 #define APPEND_OP_AFTER(ELT, TO)        \
347   do                                    \
348     {                                   \
349       (TO)->next = (ELT);               \
350       (TO) = (ELT);                     \
351     } while (0)
352
353 /* Appends head of list FROM after TO, and move both pointers
354    to their successors.  */
355
356 #define MOVE_HEAD_AFTER(FROM, TO)       \
357   do                                    \
358     {                                   \
359       APPEND_OP_AFTER (FROM, TO);       \
360       (FROM) = (FROM)->next;            \
361     } while (0)
362
363 /* Moves OP to appropriate freelist.  OP is set to its successor.  */
364
365 #define MOVE_HEAD_TO_FREELIST(OP, TYPE)                 \
366   do                                                    \
367     {                                                   \
368       TYPE##_optype_p next = (OP)->next;                \
369       (OP)->next = free_##TYPE##s;                      \
370       free_##TYPE##s = (OP);                            \
371       (OP) = next;                                      \
372     } while (0)
373
374 /* Initializes immediate use at USE_PTR to value VAL, and links it to the list
375    of immediate uses.  STMT is the current statement.  */
376
377 #define INITIALIZE_USE(USE_PTR, VAL, STMT)              \
378   do                                                    \
379     {                                                   \
380       (USE_PTR)->use = (VAL);                           \
381       link_imm_use_stmt ((USE_PTR), *(VAL), (STMT));    \
382     } while (0)
383
384 /* Adds OP to the list of defs after LAST, and moves
385    LAST to the new element.  */
386
387 static inline void
388 add_def_op (tree *op, def_optype_p *last)
389 {
390   def_optype_p new;
391
392   ALLOC_OPTYPE (new, def);
393   DEF_OP_PTR (new) = op;
394   APPEND_OP_AFTER (new, *last);  
395 }
396
397 /* Adds OP to the list of uses of statement STMT after LAST, and moves
398    LAST to the new element.  */
399
400 static inline void
401 add_use_op (tree stmt, tree *op, use_optype_p *last)
402 {
403   use_optype_p new;
404
405   ALLOC_OPTYPE (new, use);
406   INITIALIZE_USE (USE_OP_PTR (new), op, stmt);
407   APPEND_OP_AFTER (new, *last);  
408 }
409
410 /* Adds OP to the list of vuses of statement STMT after LAST, and moves
411    LAST to the new element.  */
412
413 static inline void
414 add_vuse_op (tree stmt, tree op, vuse_optype_p *last)
415 {
416   vuse_optype_p new;
417
418   ALLOC_OPTYPE (new, vuse);
419   VUSE_OP (new) = op;
420   INITIALIZE_USE (VUSE_OP_PTR (new), &VUSE_OP (new), stmt);
421   APPEND_OP_AFTER (new, *last);  
422 }
423
424 /* Adds OP to the list of maydefs of statement STMT after LAST, and moves
425    LAST to the new element.  */
426
427 static inline void
428 add_maydef_op (tree stmt, tree op, maydef_optype_p *last)
429 {
430   maydef_optype_p new;
431
432   ALLOC_OPTYPE (new, maydef);
433   MAYDEF_RESULT (new) = op;
434   MAYDEF_OP (new) = op;
435   INITIALIZE_USE (MAYDEF_OP_PTR (new), &MAYDEF_OP (new), stmt);
436   APPEND_OP_AFTER (new, *last);  
437 }
438
439 /* Adds OP to the list of mustdefs of statement STMT after LAST, and moves
440    LAST to the new element.  */
441
442 static inline void
443 add_mustdef_op (tree stmt, tree op, mustdef_optype_p *last)
444 {
445   mustdef_optype_p new;
446
447   ALLOC_OPTYPE (new, mustdef);
448   MUSTDEF_RESULT (new) = op;
449   MUSTDEF_KILL (new) = op;
450   INITIALIZE_USE (MUSTDEF_KILL_PTR (new), &MUSTDEF_KILL (new), stmt);
451   APPEND_OP_AFTER (new, *last);
452 }
453
454 /* Takes elements from build_defs and turns them into def operands of STMT.
455    TODO -- Given that def operands list is not necessarily sorted, merging
456            the operands this way does not make much sense.
457         -- Make build_defs VEC of tree *.  */
458
459 static inline void
460 finalize_ssa_def_ops (tree stmt)
461 {
462   unsigned new_i;
463   struct def_optype_d new_list;
464   def_optype_p old_ops, last;
465   tree *old_base;
466
467   new_list.next = NULL;
468   last = &new_list;
469
470   old_ops = DEF_OPS (stmt);
471
472   new_i = 0;
473   while (old_ops && new_i < VEC_length (tree, build_defs))
474     {
475       tree *new_base = (tree *) VEC_index (tree, build_defs, new_i);
476       old_base = DEF_OP_PTR (old_ops);
477
478       if (old_base == new_base)
479         {
480           /* if variables are the same, reuse this node.  */
481           MOVE_HEAD_AFTER (old_ops, last);
482           new_i++;
483         }
484       else if (old_base < new_base)
485         {
486           /* if old is less than new, old goes to the free list.  */
487           MOVE_HEAD_TO_FREELIST (old_ops, def);
488         }
489       else
490         {
491           /* This is a new operand.  */
492           add_def_op (new_base, &last);
493           new_i++;
494         }
495     }
496
497   /* If there is anything remaining in the build_defs list, simply emit it.  */
498   for ( ; new_i < VEC_length (tree, build_defs); new_i++)
499     add_def_op ((tree *) VEC_index (tree, build_defs, new_i), &last);
500
501   last->next = NULL;
502
503   /* If there is anything in the old list, free it.  */
504   if (old_ops)
505     {
506       old_ops->next = free_defs;
507       free_defs = old_ops;
508     }
509
510   /* Now set the stmt's operands.  */
511   DEF_OPS (stmt) = new_list.next;
512
513 #ifdef ENABLE_CHECKING
514   {
515     def_optype_p ptr;
516     unsigned x = 0;
517     for (ptr = DEF_OPS (stmt); ptr; ptr = ptr->next)
518       x++;
519
520     gcc_assert (x == VEC_length (tree, build_defs));
521   }
522 #endif
523 }
524
525 /* This routine will create stmt operands for STMT from the def build list.  */
526
527 static void
528 finalize_ssa_defs (tree stmt)
529 {
530   unsigned int num = VEC_length (tree, build_defs);
531
532   /* There should only be a single real definition per assignment.  */
533   gcc_assert ((stmt && TREE_CODE (stmt) != MODIFY_EXPR) || num <= 1);
534
535   /* If there is an old list, often the new list is identical, or close, so
536      find the elements at the beginning that are the same as the vector.  */
537   finalize_ssa_def_ops (stmt);
538   VEC_truncate (tree, build_defs, 0);
539 }
540
541 /* Takes elements from build_uses and turns them into use operands of STMT.
542    TODO -- Make build_uses VEC of tree *.  */
543
544 static inline void
545 finalize_ssa_use_ops (tree stmt)
546 {
547   unsigned new_i;
548   struct use_optype_d new_list;
549   use_optype_p old_ops, ptr, last;
550
551   new_list.next = NULL;
552   last = &new_list;
553
554   old_ops = USE_OPS (stmt);
555
556   /* If there is anything in the old list, free it.  */
557   if (old_ops)
558     {
559       for (ptr = old_ops; ptr; ptr = ptr->next)
560         delink_imm_use (USE_OP_PTR (ptr));
561       old_ops->next = free_uses;
562       free_uses = old_ops;
563     }
564
565   /* Now create nodes for all the new nodes.  */
566   for (new_i = 0; new_i < VEC_length (tree, build_uses); new_i++)
567     add_use_op (stmt, (tree *) VEC_index (tree, build_uses, new_i), &last);
568
569   last->next = NULL;
570
571   /* Now set the stmt's operands.  */
572   USE_OPS (stmt) = new_list.next;
573
574 #ifdef ENABLE_CHECKING
575   {
576     unsigned x = 0;
577     for (ptr = USE_OPS (stmt); ptr; ptr = ptr->next)
578       x++;
579
580     gcc_assert (x == VEC_length (tree, build_uses));
581   }
582 #endif
583 }
584
585 /* Return a new use operand vector for STMT, comparing to OLD_OPS_P.  */
586                                                                               
587 static void
588 finalize_ssa_uses (tree stmt)
589 {
590 #ifdef ENABLE_CHECKING
591   {
592     unsigned x;
593     unsigned num = VEC_length (tree, build_uses);
594
595     /* If the pointer to the operand is the statement itself, something is
596        wrong.  It means that we are pointing to a local variable (the 
597        initial call to update_stmt_operands does not pass a pointer to a 
598        statement).  */
599     for (x = 0; x < num; x++)
600       gcc_assert (*((tree *)VEC_index (tree, build_uses, x)) != stmt);
601   }
602 #endif
603   finalize_ssa_use_ops (stmt);
604   VEC_truncate (tree, build_uses, 0);
605 }
606
607
608 /* Takes elements from build_v_may_defs and turns them into maydef operands of
609    STMT.  */
610
611 static inline void
612 finalize_ssa_v_may_def_ops (tree stmt)
613 {
614   unsigned new_i;
615   struct maydef_optype_d new_list;
616   maydef_optype_p old_ops, ptr, last;
617   tree act;
618   unsigned old_base, new_base;
619
620   new_list.next = NULL;
621   last = &new_list;
622
623   old_ops = MAYDEF_OPS (stmt);
624
625   new_i = 0;
626   while (old_ops && new_i < VEC_length (tree, build_v_may_defs))
627     {
628       act = VEC_index (tree, build_v_may_defs, new_i);
629       new_base = get_name_decl (act);
630       old_base = get_name_decl (MAYDEF_OP (old_ops));
631
632       if (old_base == new_base)
633         {
634           /* if variables are the same, reuse this node.  */
635           MOVE_HEAD_AFTER (old_ops, last);
636           set_virtual_use_link (MAYDEF_OP_PTR (last), stmt);
637           new_i++;
638         }
639       else if (old_base < new_base)
640         {
641           /* if old is less than new, old goes to the free list.  */
642           delink_imm_use (MAYDEF_OP_PTR (old_ops));
643           MOVE_HEAD_TO_FREELIST (old_ops, maydef);
644         }
645       else
646         {
647           /* This is a new operand.  */
648           add_maydef_op (stmt, act, &last);
649           new_i++;
650         }
651     }
652
653   /* If there is anything remaining in the build_v_may_defs list, simply emit it.  */
654   for ( ; new_i < VEC_length (tree, build_v_may_defs); new_i++)
655     add_maydef_op (stmt, VEC_index (tree, build_v_may_defs, new_i), &last);
656
657   last->next = NULL;
658
659   /* If there is anything in the old list, free it.  */
660   if (old_ops)
661     {
662       for (ptr = old_ops; ptr; ptr = ptr->next)
663         delink_imm_use (MAYDEF_OP_PTR (ptr));
664       old_ops->next = free_maydefs;
665       free_maydefs = old_ops;
666     }
667
668   /* Now set the stmt's operands.  */
669   MAYDEF_OPS (stmt) = new_list.next;
670
671 #ifdef ENABLE_CHECKING
672   {
673     unsigned x = 0;
674     for (ptr = MAYDEF_OPS (stmt); ptr; ptr = ptr->next)
675       x++;
676
677     gcc_assert (x == VEC_length (tree, build_v_may_defs));
678   }
679 #endif
680 }
681
682 static void
683 finalize_ssa_v_may_defs (tree stmt)
684 {
685   finalize_ssa_v_may_def_ops (stmt);
686 }
687                                                                                
688
689 /* Clear the in_list bits and empty the build array for V_MAY_DEFs.  */
690
691 static inline void
692 cleanup_v_may_defs (void)
693 {
694   unsigned x, num;
695   num = VEC_length (tree, build_v_may_defs);
696
697   for (x = 0; x < num; x++)
698     {
699       tree t = VEC_index (tree, build_v_may_defs, x);
700       if (TREE_CODE (t) != SSA_NAME)
701         {
702           var_ann_t ann = var_ann (t);
703           ann->in_v_may_def_list = 0;
704         }
705     }
706   VEC_truncate (tree, build_v_may_defs, 0);
707 }                                                                             
708
709
710 /* Takes elements from build_vuses and turns them into vuse operands of
711    STMT.  */
712
713 static inline void
714 finalize_ssa_vuse_ops (tree stmt)
715 {
716   unsigned new_i;
717   struct vuse_optype_d new_list;
718   vuse_optype_p old_ops, ptr, last;
719   tree act;
720   unsigned old_base, new_base;
721
722   new_list.next = NULL;
723   last = &new_list;
724
725   old_ops = VUSE_OPS (stmt);
726
727   new_i = 0;
728   while (old_ops && new_i < VEC_length (tree, build_vuses))
729     {
730       act = VEC_index (tree, build_vuses, new_i);
731       new_base = get_name_decl (act);
732       old_base = get_name_decl (VUSE_OP (old_ops));
733
734       if (old_base == new_base)
735         {
736           /* if variables are the same, reuse this node.  */
737           MOVE_HEAD_AFTER (old_ops, last);
738           set_virtual_use_link (VUSE_OP_PTR (last), stmt);
739           new_i++;
740         }
741       else if (old_base < new_base)
742         {
743           /* if old is less than new, old goes to the free list.  */
744           delink_imm_use (USE_OP_PTR (old_ops));
745           MOVE_HEAD_TO_FREELIST (old_ops, vuse);
746         }
747       else
748         {
749           /* This is a new operand.  */
750           add_vuse_op (stmt, act, &last);
751           new_i++;
752         }
753     }
754
755   /* If there is anything remaining in the build_vuses list, simply emit it.  */
756   for ( ; new_i < VEC_length (tree, build_vuses); new_i++)
757     add_vuse_op (stmt, VEC_index (tree, build_vuses, new_i), &last);
758
759   last->next = NULL;
760
761   /* If there is anything in the old list, free it.  */
762   if (old_ops)
763     {
764       for (ptr = old_ops; ptr; ptr = ptr->next)
765         delink_imm_use (VUSE_OP_PTR (ptr));
766       old_ops->next = free_vuses;
767       free_vuses = old_ops;
768     }
769
770   /* Now set the stmt's operands.  */
771   VUSE_OPS (stmt) = new_list.next;
772
773 #ifdef ENABLE_CHECKING
774   {
775     unsigned x = 0;
776     for (ptr = VUSE_OPS (stmt); ptr; ptr = ptr->next)
777       x++;
778
779     gcc_assert (x == VEC_length (tree, build_vuses));
780   }
781 #endif
782 }
783                                                                               
784 /* Return a new VUSE operand vector, comparing to OLD_OPS_P.  */
785                                                                               
786 static void
787 finalize_ssa_vuses (tree stmt)
788 {
789   unsigned num, num_v_may_defs;
790   unsigned vuse_index;
791
792   /* Remove superfluous VUSE operands.  If the statement already has a
793      V_MAY_DEF operation for a variable 'a', then a VUSE for 'a' is
794      not needed because V_MAY_DEFs imply a VUSE of the variable.  For
795      instance, suppose that variable 'a' is aliased:
796
797               # VUSE <a_2>
798               # a_3 = V_MAY_DEF <a_2>
799               a = a + 1;
800
801      The VUSE <a_2> is superfluous because it is implied by the
802      V_MAY_DEF operation.  */
803   num = VEC_length (tree, build_vuses);
804   num_v_may_defs = VEC_length (tree, build_v_may_defs);
805
806   if (num > 0 && num_v_may_defs > 0)
807     {
808       for (vuse_index = 0; vuse_index < VEC_length (tree, build_vuses); )
809         {
810           tree vuse;
811           vuse = VEC_index (tree, build_vuses, vuse_index);
812           if (TREE_CODE (vuse) != SSA_NAME)
813             {
814               var_ann_t ann = var_ann (vuse);
815               ann->in_vuse_list = 0;
816               if (ann->in_v_may_def_list)
817                 {
818                   VEC_ordered_remove (tree, build_vuses, vuse_index);
819                   continue;
820                 }
821             }
822           vuse_index++;
823         }
824     }
825   else
826     {
827       /* Clear out the in_list bits.  */
828       for (vuse_index = 0;
829           vuse_index < VEC_length (tree, build_vuses);
830           vuse_index++)
831         {
832           tree t = VEC_index (tree, build_vuses, vuse_index);
833           if (TREE_CODE (t) != SSA_NAME)
834             {
835               var_ann_t ann = var_ann (t);
836               ann->in_vuse_list = 0;
837             }
838         }
839     }
840
841   finalize_ssa_vuse_ops (stmt);
842
843   /* The V_MAY_DEF build vector wasn't cleaned up because we needed it.  */
844   cleanup_v_may_defs ();
845                                                                               
846   /* Free the VUSEs build vector.  */
847   VEC_truncate (tree, build_vuses, 0);
848
849 }
850
851 /* Takes elements from build_v_must_defs and turns them into mustdef operands of
852    STMT.  */
853
854 static inline void
855 finalize_ssa_v_must_def_ops (tree stmt)
856 {
857   unsigned new_i;
858   struct mustdef_optype_d new_list;
859   mustdef_optype_p old_ops, ptr, last;
860   tree act;
861   unsigned old_base, new_base;
862
863   new_list.next = NULL;
864   last = &new_list;
865
866   old_ops = MUSTDEF_OPS (stmt);
867
868   new_i = 0;
869   while (old_ops && new_i < VEC_length (tree, build_v_must_defs))
870     {
871       act = VEC_index (tree, build_v_must_defs, new_i);
872       new_base = get_name_decl (act);
873       old_base = get_name_decl (MUSTDEF_KILL (old_ops));
874
875       if (old_base == new_base)
876         {
877           /* If variables are the same, reuse this node.  */
878           MOVE_HEAD_AFTER (old_ops, last);
879           set_virtual_use_link (MUSTDEF_KILL_PTR (last), stmt);
880           new_i++;
881         }
882       else if (old_base < new_base)
883         {
884           /* If old is less than new, old goes to the free list.  */
885           delink_imm_use (MUSTDEF_KILL_PTR (old_ops));
886           MOVE_HEAD_TO_FREELIST (old_ops, mustdef);
887         }
888       else
889         {
890           /* This is a new operand.  */
891           add_mustdef_op (stmt, act, &last);
892           new_i++;
893         }
894     }
895
896   /* If there is anything remaining in the build_v_must_defs list, simply emit it.  */
897   for ( ; new_i < VEC_length (tree, build_v_must_defs); new_i++)
898     add_mustdef_op (stmt, VEC_index (tree, build_v_must_defs, new_i), &last);
899
900   last->next = NULL;
901
902   /* If there is anything in the old list, free it.  */
903   if (old_ops)
904     {
905       for (ptr = old_ops; ptr; ptr = ptr->next)
906         delink_imm_use (MUSTDEF_KILL_PTR (ptr));
907       old_ops->next = free_mustdefs;
908       free_mustdefs = old_ops;
909     }
910
911   /* Now set the stmt's operands.  */
912   MUSTDEF_OPS (stmt) = new_list.next;
913
914 #ifdef ENABLE_CHECKING
915   {
916     unsigned x = 0;
917     for (ptr = MUSTDEF_OPS (stmt); ptr; ptr = ptr->next)
918       x++;
919
920     gcc_assert (x == VEC_length (tree, build_v_must_defs));
921   }
922 #endif
923 }
924
925 static void
926 finalize_ssa_v_must_defs (tree stmt)
927 {
928   /* In the presence of subvars, there may be more than one V_MUST_DEF
929      per statement (one for each subvar).  It is a bit expensive to
930      verify that all must-defs in a statement belong to subvars if
931      there is more than one must-def, so we don't do it.  Suffice to
932      say, if you reach here without having subvars, and have num >1,
933      you have hit a bug.  */
934   finalize_ssa_v_must_def_ops (stmt);
935   VEC_truncate (tree, build_v_must_defs, 0);
936 }
937
938
939 /* Finalize all the build vectors, fill the new ones into INFO.  */
940                                                                               
941 static inline void
942 finalize_ssa_stmt_operands (tree stmt)
943 {
944   finalize_ssa_defs (stmt);
945   finalize_ssa_uses (stmt);
946   finalize_ssa_v_must_defs (stmt);
947   finalize_ssa_v_may_defs (stmt);
948   finalize_ssa_vuses (stmt);
949 }
950
951
952 /* Start the process of building up operands vectors in INFO.  */
953
954 static inline void
955 start_ssa_stmt_operands (void)
956 {
957   gcc_assert (VEC_length (tree, build_defs) == 0);
958   gcc_assert (VEC_length (tree, build_uses) == 0);
959   gcc_assert (VEC_length (tree, build_vuses) == 0);
960   gcc_assert (VEC_length (tree, build_v_may_defs) == 0);
961   gcc_assert (VEC_length (tree, build_v_must_defs) == 0);
962 }
963
964
965 /* Add DEF_P to the list of pointers to operands.  */
966
967 static inline void
968 append_def (tree *def_p)
969 {
970   VEC_safe_push (tree, heap, build_defs, (tree)def_p);
971 }
972
973
974 /* Add USE_P to the list of pointers to operands.  */
975
976 static inline void
977 append_use (tree *use_p)
978 {
979   VEC_safe_push (tree, heap, build_uses, (tree)use_p);
980 }
981
982
983 /* Add a new virtual may def for variable VAR to the build array.  */
984
985 static inline void
986 append_v_may_def (tree var)
987 {
988   if (TREE_CODE (var) != SSA_NAME)
989     {
990       var_ann_t ann = get_var_ann (var);
991
992       /* Don't allow duplicate entries.  */
993       if (ann->in_v_may_def_list)
994         return;
995       ann->in_v_may_def_list = 1;
996     }
997
998   VEC_safe_push (tree, heap, build_v_may_defs, (tree)var);
999 }
1000
1001
1002 /* Add VAR to the list of virtual uses.  */
1003
1004 static inline void
1005 append_vuse (tree var)
1006 {
1007   /* Don't allow duplicate entries.  */
1008   if (TREE_CODE (var) != SSA_NAME)
1009     {
1010       var_ann_t ann = get_var_ann (var);
1011
1012       if (ann->in_vuse_list || ann->in_v_may_def_list)
1013         return;
1014       ann->in_vuse_list = 1;
1015     }
1016
1017   VEC_safe_push (tree, heap, build_vuses, (tree)var);
1018 }
1019
1020
1021 /* Add VAR to the list of virtual must definitions for INFO.  */
1022
1023 static inline void
1024 append_v_must_def (tree var)
1025 {
1026   unsigned i;
1027
1028   /* Don't allow duplicate entries.  */
1029   for (i = 0; i < VEC_length (tree, build_v_must_defs); i++)
1030     if (var == VEC_index (tree, build_v_must_defs, i))
1031       return;
1032
1033   VEC_safe_push (tree, heap, build_v_must_defs, (tree)var);
1034 }
1035
1036
1037 /* REF is a tree that contains the entire pointer dereference
1038    expression, if available, or NULL otherwise.  ALIAS is the variable
1039    we are asking if REF can access.  OFFSET and SIZE come from the
1040    memory access expression that generated this virtual operand.  */
1041
1042 static bool
1043 access_can_touch_variable (tree ref, tree alias, HOST_WIDE_INT offset,
1044                            HOST_WIDE_INT size)
1045 {  
1046   bool offsetgtz = offset > 0;
1047   unsigned HOST_WIDE_INT uoffset = (unsigned HOST_WIDE_INT) offset;
1048   tree base = ref ? get_base_address (ref) : NULL;
1049
1050   /* If ALIAS is .GLOBAL_VAR then the memory reference REF must be
1051      using a call-clobbered memory tag.  By definition, call-clobbered
1052      memory tags can always touch .GLOBAL_VAR.  */
1053   if (alias == global_var)
1054     return true;
1055
1056   /* We cannot prune nonlocal aliases because they are not type
1057      specific.  */
1058   if (alias == nonlocal_all)
1059     return true;
1060
1061   /* If ALIAS is an SFT, it can't be touched if the offset     
1062      and size of the access is not overlapping with the SFT offset and
1063      size.  This is only true if we are accessing through a pointer
1064      to a type that is the same as SFT_PARENT_VAR.  Otherwise, we may
1065      be accessing through a pointer to some substruct of the
1066      structure, and if we try to prune there, we will have the wrong
1067      offset, and get the wrong answer.
1068      i.e., we can't prune without more work if we have something like
1069
1070      struct gcc_target
1071      {
1072        struct asm_out
1073        {
1074          const char *byte_op;
1075          struct asm_int_op
1076          {    
1077            const char *hi;
1078          } aligned_op;
1079        } asm_out;
1080      } targetm;
1081      
1082      foo = &targetm.asm_out.aligned_op;
1083      return foo->hi;
1084
1085      SFT.1, which represents hi, will have SFT_OFFSET=32 because in
1086      terms of SFT_PARENT_VAR, that is where it is.
1087      However, the access through the foo pointer will be at offset 0.  */
1088   if (size != -1
1089       && TREE_CODE (alias) == STRUCT_FIELD_TAG
1090       && base
1091       && TREE_TYPE (base) == TREE_TYPE (SFT_PARENT_VAR (alias))
1092       && !overlap_subvar (offset, size, alias, NULL))
1093     {
1094 #ifdef ACCESS_DEBUGGING
1095       fprintf (stderr, "Access to ");
1096       print_generic_expr (stderr, ref, 0);
1097       fprintf (stderr, " may not touch ");
1098       print_generic_expr (stderr, alias, 0);
1099       fprintf (stderr, " in function %s\n", get_name (current_function_decl));
1100 #endif
1101       return false;
1102     }
1103
1104   /* Without strict aliasing, it is impossible for a component access
1105      through a pointer to touch a random variable, unless that
1106      variable *is* a structure or a pointer.
1107
1108      That is, given p->c, and some random global variable b,
1109      there is no legal way that p->c could be an access to b.
1110      
1111      Without strict aliasing on, we consider it legal to do something
1112      like:
1113
1114      struct foos { int l; };
1115      int foo;
1116      static struct foos *getfoo(void);
1117      int main (void)
1118      {
1119        struct foos *f = getfoo();
1120        f->l = 1;
1121        foo = 2;
1122        if (f->l == 1)
1123          abort();
1124        exit(0);
1125      }
1126      static struct foos *getfoo(void)     
1127      { return (struct foos *)&foo; }
1128      
1129      (taken from 20000623-1.c)
1130
1131      The docs also say/imply that access through union pointers
1132      is legal (but *not* if you take the address of the union member,
1133      i.e. the inverse), such that you can do
1134
1135      typedef union {
1136        int d;
1137      } U;
1138
1139      int rv;
1140      void breakme()
1141      {
1142        U *rv0;
1143        U *pretmp = (U*)&rv;
1144        rv0 = pretmp;
1145        rv0->d = 42;    
1146      }
1147      To implement this, we just punt on accesses through union
1148      pointers entirely.
1149   */
1150   else if (ref 
1151            && flag_strict_aliasing
1152            && TREE_CODE (ref) != INDIRECT_REF
1153            && !MTAG_P (alias)
1154            && (TREE_CODE (base) != INDIRECT_REF
1155                || TREE_CODE (TREE_TYPE (base)) != UNION_TYPE)
1156            && !AGGREGATE_TYPE_P (TREE_TYPE (alias))
1157            && TREE_CODE (TREE_TYPE (alias)) != COMPLEX_TYPE
1158 #if 0
1159            /* FIXME: PR tree-optimization/29680.  */
1160            && !var_ann (alias)->is_heapvar
1161 #else
1162            && !POINTER_TYPE_P (TREE_TYPE (alias))
1163 #endif
1164            /* When the struct has may_alias attached to it, we need not to
1165               return true.  */
1166            && get_alias_set (base))
1167     {
1168 #ifdef ACCESS_DEBUGGING
1169       fprintf (stderr, "Access to ");
1170       print_generic_expr (stderr, ref, 0);
1171       fprintf (stderr, " may not touch ");
1172       print_generic_expr (stderr, alias, 0);
1173       fprintf (stderr, " in function %s\n", get_name (current_function_decl));
1174 #endif
1175       return false;
1176     }
1177
1178   /* If the offset of the access is greater than the size of one of
1179      the possible aliases, it can't be touching that alias, because it
1180      would be past the end of the structure.  */
1181   else if (ref
1182            && flag_strict_aliasing
1183            && TREE_CODE (ref) != INDIRECT_REF
1184            && !MTAG_P (alias)
1185            && !POINTER_TYPE_P (TREE_TYPE (alias))
1186            && offsetgtz
1187            && DECL_SIZE (alias)
1188            && TREE_CODE (DECL_SIZE (alias)) == INTEGER_CST
1189            && uoffset > TREE_INT_CST_LOW (DECL_SIZE (alias)))
1190     {
1191 #ifdef ACCESS_DEBUGGING
1192       fprintf (stderr, "Access to ");
1193       print_generic_expr (stderr, ref, 0);
1194       fprintf (stderr, " may not touch ");
1195       print_generic_expr (stderr, alias, 0);
1196       fprintf (stderr, " in function %s\n", get_name (current_function_decl));
1197 #endif
1198       return false;
1199     }      
1200
1201   return true;
1202 }
1203
1204
1205 /* Add VAR to the virtual operands array.  FLAGS is as in
1206    get_expr_operands.  FULL_REF is a tree that contains the entire
1207    pointer dereference expression, if available, or NULL otherwise.
1208    OFFSET and SIZE come from the memory access expression that
1209    generated this virtual operand.  FOR_CLOBBER is true is this is
1210    adding a virtual operand for a call clobber.  */
1211
1212 static void 
1213 add_virtual_operand (tree var, stmt_ann_t s_ann, int flags,
1214                      tree full_ref, HOST_WIDE_INT offset,
1215                      HOST_WIDE_INT size, bool for_clobber)
1216 {
1217   VEC(tree,gc) *aliases;
1218   tree sym;
1219   var_ann_t v_ann;
1220   
1221   sym = (TREE_CODE (var) == SSA_NAME ? SSA_NAME_VAR (var) : var);
1222   v_ann = var_ann (sym);
1223   
1224   /* Mark statements with volatile operands.  Optimizers should back
1225      off from statements having volatile operands.  */
1226   if (TREE_THIS_VOLATILE (sym) && s_ann)
1227     s_ann->has_volatile_ops = true;
1228
1229   /* If the variable cannot be modified and this is a V_MAY_DEF change
1230      it into a VUSE.  This happens when read-only variables are marked
1231      call-clobbered and/or aliased to writable variables.  So we only
1232      check that this only happens on non-specific stores.
1233
1234      Note that if this is a specific store, i.e. associated with a
1235      modify_expr, then we can't suppress the V_MAY_DEF, lest we run
1236      into validation problems.
1237
1238      This can happen when programs cast away const, leaving us with a
1239      store to read-only memory.  If the statement is actually executed
1240      at runtime, then the program is ill formed.  If the statement is
1241      not executed then all is well.  At the very least, we cannot ICE.  */
1242   if ((flags & opf_non_specific) && unmodifiable_var_p (var))
1243     flags &= ~(opf_is_def | opf_kill_def);
1244   
1245   /* The variable is not a GIMPLE register.  Add it (or its aliases) to
1246      virtual operands, unless the caller has specifically requested
1247      not to add virtual operands (used when adding operands inside an
1248      ADDR_EXPR expression).  */
1249   if (flags & opf_no_vops)
1250     return;
1251   
1252   aliases = v_ann->may_aliases;
1253   if (aliases == NULL)
1254     {
1255       /* The variable is not aliased or it is an alias tag.  */
1256       if (flags & opf_is_def)
1257         {
1258           if (flags & opf_kill_def)
1259             {
1260               /* V_MUST_DEF for non-aliased, non-GIMPLE register 
1261                  variable definitions.  */
1262               gcc_assert (!MTAG_P (var)
1263                           || TREE_CODE (var) == STRUCT_FIELD_TAG);
1264               append_v_must_def (var);
1265             }
1266           else
1267             {
1268               /* Add a V_MAY_DEF for call-clobbered variables and
1269                  memory tags.  */
1270               append_v_may_def (var);
1271             }
1272         }
1273       else
1274         append_vuse (var);
1275     }
1276   else
1277     {
1278       unsigned i;
1279       tree al;
1280       
1281       /* The variable is aliased.  Add its aliases to the virtual
1282          operands.  */
1283       gcc_assert (VEC_length (tree, aliases) != 0);
1284       
1285       if (flags & opf_is_def)
1286         {
1287           
1288           bool none_added = true;
1289
1290           for (i = 0; VEC_iterate (tree, aliases, i, al); i++)
1291             {
1292               if (!access_can_touch_variable (full_ref, al, offset, size))
1293                 continue;
1294               
1295               none_added = false;
1296               append_v_may_def (al);
1297             }
1298
1299           /* If the variable is also an alias tag, add a virtual
1300              operand for it, otherwise we will miss representing
1301              references to the members of the variable's alias set.          
1302              This fixes the bug in gcc.c-torture/execute/20020503-1.c.
1303              
1304              It is also necessary to add bare defs on clobbers for
1305              SMT's, so that bare SMT uses caused by pruning all the
1306              aliases will link up properly with calls.   In order to
1307              keep the number of these bare defs we add down to the
1308              minimum necessary, we keep track of which SMT's were used
1309              alone in statement vdefs or VUSEs.  */
1310           if (v_ann->is_aliased
1311               || none_added
1312               || (TREE_CODE (var) == SYMBOL_MEMORY_TAG
1313                   && for_clobber
1314                   && SMT_USED_ALONE (var)))
1315             {
1316               /* Every bare SMT def we add should have SMT_USED_ALONE
1317                  set on it, or else we will get the wrong answer on
1318                  clobbers.  */
1319               if (none_added
1320                   && !updating_used_alone && aliases_computed_p
1321                   && TREE_CODE (var) == SYMBOL_MEMORY_TAG)
1322                 gcc_assert (SMT_USED_ALONE (var));
1323
1324               append_v_may_def (var);
1325             }
1326         }
1327       else
1328         {
1329           bool none_added = true;
1330           for (i = 0; VEC_iterate (tree, aliases, i, al); i++)
1331             {
1332               if (!access_can_touch_variable (full_ref, al, offset, size))
1333                 continue;
1334               none_added = false;
1335               append_vuse (al);
1336             }
1337
1338           /* Similarly, append a virtual uses for VAR itself, when
1339              it is an alias tag.  */
1340           if (v_ann->is_aliased || none_added)
1341             append_vuse (var);
1342         }
1343     }
1344 }
1345
1346
1347 /* Add *VAR_P to the appropriate operand array for S_ANN.  FLAGS is as in
1348    get_expr_operands.  If *VAR_P is a GIMPLE register, it will be added to
1349    the statement's real operands, otherwise it is added to virtual
1350    operands.  */
1351
1352 static void
1353 add_stmt_operand (tree *var_p, stmt_ann_t s_ann, int flags)
1354 {
1355   bool is_real_op;
1356   tree var, sym;
1357   var_ann_t v_ann;
1358
1359   var = *var_p;
1360   gcc_assert (SSA_VAR_P (var));
1361
1362   is_real_op = is_gimple_reg (var);
1363
1364   /* If this is a real operand, the operand is either an SSA name or a 
1365      decl.  Virtual operands may only be decls.  */
1366   gcc_assert (is_real_op || DECL_P (var));
1367
1368   sym = (TREE_CODE (var) == SSA_NAME ? SSA_NAME_VAR (var) : var);
1369   v_ann = var_ann (sym);
1370
1371   /* Mark statements with volatile operands.  Optimizers should back
1372      off from statements having volatile operands.  */
1373   if (TREE_THIS_VOLATILE (sym) && s_ann)
1374     s_ann->has_volatile_ops = true;
1375
1376   if (is_real_op)
1377     {
1378       /* The variable is a GIMPLE register.  Add it to real operands.  */
1379       if (flags & opf_is_def)
1380         append_def (var_p);
1381       else
1382         append_use (var_p);
1383     }
1384   else
1385     add_virtual_operand (var, s_ann, flags, NULL_TREE, 0, -1, false);
1386 }
1387
1388
1389 /* A subroutine of get_expr_operands to handle INDIRECT_REF,
1390    ALIGN_INDIRECT_REF and MISALIGNED_INDIRECT_REF.  
1391
1392    STMT is the statement being processed, EXPR is the INDIRECT_REF
1393       that got us here.
1394    
1395    FLAGS is as in get_expr_operands.
1396
1397    FULL_REF contains the full pointer dereference expression, if we
1398       have it, or NULL otherwise.
1399
1400    OFFSET and SIZE are the location of the access inside the
1401       dereferenced pointer, if known.
1402
1403    RECURSE_ON_BASE should be set to true if we want to continue
1404       calling get_expr_operands on the base pointer, and false if
1405       something else will do it for us.  */
1406
1407 static void
1408 get_indirect_ref_operands (tree stmt, tree expr, int flags,
1409                            tree full_ref,
1410                            HOST_WIDE_INT offset, HOST_WIDE_INT size,
1411                            bool recurse_on_base)
1412 {
1413   tree *pptr = &TREE_OPERAND (expr, 0);
1414   tree ptr = *pptr;
1415   stmt_ann_t s_ann = stmt_ann (stmt);
1416
1417   /* Stores into INDIRECT_REF operands are never killing definitions.  */
1418   flags &= ~opf_kill_def;
1419
1420   if (SSA_VAR_P (ptr))
1421     {
1422       struct ptr_info_def *pi = NULL;
1423
1424       /* If PTR has flow-sensitive points-to information, use it.  */
1425       if (TREE_CODE (ptr) == SSA_NAME
1426           && (pi = SSA_NAME_PTR_INFO (ptr)) != NULL
1427           && pi->name_mem_tag)
1428         {
1429           /* PTR has its own memory tag.  Use it.  */
1430           add_virtual_operand (pi->name_mem_tag, s_ann, flags,
1431                                full_ref, offset, size, false);
1432         }
1433       else
1434         {
1435           /* If PTR is not an SSA_NAME or it doesn't have a name
1436              tag, use its symbol memory tag.  */
1437           var_ann_t v_ann;
1438
1439           /* If we are emitting debugging dumps, display a warning if
1440              PTR is an SSA_NAME with no flow-sensitive alias
1441              information.  That means that we may need to compute
1442              aliasing again.  */
1443           if (dump_file
1444               && TREE_CODE (ptr) == SSA_NAME
1445               && pi == NULL)
1446             {
1447               fprintf (dump_file,
1448                   "NOTE: no flow-sensitive alias info for ");
1449               print_generic_expr (dump_file, ptr, dump_flags);
1450               fprintf (dump_file, " in ");
1451               print_generic_stmt (dump_file, stmt, dump_flags);
1452             }
1453
1454           if (TREE_CODE (ptr) == SSA_NAME)
1455             ptr = SSA_NAME_VAR (ptr);
1456           v_ann = var_ann (ptr);
1457
1458           if (v_ann->symbol_mem_tag)
1459             add_virtual_operand (v_ann->symbol_mem_tag, s_ann, flags,
1460                                  full_ref, offset, size, false);
1461         }
1462     }
1463   else if (TREE_CODE (ptr) == INTEGER_CST)
1464     {
1465       /* If a constant is used as a pointer, we can't generate a real
1466          operand for it but we mark the statement volatile to prevent
1467          optimizations from messing things up.  */
1468       if (s_ann)
1469         s_ann->has_volatile_ops = true;
1470       return;
1471     }
1472   else
1473     {
1474       /* Ok, this isn't even is_gimple_min_invariant.  Something's broke.  */
1475       gcc_unreachable ();
1476     }
1477
1478   /* If requested, add a USE operand for the base pointer.  */
1479   if (recurse_on_base)
1480     get_expr_operands (stmt, pptr, opf_none);
1481 }
1482
1483
1484 /* A subroutine of get_expr_operands to handle TARGET_MEM_REF.  */
1485
1486 static void
1487 get_tmr_operands (tree stmt, tree expr, int flags)
1488 {
1489   tree tag = TMR_TAG (expr), ref;
1490   HOST_WIDE_INT offset, size, maxsize;
1491   subvar_t svars, sv;
1492   stmt_ann_t s_ann = stmt_ann (stmt);
1493
1494   /* First record the real operands.  */
1495   get_expr_operands (stmt, &TMR_BASE (expr), opf_none);
1496   get_expr_operands (stmt, &TMR_INDEX (expr), opf_none);
1497
1498   /* MEM_REFs should never be killing.  */
1499   flags &= ~opf_kill_def;
1500
1501   if (TMR_SYMBOL (expr))
1502     {
1503       stmt_ann_t ann = stmt_ann (stmt);
1504       add_to_addressable_set (TMR_SYMBOL (expr), &ann->addresses_taken);
1505     }
1506
1507   if (!tag)
1508     {
1509       /* Something weird, so ensure that we will be careful.  */
1510       stmt_ann (stmt)->has_volatile_ops = true;
1511       return;
1512     }
1513
1514   if (DECL_P (tag))
1515     {
1516       get_expr_operands (stmt, &tag, flags);
1517       return;
1518     }
1519
1520   ref = get_ref_base_and_extent (tag, &offset, &size, &maxsize);
1521   gcc_assert (ref != NULL_TREE);
1522   svars = get_subvars_for_var (ref);
1523   for (sv = svars; sv; sv = sv->next)
1524     {
1525       bool exact;               
1526       if (overlap_subvar (offset, maxsize, sv->var, &exact))
1527         {
1528           int subvar_flags = flags;
1529           if (!exact || size != maxsize)
1530             subvar_flags &= ~opf_kill_def;
1531           add_stmt_operand (&sv->var, s_ann, subvar_flags);
1532         }
1533     }
1534 }
1535
1536
1537 /* Add clobbering definitions for .GLOBAL_VAR or for each of the call
1538    clobbered variables in the function.  */
1539
1540 static void
1541 add_call_clobber_ops (tree stmt, tree callee)
1542 {
1543   unsigned u;
1544   bitmap_iterator bi;
1545   stmt_ann_t s_ann = stmt_ann (stmt);
1546   bitmap not_read_b, not_written_b;
1547   
1548   /* Functions that are not const, pure or never return may clobber
1549      call-clobbered variables.  */
1550   if (s_ann)
1551     s_ann->makes_clobbering_call = true;
1552
1553   /* If we created .GLOBAL_VAR earlier, just use it.  See compute_may_aliases 
1554      for the heuristic used to decide whether to create .GLOBAL_VAR or not.  */
1555   if (global_var)
1556     {
1557       add_stmt_operand (&global_var, s_ann, opf_is_def);
1558       return;
1559     }
1560
1561   /* Get info for local and module level statics.  There is a bit
1562      set for each static if the call being processed does not read
1563      or write that variable.  */
1564   not_read_b = callee ? ipa_reference_get_not_read_global (callee) : NULL; 
1565   not_written_b = callee ? ipa_reference_get_not_written_global (callee) : NULL; 
1566   /* Add a V_MAY_DEF operand for every call clobbered variable.  */
1567   EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, u, bi)
1568     {
1569       tree var = referenced_var_lookup (u);
1570       unsigned int escape_mask = var_ann (var)->escape_mask;
1571       tree real_var = var;
1572       bool not_read;
1573       bool not_written;
1574       
1575       /* Not read and not written are computed on regular vars, not
1576          subvars, so look at the parent var if this is an SFT. */
1577       if (TREE_CODE (var) == STRUCT_FIELD_TAG)
1578         real_var = SFT_PARENT_VAR (var);
1579
1580       not_read = not_read_b ? bitmap_bit_p (not_read_b, 
1581                                             DECL_UID (real_var)) : false;
1582       not_written = not_written_b ? bitmap_bit_p (not_written_b, 
1583                                                   DECL_UID (real_var)) : false;
1584       gcc_assert (!unmodifiable_var_p (var));
1585       
1586       clobber_stats.clobbered_vars++;
1587
1588       /* See if this variable is really clobbered by this function.  */
1589
1590       /* Trivial case: Things escaping only to pure/const are not
1591          clobbered by non-pure-const, and only read by pure/const. */
1592       if ((escape_mask & ~(ESCAPE_TO_PURE_CONST)) == 0)
1593         {
1594           tree call = get_call_expr_in (stmt);
1595           if (call_expr_flags (call) & (ECF_CONST | ECF_PURE))
1596             {
1597               add_stmt_operand (&var, s_ann, opf_none);
1598               clobber_stats.unescapable_clobbers_avoided++;
1599               continue;
1600             }
1601           else
1602             {
1603               clobber_stats.unescapable_clobbers_avoided++;
1604               continue;
1605             }
1606         }
1607             
1608       if (not_written)
1609         {
1610           clobber_stats.static_write_clobbers_avoided++;
1611           if (!not_read)
1612             add_stmt_operand (&var, s_ann, opf_none);
1613           else
1614             clobber_stats.static_read_clobbers_avoided++;
1615         }
1616       else
1617         add_virtual_operand (var, s_ann, opf_is_def, NULL, 0, -1, true);
1618     }
1619 }
1620
1621
1622 /* Add VUSE operands for .GLOBAL_VAR or all call clobbered variables in the
1623    function.  */
1624
1625 static void
1626 add_call_read_ops (tree stmt, tree callee)
1627 {
1628   unsigned u;
1629   bitmap_iterator bi;
1630   stmt_ann_t s_ann = stmt_ann (stmt);
1631   bitmap not_read_b;
1632
1633   /* if the function is not pure, it may reference memory.  Add
1634      a VUSE for .GLOBAL_VAR if it has been created.  See add_referenced_var
1635      for the heuristic used to decide whether to create .GLOBAL_VAR.  */
1636   if (global_var)
1637     {
1638       add_stmt_operand (&global_var, s_ann, opf_none);
1639       return;
1640     }
1641   
1642   not_read_b = callee ? ipa_reference_get_not_read_global (callee) : NULL; 
1643
1644   /* Add a VUSE for each call-clobbered variable.  */
1645   EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, u, bi)
1646     {
1647       tree var = referenced_var (u);
1648       tree real_var = var;
1649       bool not_read;
1650       
1651       clobber_stats.readonly_clobbers++;
1652
1653       /* Not read and not written are computed on regular vars, not
1654          subvars, so look at the parent var if this is an SFT. */
1655
1656       if (TREE_CODE (var) == STRUCT_FIELD_TAG)
1657         real_var = SFT_PARENT_VAR (var);
1658
1659       not_read = not_read_b ? bitmap_bit_p (not_read_b, DECL_UID (real_var))
1660                             : false;
1661       
1662       if (not_read)
1663         {
1664           clobber_stats.static_readonly_clobbers_avoided++;
1665           continue;
1666         }
1667             
1668       add_stmt_operand (&var, s_ann, opf_none | opf_non_specific);
1669     }
1670 }
1671
1672
1673 /* A subroutine of get_expr_operands to handle CALL_EXPR.  */
1674
1675 static void
1676 get_call_expr_operands (tree stmt, tree expr)
1677 {
1678   tree op;
1679   int call_flags = call_expr_flags (expr);
1680
1681   /* If aliases have been computed already, add V_MAY_DEF or V_USE
1682      operands for all the symbols that have been found to be
1683      call-clobbered.
1684      
1685      Note that if aliases have not been computed, the global effects
1686      of calls will not be included in the SSA web. This is fine
1687      because no optimizer should run before aliases have been
1688      computed.  By not bothering with virtual operands for CALL_EXPRs
1689      we avoid adding superfluous virtual operands, which can be a
1690      significant compile time sink (See PR 15855).  */
1691   if (aliases_computed_p
1692       && !bitmap_empty_p (call_clobbered_vars)
1693       && !(call_flags & ECF_NOVOPS))
1694     {
1695       /* A 'pure' or a 'const' function never call-clobbers anything. 
1696          A 'noreturn' function might, but since we don't return anyway 
1697          there is no point in recording that.  */ 
1698       if (TREE_SIDE_EFFECTS (expr)
1699           && !(call_flags & (ECF_PURE | ECF_CONST | ECF_NORETURN)))
1700         add_call_clobber_ops (stmt, get_callee_fndecl (expr));
1701       else if (!(call_flags & ECF_CONST))
1702         add_call_read_ops (stmt, get_callee_fndecl (expr));
1703     }
1704
1705   /* Find uses in the called function.  */
1706   get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_none);
1707
1708   for (op = TREE_OPERAND (expr, 1); op; op = TREE_CHAIN (op))
1709     get_expr_operands (stmt, &TREE_VALUE (op), opf_none);
1710
1711   get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1712 }
1713
1714
1715 /* Scan operands in the ASM_EXPR stmt referred to in INFO.  */
1716
1717 static void
1718 get_asm_expr_operands (tree stmt)
1719 {
1720   stmt_ann_t s_ann = stmt_ann (stmt);
1721   int noutputs = list_length (ASM_OUTPUTS (stmt));
1722   const char **oconstraints
1723     = (const char **) alloca ((noutputs) * sizeof (const char *));
1724   int i;
1725   tree link;
1726   const char *constraint;
1727   bool allows_mem, allows_reg, is_inout;
1728
1729   for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
1730     {
1731       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1732       oconstraints[i] = constraint;
1733       parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
1734                                &allows_reg, &is_inout);
1735
1736       /* This should have been split in gimplify_asm_expr.  */
1737       gcc_assert (!allows_reg || !is_inout);
1738
1739       /* Memory operands are addressable.  Note that STMT needs the
1740          address of this operand.  */
1741       if (!allows_reg && allows_mem)
1742         {
1743           tree t = get_base_address (TREE_VALUE (link));
1744           if (t && DECL_P (t) && s_ann)
1745             add_to_addressable_set (t, &s_ann->addresses_taken);
1746         }
1747
1748       get_expr_operands (stmt, &TREE_VALUE (link), opf_is_def);
1749     }
1750
1751   for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
1752     {
1753       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1754       parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1755                               oconstraints, &allows_mem, &allows_reg);
1756
1757       /* Memory operands are addressable.  Note that STMT needs the
1758          address of this operand.  */
1759       if (!allows_reg && allows_mem)
1760         {
1761           tree t = get_base_address (TREE_VALUE (link));
1762           if (t && DECL_P (t) && s_ann)
1763             add_to_addressable_set (t, &s_ann->addresses_taken);
1764         }
1765
1766       get_expr_operands (stmt, &TREE_VALUE (link), 0);
1767     }
1768
1769
1770   /* Clobber memory for asm ("" : : : "memory");  */
1771   for (link = ASM_CLOBBERS (stmt); link; link = TREE_CHAIN (link))
1772     if (strcmp (TREE_STRING_POINTER (TREE_VALUE (link)), "memory") == 0)
1773       {
1774         unsigned i;
1775         bitmap_iterator bi;
1776
1777         /* Clobber all call-clobbered variables (or .GLOBAL_VAR if we
1778            decided to group them).  */
1779         if (global_var)
1780           add_stmt_operand (&global_var, s_ann, opf_is_def);
1781         else
1782           EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1783             {
1784               tree var = referenced_var (i);
1785               add_stmt_operand (&var, s_ann, opf_is_def | opf_non_specific);
1786             }
1787
1788         /* Now clobber all addressables.  */
1789         EXECUTE_IF_SET_IN_BITMAP (addressable_vars, 0, i, bi)
1790             {
1791               tree var = referenced_var (i);
1792
1793               /* Subvars are explicitly represented in this list, so
1794                  we don't need the original to be added to the clobber
1795                  ops, but the original *will* be in this list because 
1796                  we keep the addressability of the original
1797                  variable up-to-date so we don't screw up the rest of
1798                  the backend.  */
1799               if (var_can_have_subvars (var)
1800                   && get_subvars_for_var (var) != NULL)
1801                 continue;               
1802
1803               add_stmt_operand (&var, s_ann, opf_is_def | opf_non_specific);
1804             }
1805
1806         break;
1807       }
1808 }
1809
1810
1811 /* Scan operands for the assignment expression EXPR in statement STMT.  */
1812
1813 static void
1814 get_modify_expr_operands (tree stmt, tree expr)
1815 {
1816   /* First get operands from the RHS.  */
1817   get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1818
1819   /* For the LHS, use a regular definition (OPF_IS_DEF) for GIMPLE
1820      registers.  If the LHS is a store to memory, we will either need
1821      a preserving definition (V_MAY_DEF) or a killing definition
1822      (V_MUST_DEF).
1823
1824      Preserving definitions are those that modify a part of an
1825      aggregate object for which no subvars have been computed (or the
1826      reference does not correspond exactly to one of them). Stores
1827      through a pointer are also represented with V_MAY_DEF operators.
1828
1829      The determination of whether to use a preserving or a killing
1830      definition is done while scanning the LHS of the assignment.  By
1831      default, assume that we will emit a V_MUST_DEF.  */
1832   get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_is_def|opf_kill_def);
1833 }
1834
1835
1836 /* Recursively scan the expression pointed to by EXPR_P in statement
1837    STMT.  FLAGS is one of the OPF_* constants modifying how to
1838    interpret the operands found.  */
1839
1840 static void
1841 get_expr_operands (tree stmt, tree *expr_p, int flags)
1842 {
1843   enum tree_code code;
1844   enum tree_code_class class;
1845   tree expr = *expr_p;
1846   stmt_ann_t s_ann = stmt_ann (stmt);
1847
1848   if (expr == NULL)
1849     return;
1850
1851   code = TREE_CODE (expr);
1852   class = TREE_CODE_CLASS (code);
1853
1854   switch (code)
1855     {
1856     case ADDR_EXPR:
1857       /* Taking the address of a variable does not represent a
1858          reference to it, but the fact that the statement takes its
1859          address will be of interest to some passes (e.g. alias
1860          resolution).  */
1861       add_to_addressable_set (TREE_OPERAND (expr, 0), &s_ann->addresses_taken);
1862
1863       /* If the address is invariant, there may be no interesting
1864          variable references inside.  */
1865       if (is_gimple_min_invariant (expr))
1866         return;
1867
1868       /* Otherwise, there may be variables referenced inside but there
1869          should be no VUSEs created, since the referenced objects are
1870          not really accessed.  The only operands that we should find
1871          here are ARRAY_REF indices which will always be real operands
1872          (GIMPLE does not allow non-registers as array indices).  */
1873       flags |= opf_no_vops;
1874       get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1875       return;
1876
1877     case SSA_NAME:
1878     case STRUCT_FIELD_TAG:
1879     case SYMBOL_MEMORY_TAG:
1880     case NAME_MEMORY_TAG:
1881      add_stmt_operand (expr_p, s_ann, flags);
1882      return;
1883
1884     case VAR_DECL:
1885     case PARM_DECL:
1886     case RESULT_DECL:
1887       {
1888         subvar_t svars;
1889         
1890         /* Add the subvars for a variable, if it has subvars, to DEFS
1891            or USES.  Otherwise, add the variable itself.  Whether it
1892            goes to USES or DEFS depends on the operand flags.  */
1893         if (var_can_have_subvars (expr)
1894             && (svars = get_subvars_for_var (expr)))
1895           {
1896             subvar_t sv;
1897             for (sv = svars; sv; sv = sv->next)
1898               add_stmt_operand (&sv->var, s_ann, flags);
1899           }
1900         else
1901           add_stmt_operand (expr_p, s_ann, flags);
1902
1903         return;
1904       }
1905
1906     case MISALIGNED_INDIRECT_REF:
1907       get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1908       /* fall through */
1909
1910     case ALIGN_INDIRECT_REF:
1911     case INDIRECT_REF:
1912       get_indirect_ref_operands (stmt, expr, flags, NULL_TREE, 0, -1, true);
1913       return;
1914
1915     case TARGET_MEM_REF:
1916       get_tmr_operands (stmt, expr, flags);
1917       return;
1918
1919     case ARRAY_REF:
1920     case ARRAY_RANGE_REF:
1921     case COMPONENT_REF:
1922     case REALPART_EXPR:
1923     case IMAGPART_EXPR:
1924       {
1925         tree ref;
1926         HOST_WIDE_INT offset, size, maxsize;
1927         bool none = true;
1928
1929         /* This component reference becomes an access to all of the
1930            subvariables it can touch, if we can determine that, but
1931            *NOT* the real one.  If we can't determine which fields we
1932            could touch, the recursion will eventually get to a
1933            variable and add *all* of its subvars, or whatever is the
1934            minimum correct subset.  */
1935         ref = get_ref_base_and_extent (expr, &offset, &size, &maxsize);
1936         if (SSA_VAR_P (ref) && get_subvars_for_var (ref))
1937           {
1938             subvar_t sv;
1939             subvar_t svars = get_subvars_for_var (ref);
1940
1941             for (sv = svars; sv; sv = sv->next)
1942               {
1943                 bool exact;             
1944
1945                 if (overlap_subvar (offset, maxsize, sv->var, &exact))
1946                   {
1947                     int subvar_flags = flags;
1948                     none = false;
1949                     if (!exact || size != maxsize)
1950                       subvar_flags &= ~opf_kill_def;
1951                     add_stmt_operand (&sv->var, s_ann, subvar_flags);
1952                   }
1953               }
1954
1955             if (!none)
1956               flags |= opf_no_vops;
1957           }
1958         else if (TREE_CODE (ref) == INDIRECT_REF)
1959           {
1960             get_indirect_ref_operands (stmt, ref, flags, expr, offset,
1961                                        maxsize, false);
1962             flags |= opf_no_vops;
1963           }
1964
1965         /* Even if we found subvars above we need to ensure to see
1966            immediate uses for d in s.a[d].  In case of s.a having
1967            a subvar or we would miss it otherwise.  */
1968         get_expr_operands (stmt, &TREE_OPERAND (expr, 0),
1969                            flags & ~opf_kill_def);
1970         
1971         if (code == COMPONENT_REF)
1972           {
1973             if (s_ann && TREE_THIS_VOLATILE (TREE_OPERAND (expr, 1)))
1974               s_ann->has_volatile_ops = true; 
1975             get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1976           }
1977         else if (code == ARRAY_REF || code == ARRAY_RANGE_REF)
1978           {
1979             get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1980             get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1981             get_expr_operands (stmt, &TREE_OPERAND (expr, 3), opf_none);
1982           }
1983
1984         return;
1985       }
1986
1987     case WITH_SIZE_EXPR:
1988       /* WITH_SIZE_EXPR is a pass-through reference to its first argument,
1989          and an rvalue reference to its second argument.  */
1990       get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1991       get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1992       return;
1993
1994     case CALL_EXPR:
1995       get_call_expr_operands (stmt, expr);
1996       return;
1997
1998     case COND_EXPR:
1999     case VEC_COND_EXPR:
2000       get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_none);
2001       get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
2002       get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
2003       return;
2004
2005     case MODIFY_EXPR:
2006       get_modify_expr_operands (stmt, expr);
2007       return;
2008
2009     case CONSTRUCTOR:
2010       {
2011         /* General aggregate CONSTRUCTORs have been decomposed, but they
2012            are still in use as the COMPLEX_EXPR equivalent for vectors.  */
2013         constructor_elt *ce;
2014         unsigned HOST_WIDE_INT idx;
2015
2016         for (idx = 0;
2017              VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (expr), idx, ce);
2018              idx++)
2019           get_expr_operands (stmt, &ce->value, opf_none);
2020
2021         return;
2022       }
2023
2024     case BIT_FIELD_REF:
2025       /* Stores using BIT_FIELD_REF are always preserving definitions.  */
2026       flags &= ~opf_kill_def;
2027
2028       /* Fallthru  */
2029
2030     case TRUTH_NOT_EXPR:
2031     case VIEW_CONVERT_EXPR:
2032     do_unary:
2033       get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
2034       return;
2035
2036     case TRUTH_AND_EXPR:
2037     case TRUTH_OR_EXPR:
2038     case TRUTH_XOR_EXPR:
2039     case COMPOUND_EXPR:
2040     case OBJ_TYPE_REF:
2041     case ASSERT_EXPR:
2042     do_binary:
2043       {
2044         get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
2045         get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
2046         return;
2047       }
2048
2049     case DOT_PROD_EXPR:
2050     case REALIGN_LOAD_EXPR:
2051       {
2052         get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
2053         get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
2054         get_expr_operands (stmt, &TREE_OPERAND (expr, 2), flags);
2055         return;
2056       }
2057
2058     case BLOCK:
2059     case FUNCTION_DECL:
2060     case EXC_PTR_EXPR:
2061     case FILTER_EXPR:
2062     case LABEL_DECL:
2063     case CONST_DECL:
2064     case OMP_PARALLEL:
2065     case OMP_SECTIONS:
2066     case OMP_FOR:
2067     case OMP_SINGLE:
2068     case OMP_MASTER:
2069     case OMP_ORDERED:
2070     case OMP_CRITICAL:
2071     case OMP_RETURN:
2072     case OMP_CONTINUE:
2073       /* Expressions that make no memory references.  */
2074       return;
2075
2076     default:
2077       if (class == tcc_unary)
2078         goto do_unary;
2079       if (class == tcc_binary || class == tcc_comparison)
2080         goto do_binary;
2081       if (class == tcc_constant || class == tcc_type)
2082         return;
2083     }
2084
2085   /* If we get here, something has gone wrong.  */
2086 #ifdef ENABLE_CHECKING
2087   fprintf (stderr, "unhandled expression in get_expr_operands():\n");
2088   debug_tree (expr);
2089   fputs ("\n", stderr);
2090 #endif
2091   gcc_unreachable ();
2092 }
2093
2094
2095 /* Parse STMT looking for operands.  When finished, the various
2096    build_* operand vectors will have potential operands in them.  */
2097
2098 static void
2099 parse_ssa_operands (tree stmt)
2100 {
2101   enum tree_code code;
2102
2103   code = TREE_CODE (stmt);
2104   switch (code)
2105     {
2106     case MODIFY_EXPR:
2107       get_modify_expr_operands (stmt, stmt);
2108       break;
2109
2110     case COND_EXPR:
2111       get_expr_operands (stmt, &COND_EXPR_COND (stmt), opf_none);
2112       break;
2113
2114     case SWITCH_EXPR:
2115       get_expr_operands (stmt, &SWITCH_COND (stmt), opf_none);
2116       break;
2117
2118     case ASM_EXPR:
2119       get_asm_expr_operands (stmt);
2120       break;
2121
2122     case RETURN_EXPR:
2123       get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), opf_none);
2124       break;
2125
2126     case GOTO_EXPR:
2127       get_expr_operands (stmt, &GOTO_DESTINATION (stmt), opf_none);
2128       break;
2129
2130     case LABEL_EXPR:
2131       get_expr_operands (stmt, &LABEL_EXPR_LABEL (stmt), opf_none);
2132       break;
2133
2134     case BIND_EXPR:
2135     case CASE_LABEL_EXPR:
2136     case TRY_CATCH_EXPR:
2137     case TRY_FINALLY_EXPR:
2138     case EH_FILTER_EXPR:
2139     case CATCH_EXPR:
2140     case RESX_EXPR:
2141       /* These nodes contain no variable references.  */
2142       break;
2143
2144     default:
2145       /* Notice that if get_expr_operands tries to use &STMT as the
2146          operand pointer (which may only happen for USE operands), we
2147          will fail in add_stmt_operand.  This default will handle
2148          statements like empty statements, or CALL_EXPRs that may
2149          appear on the RHS of a statement or as statements themselves.  */
2150       get_expr_operands (stmt, &stmt, opf_none);
2151       break;
2152     }
2153 }
2154
2155
2156 /* Create an operands cache for STMT.  */
2157
2158 static void
2159 build_ssa_operands (tree stmt)
2160 {
2161   stmt_ann_t ann = get_stmt_ann (stmt);
2162   
2163   /* Initially assume that the statement has no volatile operands.  */
2164   if (ann)
2165     ann->has_volatile_ops = false;
2166
2167   start_ssa_stmt_operands ();
2168
2169   parse_ssa_operands (stmt);
2170   operand_build_sort_virtual (build_vuses);
2171   operand_build_sort_virtual (build_v_may_defs);
2172   operand_build_sort_virtual (build_v_must_defs);
2173
2174   finalize_ssa_stmt_operands (stmt);
2175 }
2176
2177
2178 /* Free any operands vectors in OPS.  */
2179
2180 void 
2181 free_ssa_operands (stmt_operands_p ops)
2182 {
2183   ops->def_ops = NULL;
2184   ops->use_ops = NULL;
2185   ops->maydef_ops = NULL;
2186   ops->mustdef_ops = NULL;
2187   ops->vuse_ops = NULL;
2188 }
2189
2190
2191 /* Get the operands of statement STMT.  */
2192
2193 void
2194 update_stmt_operands (tree stmt)
2195 {
2196   stmt_ann_t ann = get_stmt_ann (stmt);
2197
2198   /* If update_stmt_operands is called before SSA is initialized, do
2199      nothing.  */
2200   if (!ssa_operands_active ())
2201     return;
2202
2203   /* The optimizers cannot handle statements that are nothing but a
2204      _DECL.  This indicates a bug in the gimplifier.  */
2205   gcc_assert (!SSA_VAR_P (stmt));
2206
2207   gcc_assert (ann->modified);
2208
2209   timevar_push (TV_TREE_OPS);
2210
2211   build_ssa_operands (stmt);
2212
2213   /* Clear the modified bit for STMT.  */
2214   ann->modified = 0;
2215
2216   timevar_pop (TV_TREE_OPS);
2217 }
2218
2219
2220 /* Copies virtual operands from SRC to DST.  */
2221
2222 void
2223 copy_virtual_operands (tree dest, tree src)
2224 {
2225   tree t;
2226   ssa_op_iter iter, old_iter;
2227   use_operand_p use_p, u2;
2228   def_operand_p def_p, d2;
2229
2230   build_ssa_operands (dest);
2231
2232   /* Copy all the virtual fields.  */
2233   FOR_EACH_SSA_TREE_OPERAND (t, src, iter, SSA_OP_VUSE)
2234     append_vuse (t);
2235   FOR_EACH_SSA_TREE_OPERAND (t, src, iter, SSA_OP_VMAYDEF)
2236     append_v_may_def (t);
2237   FOR_EACH_SSA_TREE_OPERAND (t, src, iter, SSA_OP_VMUSTDEF)
2238     append_v_must_def (t);
2239
2240   if (VEC_length (tree, build_vuses) == 0
2241       && VEC_length (tree, build_v_may_defs) == 0
2242       && VEC_length (tree, build_v_must_defs) == 0)
2243     return;
2244
2245   /* Now commit the virtual operands to this stmt.  */
2246   finalize_ssa_v_must_defs (dest);
2247   finalize_ssa_v_may_defs (dest);
2248   finalize_ssa_vuses (dest);
2249
2250   /* Finally, set the field to the same values as then originals.  */
2251   t = op_iter_init_tree (&old_iter, src, SSA_OP_VUSE);
2252   FOR_EACH_SSA_USE_OPERAND (use_p, dest, iter, SSA_OP_VUSE)
2253     {
2254       gcc_assert (!op_iter_done (&old_iter));
2255       SET_USE (use_p, t);
2256       t = op_iter_next_tree (&old_iter);
2257     }
2258   gcc_assert (op_iter_done (&old_iter));
2259
2260   op_iter_init_maydef (&old_iter, src, &u2, &d2);
2261   FOR_EACH_SSA_MAYDEF_OPERAND (def_p, use_p, dest, iter)
2262     {
2263       gcc_assert (!op_iter_done (&old_iter));
2264       SET_USE (use_p, USE_FROM_PTR (u2));
2265       SET_DEF (def_p, DEF_FROM_PTR (d2));
2266       op_iter_next_maymustdef (&u2, &d2, &old_iter);
2267     }
2268   gcc_assert (op_iter_done (&old_iter));
2269
2270   op_iter_init_mustdef (&old_iter, src, &u2, &d2);
2271   FOR_EACH_SSA_MUSTDEF_OPERAND (def_p, use_p, dest, iter)
2272     {
2273       gcc_assert (!op_iter_done (&old_iter));
2274       SET_USE (use_p, USE_FROM_PTR (u2));
2275       SET_DEF (def_p, DEF_FROM_PTR (d2));
2276       op_iter_next_maymustdef (&u2, &d2, &old_iter);
2277     }
2278   gcc_assert (op_iter_done (&old_iter));
2279
2280 }
2281
2282
2283 /* Specifically for use in DOM's expression analysis.  Given a store, we
2284    create an artificial stmt which looks like a load from the store, this can
2285    be used to eliminate redundant loads.  OLD_OPS are the operands from the 
2286    store stmt, and NEW_STMT is the new load which represents a load of the
2287    values stored.  */
2288
2289 void
2290 create_ssa_artficial_load_stmt (tree new_stmt, tree old_stmt)
2291 {
2292   stmt_ann_t ann;
2293   tree op;
2294   ssa_op_iter iter;
2295   use_operand_p use_p;
2296   unsigned x;
2297
2298   ann = get_stmt_ann (new_stmt);
2299
2300   /* Process the stmt looking for operands.  */
2301   start_ssa_stmt_operands ();
2302   parse_ssa_operands (new_stmt);
2303
2304   for (x = 0; x < VEC_length (tree, build_vuses); x++)
2305     {
2306       tree t = VEC_index (tree, build_vuses, x);
2307       if (TREE_CODE (t) != SSA_NAME)
2308         {
2309           var_ann_t ann = var_ann (t);
2310           ann->in_vuse_list = 0;
2311         }
2312     }
2313    
2314   for (x = 0; x < VEC_length (tree, build_v_may_defs); x++)
2315     {
2316       tree t = VEC_index (tree, build_v_may_defs, x);
2317       if (TREE_CODE (t) != SSA_NAME)
2318         {
2319           var_ann_t ann = var_ann (t);
2320           ann->in_v_may_def_list = 0;
2321         }
2322     }
2323
2324   /* Remove any virtual operands that were found.  */
2325   VEC_truncate (tree, build_v_may_defs, 0);
2326   VEC_truncate (tree, build_v_must_defs, 0);
2327   VEC_truncate (tree, build_vuses, 0);
2328
2329   /* For each VDEF on the original statement, we want to create a
2330      VUSE of the V_MAY_DEF result or V_MUST_DEF op on the new 
2331      statement.  */
2332   FOR_EACH_SSA_TREE_OPERAND (op, old_stmt, iter, 
2333                              (SSA_OP_VMAYDEF | SSA_OP_VMUSTDEF))
2334     append_vuse (op);
2335     
2336   /* Now build the operands for this new stmt.  */
2337   finalize_ssa_stmt_operands (new_stmt);
2338
2339   /* All uses in this fake stmt must not be in the immediate use lists.  */
2340   FOR_EACH_SSA_USE_OPERAND (use_p, new_stmt, iter, SSA_OP_ALL_USES)
2341     delink_imm_use (use_p);
2342 }
2343
2344
2345 /* Swap operands EXP0 and EXP1 in statement STMT.  No attempt is done
2346    to test the validity of the swap operation.  */
2347
2348 void
2349 swap_tree_operands (tree stmt, tree *exp0, tree *exp1)
2350 {
2351   tree op0, op1;
2352   op0 = *exp0;
2353   op1 = *exp1;
2354
2355   /* If the operand cache is active, attempt to preserve the relative
2356      positions of these two operands in their respective immediate use
2357      lists.  */
2358   if (ssa_operands_active () && op0 != op1)
2359     {
2360       use_optype_p use0, use1, ptr;
2361       use0 = use1 = NULL;
2362
2363       /* Find the 2 operands in the cache, if they are there.  */
2364       for (ptr = USE_OPS (stmt); ptr; ptr = ptr->next)
2365         if (USE_OP_PTR (ptr)->use == exp0)
2366           {
2367             use0 = ptr;
2368             break;
2369           }
2370
2371       for (ptr = USE_OPS (stmt); ptr; ptr = ptr->next)
2372         if (USE_OP_PTR (ptr)->use == exp1)
2373           {
2374             use1 = ptr;
2375             break;
2376           }
2377
2378       /* If both uses don't have operand entries, there isn't much we can do
2379          at this point.  Presumably we don't need to worry about it.  */
2380       if (use0 && use1)
2381         {
2382           tree *tmp = USE_OP_PTR (use1)->use;
2383           USE_OP_PTR (use1)->use = USE_OP_PTR (use0)->use;
2384           USE_OP_PTR (use0)->use = tmp;
2385         }
2386     }
2387
2388   /* Now swap the data.  */
2389   *exp0 = op1;
2390   *exp1 = op0;
2391 }
2392
2393
2394 /* Add the base address of REF to the set *ADDRESSES_TAKEN.  If
2395    *ADDRESSES_TAKEN is NULL, a new set is created.  REF may be
2396    a single variable whose address has been taken or any other valid
2397    GIMPLE memory reference (structure reference, array, etc).  If the
2398    base address of REF is a decl that has sub-variables, also add all
2399    of its sub-variables.  */
2400
2401 void
2402 add_to_addressable_set (tree ref, bitmap *addresses_taken)
2403 {
2404   tree var;
2405   subvar_t svars;
2406
2407   gcc_assert (addresses_taken);
2408
2409   /* Note that it is *NOT OKAY* to use the target of a COMPONENT_REF
2410      as the only thing we take the address of.  If VAR is a structure,
2411      taking the address of a field means that the whole structure may
2412      be referenced using pointer arithmetic.  See PR 21407 and the
2413      ensuing mailing list discussion.  */
2414   var = get_base_address (ref);
2415   if (var && SSA_VAR_P (var))
2416     {
2417       if (*addresses_taken == NULL)
2418         *addresses_taken = BITMAP_GGC_ALLOC ();      
2419       
2420       if (var_can_have_subvars (var)
2421           && (svars = get_subvars_for_var (var)))
2422         {
2423           subvar_t sv;
2424           for (sv = svars; sv; sv = sv->next)
2425             {
2426               bitmap_set_bit (*addresses_taken, DECL_UID (sv->var));
2427               TREE_ADDRESSABLE (sv->var) = 1;
2428             }
2429         }
2430       else
2431         {
2432           bitmap_set_bit (*addresses_taken, DECL_UID (var));
2433           TREE_ADDRESSABLE (var) = 1;
2434         }
2435     }
2436 }
2437
2438
2439 /* Scan the immediate_use list for VAR making sure its linked properly.
2440    Return TRUE if there is a problem and emit an error message to F.  */
2441
2442 bool
2443 verify_imm_links (FILE *f, tree var)
2444 {
2445   use_operand_p ptr, prev, list;
2446   int count;
2447
2448   gcc_assert (TREE_CODE (var) == SSA_NAME);
2449
2450   list = &(SSA_NAME_IMM_USE_NODE (var));
2451   gcc_assert (list->use == NULL);
2452
2453   if (list->prev == NULL)
2454     {
2455       gcc_assert (list->next == NULL);
2456       return false;
2457     }
2458
2459   prev = list;
2460   count = 0;
2461   for (ptr = list->next; ptr != list; )
2462     {
2463       if (prev != ptr->prev)
2464         goto error;
2465       
2466       if (ptr->use == NULL)
2467         goto error; /* 2 roots, or SAFE guard node.  */
2468       else if (*(ptr->use) != var)
2469         goto error;
2470
2471       prev = ptr;
2472       ptr = ptr->next;
2473
2474       /* Avoid infinite loops.  50,000,000 uses probably indicates a
2475          problem.  */
2476       if (count++ > 50000000)
2477         goto error;
2478     }
2479
2480   /* Verify list in the other direction.  */
2481   prev = list;
2482   for (ptr = list->prev; ptr != list; )
2483     {
2484       if (prev != ptr->next)
2485         goto error;
2486       prev = ptr;
2487       ptr = ptr->prev;
2488       if (count-- < 0)
2489         goto error;
2490     }
2491
2492   if (count != 0)
2493     goto error;
2494
2495   return false;
2496
2497  error:
2498   if (ptr->stmt && stmt_modified_p (ptr->stmt))
2499     {
2500       fprintf (f, " STMT MODIFIED. - <%p> ", (void *)ptr->stmt);
2501       print_generic_stmt (f, ptr->stmt, TDF_SLIM);
2502     }
2503   fprintf (f, " IMM ERROR : (use_p : tree - %p:%p)", (void *)ptr, 
2504            (void *)ptr->use);
2505   print_generic_expr (f, USE_FROM_PTR (ptr), TDF_SLIM);
2506   fprintf(f, "\n");
2507   return true;
2508 }
2509
2510
2511 /* Dump all the immediate uses to FILE.  */
2512
2513 void
2514 dump_immediate_uses_for (FILE *file, tree var)
2515 {
2516   imm_use_iterator iter;
2517   use_operand_p use_p;
2518
2519   gcc_assert (var && TREE_CODE (var) == SSA_NAME);
2520
2521   print_generic_expr (file, var, TDF_SLIM);
2522   fprintf (file, " : -->");
2523   if (has_zero_uses (var))
2524     fprintf (file, " no uses.\n");
2525   else
2526     if (has_single_use (var))
2527       fprintf (file, " single use.\n");
2528     else
2529       fprintf (file, "%d uses.\n", num_imm_uses (var));
2530
2531   FOR_EACH_IMM_USE_FAST (use_p, iter, var)
2532     {
2533       if (use_p->stmt == NULL && use_p->use == NULL)
2534         fprintf (file, "***end of stmt iterator marker***\n");
2535       else
2536         if (!is_gimple_reg (USE_FROM_PTR (use_p)))
2537           print_generic_stmt (file, USE_STMT (use_p), TDF_VOPS);
2538         else
2539           print_generic_stmt (file, USE_STMT (use_p), TDF_SLIM);
2540     }
2541   fprintf(file, "\n");
2542 }
2543
2544
2545 /* Dump all the immediate uses to FILE.  */
2546
2547 void
2548 dump_immediate_uses (FILE *file)
2549 {
2550   tree var;
2551   unsigned int x;
2552
2553   fprintf (file, "Immediate_uses: \n\n");
2554   for (x = 1; x < num_ssa_names; x++)
2555     {
2556       var = ssa_name(x);
2557       if (!var)
2558         continue;
2559       dump_immediate_uses_for (file, var);
2560     }
2561 }
2562
2563
2564 /* Dump def-use edges on stderr.  */
2565
2566 void
2567 debug_immediate_uses (void)
2568 {
2569   dump_immediate_uses (stderr);
2570 }
2571
2572
2573 /* Dump def-use edges on stderr.  */
2574
2575 void
2576 debug_immediate_uses_for (tree var)
2577 {
2578   dump_immediate_uses_for (stderr, var);
2579 }
2580
2581 #include "gt-tree-ssa-operands.h"