OSDN Git Service

de5150e60099db5347374caae51588e5d4ab50d5
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-operands.c
1 /* SSA operands management for trees.
2    Copyright (C) 2003, 2004, 2005 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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, 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 "errors.h"
30 #include "tree-flow.h"
31 #include "tree-inline.h"
32 #include "tree-pass.h"
33 #include "ggc.h"
34 #include "timevar.h"
35
36 #include "langhooks.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    get_stmt_operands() in the primary entry point. 
54
55    The operand tree is the parsed by the various get_* routines which look 
56    through the stmt tree for the occurrence of operands which may be of 
57    interest, and calls are made to the append_* routines whenever one is 
58    found.  There are 5 of these routines, each representing one of the 
59    5 types of operands. Defs, Uses, Virtual Uses, Virtual May Defs, and 
60    Virtual Must Defs.
61
62    The append_* routines check for duplication, and simply keep a list of 
63    unique objects for each operand type in the build_* extendable vectors.
64
65    Once the stmt tree is completely parsed, the finalize_ssa_operands() 
66    routine is called, which proceeds to perform the finalization routine 
67    on each of the 5 operand vectors which have been built up.
68
69    If the stmt had a previous operand cache, the finalization routines 
70    attempt to match up the new operands with the old ones.  If its a perfect 
71    match, the old vector is simply reused.  If it isn't a perfect match, then 
72    a new vector is created and the new operands are placed there.  For 
73    virtual operands, if the previous cache had SSA_NAME version of a 
74    variable, and that same variable occurs in the same operands cache, then 
75    the new cache vector will also get the same SSA_NAME.
76
77   i.e., if a stmt had a VUSE of 'a_5', and 'a' occurs in the new operand 
78   vector for VUSE, then the new vector will also be modified such that 
79   it contains 'a_5' rather than 'a'.
80
81 */
82
83
84 /* Flags to describe operand properties in get_stmt_operands and helpers.  */
85
86 /* By default, operands are loaded.  */
87 #define opf_none        0
88
89 /* Operand is the target of an assignment expression or a 
90    call-clobbered variable  */
91 #define opf_is_def      (1 << 0)
92
93 /* Operand is the target of an assignment expression.  */
94 #define opf_kill_def    (1 << 1)
95
96 /* No virtual operands should be created in the expression.  This is used
97    when traversing ADDR_EXPR nodes which have different semantics than
98    other expressions.  Inside an ADDR_EXPR node, the only operands that we
99    need to consider are indices into arrays.  For instance, &a.b[i] should
100    generate a USE of 'i' but it should not generate a VUSE for 'a' nor a
101    VUSE for 'b'.  */
102 #define opf_no_vops     (1 << 2)
103
104 /* Array for building all the def operands.  */
105 static GTY (()) varray_type build_defs;
106
107 /* Array for building all the use operands.  */
108 static GTY (()) varray_type build_uses;
109
110 /* Array for building all the v_may_def operands.  */
111 static GTY (()) varray_type build_v_may_defs;
112
113 /* Array for building all the vuse operands.  */
114 static GTY (()) varray_type build_vuses;
115
116 /* Array for building all the v_must_def operands.  */
117 static GTY (()) varray_type build_v_must_defs;
118
119 /* True if the operands for call clobbered vars are cached and valid.  */
120 bool ssa_call_clobbered_cache_valid;
121 bool ssa_ro_call_cache_valid;
122
123 /* These arrays are the cached operand vectors for call clobbered calls.  */
124 static GTY (()) varray_type clobbered_v_may_defs;
125 static GTY (()) varray_type clobbered_vuses;
126 static GTY (()) varray_type ro_call_vuses;
127 static bool clobbered_aliased_loads;
128 static bool clobbered_aliased_stores;
129 static bool ro_call_aliased_loads;
130
131 def_operand_p NULL_DEF_OPERAND_P = { NULL };
132 use_operand_p NULL_USE_OPERAND_P = { NULL };
133
134 static void note_addressable (tree, stmt_ann_t);
135 static void get_expr_operands (tree, tree *, int);
136 static void get_asm_expr_operands (tree);
137 static void get_indirect_ref_operands (tree, tree, int);
138 static void get_call_expr_operands (tree, tree);
139 static inline void append_def (tree *);
140 static inline void append_use (tree *);
141 static void append_v_may_def (tree);
142 static void append_v_must_def (tree);
143 static void add_call_clobber_ops (tree);
144 static void add_call_read_ops (tree);
145 static void add_stmt_operand (tree *, stmt_ann_t, int);
146
147 /* Return a vector of contiguous memory for NUM def operands.  */
148
149 static inline def_optype
150 allocate_def_optype (unsigned num)
151 {
152   def_optype def_ops;
153   unsigned size;
154   size = sizeof (struct def_optype_d) + sizeof (tree *) * (num - 1);
155   def_ops =  ggc_alloc (size);
156   def_ops->num_defs = num;
157   return def_ops;
158 }
159
160
161 /* Return a vector of contiguous memory for NUM use operands.  */
162
163 static inline use_optype
164 allocate_use_optype (unsigned num)
165 {
166   use_optype use_ops;
167   unsigned size;
168   size = sizeof (struct use_optype_d) + sizeof (tree *) * (num - 1);
169   use_ops =  ggc_alloc (size);
170   use_ops->num_uses = num;
171   return use_ops;
172 }
173
174
175 /* Return a vector of contiguous memory for NUM v_may_def operands.  */
176
177 static inline v_may_def_optype
178 allocate_v_may_def_optype (unsigned num)
179 {
180   v_may_def_optype v_may_def_ops;
181   unsigned size;
182   size = sizeof (struct v_may_def_optype_d) 
183            + sizeof (v_def_use_operand_type_t) * (num - 1);
184   v_may_def_ops =  ggc_alloc (size);
185   v_may_def_ops->num_v_may_defs = num;
186   return v_may_def_ops;
187 }
188
189
190 /* Return a vector of contiguous memory for NUM v_use operands.  */
191
192 static inline vuse_optype
193 allocate_vuse_optype (unsigned num)
194 {
195   vuse_optype vuse_ops;
196   unsigned size;
197   size = sizeof (struct vuse_optype_d) + sizeof (tree) * (num - 1);
198   vuse_ops =  ggc_alloc (size);
199   vuse_ops->num_vuses = num;
200   return vuse_ops;
201 }
202
203
204 /* Return a vector of contiguous memory for NUM v_must_def operands.  */
205
206 static inline v_must_def_optype
207 allocate_v_must_def_optype (unsigned num)
208 {
209   v_must_def_optype v_must_def_ops;
210   unsigned size;
211   size = sizeof (struct v_must_def_optype_d) + sizeof (v_def_use_operand_type_t) * (num - 1);
212   v_must_def_ops =  ggc_alloc (size);
213   v_must_def_ops->num_v_must_defs = num;
214   return v_must_def_ops;
215 }
216
217
218 /* Free memory for USES.  */
219
220 static inline void
221 free_uses (use_optype *uses)
222 {
223   if (*uses)
224     {
225       ggc_free (*uses);
226       *uses = NULL;
227     }
228 }
229
230
231 /* Free memory for DEFS.  */
232
233 static inline void
234 free_defs (def_optype *defs)
235 {
236   if (*defs)
237     {
238       ggc_free (*defs);
239       *defs = NULL;
240     }
241 }
242
243
244 /* Free memory for VUSES.  */
245
246 static inline void
247 free_vuses (vuse_optype *vuses)
248 {
249   if (*vuses)
250     {
251       ggc_free (*vuses);
252       *vuses = NULL;
253     }
254 }
255
256
257 /* Free memory for V_MAY_DEFS.  */
258
259 static inline void
260 free_v_may_defs (v_may_def_optype *v_may_defs)
261 {
262   if (*v_may_defs)
263     {
264       ggc_free (*v_may_defs);
265       *v_may_defs = NULL;
266     }
267 }
268
269
270 /* Free memory for V_MUST_DEFS.  */
271
272 static inline void
273 free_v_must_defs (v_must_def_optype *v_must_defs)
274 {
275   if (*v_must_defs)
276     {
277       ggc_free (*v_must_defs);
278       *v_must_defs = NULL;
279     }
280 }
281
282
283 /* Initialize the operand cache routines.  */
284
285 void
286 init_ssa_operands (void)
287 {
288   VARRAY_TREE_PTR_INIT (build_defs, 5, "build defs");
289   VARRAY_TREE_PTR_INIT (build_uses, 10, "build uses");
290   VARRAY_TREE_INIT (build_v_may_defs, 10, "build v_may_defs");
291   VARRAY_TREE_INIT (build_vuses, 10, "build vuses");
292   VARRAY_TREE_INIT (build_v_must_defs, 10, "build v_must_defs");
293 }
294
295
296 /* Dispose of anything required by the operand routines.  */
297
298 void
299 fini_ssa_operands (void)
300 {
301   ggc_free (build_defs);
302   ggc_free (build_uses);
303   ggc_free (build_v_may_defs);
304   ggc_free (build_vuses);
305   ggc_free (build_v_must_defs);
306   build_defs = NULL;
307   build_uses = NULL;
308   build_v_may_defs = NULL;
309   build_vuses = NULL;
310   build_v_must_defs = NULL;
311   if (clobbered_v_may_defs)
312     {
313       ggc_free (clobbered_v_may_defs);
314       ggc_free (clobbered_vuses);
315       clobbered_v_may_defs = NULL;
316       clobbered_vuses = NULL;
317     }
318   if (ro_call_vuses)
319     {
320       ggc_free (ro_call_vuses);
321       ro_call_vuses = NULL;
322     }
323 }
324
325
326 /* All the finalize_ssa_* routines do the work required to turn the build_
327    VARRAY into an operand_vector of the appropriate type.  The original vector,
328    if any, is passed in for comparison and virtual SSA_NAME reuse.  If the
329    old vector is reused, the pointer passed in is set to NULL so that 
330    the memory is not freed when the old operands are freed.  */
331
332 /* Return a new def operand vector for STMT, comparing to OLD_OPS_P.  */
333
334 static def_optype
335 finalize_ssa_defs (def_optype *old_ops_p, tree stmt ATTRIBUTE_UNUSED)
336 {
337   unsigned num, x;
338   def_optype def_ops, old_ops;
339   bool build_diff;
340
341   num = VARRAY_ACTIVE_SIZE (build_defs);
342   if (num == 0)
343     return NULL;
344
345   /* There should only be a single real definition per assignment.  */
346   gcc_assert (TREE_CODE (stmt) != MODIFY_EXPR || num <= 1);
347
348   old_ops = *old_ops_p;
349
350   /* Compare old vector and new array.  */
351   build_diff = true;
352   if (old_ops && old_ops->num_defs == num)
353     {
354       build_diff = false;
355       for (x = 0; x < num; x++)
356         if (old_ops->defs[x].def != VARRAY_TREE_PTR (build_defs, x))
357           {
358             build_diff = true;
359             break;
360           }
361     }
362
363   if (!build_diff)
364     {
365       def_ops = old_ops;
366       *old_ops_p = NULL;
367     }
368   else
369     {
370       def_ops = allocate_def_optype (num);
371       for (x = 0; x < num ; x++)
372         def_ops->defs[x].def = VARRAY_TREE_PTR (build_defs, x);
373     }
374
375   VARRAY_POP_ALL (build_defs);
376
377   return def_ops;
378 }
379
380
381 /* Return a new use operand vector for STMT, comparing to OLD_OPS_P.  */
382
383 static use_optype
384 finalize_ssa_uses (use_optype *old_ops_p, tree stmt ATTRIBUTE_UNUSED)
385 {
386   unsigned num, x;
387   use_optype use_ops, old_ops;
388   bool build_diff;
389
390   num = VARRAY_ACTIVE_SIZE (build_uses);
391   if (num == 0)
392     return NULL;
393
394 #ifdef ENABLE_CHECKING
395   {
396     unsigned x;
397     /* If the pointer to the operand is the statement itself, something is
398        wrong.  It means that we are pointing to a local variable (the 
399        initial call to get_stmt_operands does not pass a pointer to a 
400        statement).  */
401     for (x = 0; x < num; x++)
402       gcc_assert (*(VARRAY_TREE_PTR (build_uses, x)) != stmt);
403   }
404 #endif
405   old_ops = *old_ops_p;
406
407   /* Check if the old vector and the new array are the same.  */
408   build_diff = true;
409   if (old_ops && old_ops->num_uses == num)
410     {
411       build_diff = false;
412       for (x = 0; x < num; x++)
413         if (old_ops->uses[x].use != VARRAY_TREE_PTR (build_uses, x))
414           {
415             build_diff = true;
416             break;
417           }
418     }
419
420   if (!build_diff)
421     {
422       use_ops = old_ops;
423       *old_ops_p = NULL;
424     }
425   else
426     {
427       use_ops = allocate_use_optype (num);
428       for (x = 0; x < num ; x++)
429         use_ops->uses[x].use = VARRAY_TREE_PTR (build_uses, x);
430     }
431   VARRAY_POP_ALL (build_uses);
432
433   return use_ops;
434 }
435
436
437 /* Return a new v_may_def operand vector for STMT, comparing to OLD_OPS_P.  */
438
439 static v_may_def_optype
440 finalize_ssa_v_may_defs (v_may_def_optype *old_ops_p)
441 {
442   unsigned num, x, i, old_num;
443   v_may_def_optype v_may_def_ops, old_ops;
444   tree result, var;
445   bool build_diff;
446
447   num = VARRAY_ACTIVE_SIZE (build_v_may_defs);
448   if (num == 0)
449     return NULL;
450
451   old_ops = *old_ops_p;
452
453   /* Check if the old vector and the new array are the same.  */
454   build_diff = true;
455   if (old_ops && old_ops->num_v_may_defs == num)
456     {
457       old_num = num;
458       build_diff = false;
459       for (x = 0; x < num; x++)
460         {
461           var = old_ops->v_may_defs[x].def;
462           if (TREE_CODE (var) == SSA_NAME)
463             var = SSA_NAME_VAR (var);
464           if (var != VARRAY_TREE (build_v_may_defs, x))
465             {
466               build_diff = true;
467               break;
468             }
469         }
470     }
471   else
472     old_num = (old_ops ? old_ops->num_v_may_defs : 0);
473
474   if (!build_diff)
475     {
476       v_may_def_ops = old_ops;
477       *old_ops_p = NULL;
478     }
479   else
480     {
481       v_may_def_ops = allocate_v_may_def_optype (num);
482       for (x = 0; x < num; x++)
483         {
484           var = VARRAY_TREE (build_v_may_defs, x);
485           /* Look for VAR in the old operands vector.  */
486           for (i = 0; i < old_num; i++)
487             {
488               result = old_ops->v_may_defs[i].def;
489               if (TREE_CODE (result) == SSA_NAME)
490                 result = SSA_NAME_VAR (result);
491               if (result == var)
492                 {
493                   v_may_def_ops->v_may_defs[x] = old_ops->v_may_defs[i];
494                   break;
495                 }
496             }
497           if (i == old_num)
498             {
499               v_may_def_ops->v_may_defs[x].def = var;
500               v_may_def_ops->v_may_defs[x].use = var;
501             }
502         }
503     }
504
505   /* Empty the V_MAY_DEF build vector after VUSES have been processed.  */
506
507   return v_may_def_ops;
508 }
509
510
511 /* Clear the in_list bits and empty the build array for v_may_defs.  */
512
513 static inline void
514 cleanup_v_may_defs (void)
515 {
516   unsigned x, num;
517   num = VARRAY_ACTIVE_SIZE (build_v_may_defs);
518
519   for (x = 0; x < num; x++)
520     {
521       tree t = VARRAY_TREE (build_v_may_defs, x);
522       var_ann_t ann = var_ann (t);
523       ann->in_v_may_def_list = 0;
524     }
525   VARRAY_POP_ALL (build_v_may_defs);
526 }
527
528 /* Return a new vuse operand vector, comparing to OLD_OPS_P.  */
529
530 static vuse_optype
531 finalize_ssa_vuses (vuse_optype *old_ops_p)
532 {
533   unsigned num, x, i, num_v_may_defs, old_num;
534   vuse_optype vuse_ops, old_ops;
535   bool build_diff;
536
537   num = VARRAY_ACTIVE_SIZE (build_vuses);
538   if (num == 0)
539     {
540       cleanup_v_may_defs ();
541       return NULL;
542     }
543
544   /* Remove superfluous VUSE operands.  If the statement already has a
545    V_MAY_DEF operation for a variable 'a', then a VUSE for 'a' is not
546    needed because V_MAY_DEFs imply a VUSE of the variable.  For instance,
547    suppose that variable 'a' is aliased:
548
549               # VUSE <a_2>
550               # a_3 = V_MAY_DEF <a_2>
551               a = a + 1;
552
553   The VUSE <a_2> is superfluous because it is implied by the V_MAY_DEF
554   operation.  */
555
556   num_v_may_defs = VARRAY_ACTIVE_SIZE (build_v_may_defs);
557
558   if (num_v_may_defs > 0)
559     {
560       size_t i;
561       tree vuse;
562       for (i = 0; i < VARRAY_ACTIVE_SIZE (build_vuses); i++)
563         {
564           vuse = VARRAY_TREE (build_vuses, i);
565           if (TREE_CODE (vuse) != SSA_NAME)
566             {
567               var_ann_t ann = var_ann (vuse);
568               ann->in_vuse_list = 0;
569               if (ann->in_v_may_def_list)
570                 {
571                   /* If we found a useless VUSE operand, remove it from the
572                      operand array by replacing it with the last active element
573                      in the operand array (unless the useless VUSE was the
574                      last operand, in which case we simply remove it.  */
575                   if (i != VARRAY_ACTIVE_SIZE (build_vuses) - 1)
576                     {
577                       VARRAY_TREE (build_vuses, i)
578                         = VARRAY_TREE (build_vuses,
579                                        VARRAY_ACTIVE_SIZE (build_vuses) - 1);
580                     }
581                   VARRAY_POP (build_vuses);
582
583                   /* We want to rescan the element at this index, unless
584                      this was the last element, in which case the loop
585                      terminates.  */
586                   i--;
587                 }
588             }
589         }
590     }
591   else
592     /* Clear out the in_list bits.  */
593     for (x = 0; x < num; x++)
594       {
595         tree t = VARRAY_TREE (build_vuses, x);
596         if (TREE_CODE (t) != SSA_NAME)
597           {
598             var_ann_t ann = var_ann (t);
599             ann->in_vuse_list = 0;
600           }
601       }
602
603
604   num = VARRAY_ACTIVE_SIZE (build_vuses);
605   /* We could have reduced the size to zero now, however.  */
606   if (num == 0)
607     {
608       cleanup_v_may_defs ();
609       return NULL;
610     }
611
612   old_ops = *old_ops_p;
613
614   /* Determine whether vuses is the same as the old vector.  */
615   build_diff = true;
616   if (old_ops && old_ops->num_vuses == num)
617     {
618       old_num = num;
619       build_diff = false;
620       for (x = 0; x < num ; x++)
621         {
622           tree v;
623           v = old_ops->vuses[x];
624           if (TREE_CODE (v) == SSA_NAME)
625             v = SSA_NAME_VAR (v);
626           if (v != VARRAY_TREE (build_vuses, x))
627             {
628               build_diff = true;
629               break;
630             }
631         }
632     }
633   else
634     old_num = (old_ops ? old_ops->num_vuses : 0);
635
636   if (!build_diff)
637     {
638       vuse_ops = old_ops;
639       *old_ops_p = NULL;
640     }
641   else
642     {
643       vuse_ops = allocate_vuse_optype (num);
644       for (x = 0; x < num; x++)
645         {
646           tree result, var = VARRAY_TREE (build_vuses, x);
647           /* Look for VAR in the old vector, and use that SSA_NAME.  */
648           for (i = 0; i < old_num; i++)
649             {
650               result = old_ops->vuses[i];
651               if (TREE_CODE (result) == SSA_NAME)
652                 result = SSA_NAME_VAR (result);
653               if (result == var)
654                 {
655                   vuse_ops->vuses[x] = old_ops->vuses[i];
656                   break;
657                 }
658             }
659           if (i == old_num)
660             vuse_ops->vuses[x] = var;
661         }
662     }
663
664   /* The v_may_def build vector wasn't freed because we needed it here.
665      Free it now with the vuses build vector.  */
666   VARRAY_POP_ALL (build_vuses);
667   cleanup_v_may_defs ();
668
669   return vuse_ops;
670 }
671
672 /* Return a new v_must_def operand vector for STMT, comparing to OLD_OPS_P.  */
673
674 static v_must_def_optype
675 finalize_ssa_v_must_defs (v_must_def_optype *old_ops_p, 
676                           tree stmt ATTRIBUTE_UNUSED)
677 {
678   unsigned num, x, i, old_num = 0;
679   v_must_def_optype v_must_def_ops, old_ops;
680   bool build_diff;
681
682   num = VARRAY_ACTIVE_SIZE (build_v_must_defs);
683   if (num == 0)
684     return NULL;
685
686   /* There should only be a single V_MUST_DEF per assignment.  */
687   gcc_assert (TREE_CODE (stmt) != MODIFY_EXPR || num <= 1);
688
689   old_ops = *old_ops_p;
690
691   /* Check if the old vector and the new array are the same.  */
692   build_diff = true;
693   if (old_ops && old_ops->num_v_must_defs == num)
694     {
695       old_num = num;
696       build_diff = false;
697       for (x = 0; x < num; x++)
698         {
699           tree var = old_ops->v_must_defs[x].def;
700           if (TREE_CODE (var) == SSA_NAME)
701             var = SSA_NAME_VAR (var);
702           if (var != VARRAY_TREE (build_v_must_defs, x))
703             {
704               build_diff = true;
705               break;
706             }
707         }
708     }
709   else
710     old_num = (old_ops ? old_ops->num_v_must_defs : 0);
711
712   if (!build_diff)
713     {
714       v_must_def_ops = old_ops;
715       *old_ops_p = NULL;
716     }
717   else
718     {
719       v_must_def_ops = allocate_v_must_def_optype (num);
720       for (x = 0; x < num ; x++)
721         {
722           tree result, var = VARRAY_TREE (build_v_must_defs, x);
723           /* Look for VAR in the original vector.  */
724           for (i = 0; i < old_num; i++)
725             {
726               result = old_ops->v_must_defs[i].def;
727               if (TREE_CODE (result) == SSA_NAME)
728                 result = SSA_NAME_VAR (result);
729               if (result == var)
730                 {
731                   v_must_def_ops->v_must_defs[x].def = old_ops->v_must_defs[i].def;
732                   v_must_def_ops->v_must_defs[x].use = old_ops->v_must_defs[i].use;
733                   break;
734                 }
735             }
736           if (i == old_num)
737             {
738               v_must_def_ops->v_must_defs[x].def = var;
739               v_must_def_ops->v_must_defs[x].use = var;
740             }
741         }
742     }
743   VARRAY_POP_ALL (build_v_must_defs);
744
745   return v_must_def_ops;
746 }
747
748
749 /* Finalize all the build vectors, fill the new ones into INFO.  */
750
751 static inline void
752 finalize_ssa_stmt_operands (tree stmt, stmt_operands_p old_ops, 
753                             stmt_operands_p new_ops)
754 {
755   new_ops->def_ops = finalize_ssa_defs (&(old_ops->def_ops), stmt);
756   new_ops->use_ops = finalize_ssa_uses (&(old_ops->use_ops), stmt);
757   new_ops->v_must_def_ops 
758     = finalize_ssa_v_must_defs (&(old_ops->v_must_def_ops), stmt);
759   new_ops->v_may_def_ops = finalize_ssa_v_may_defs (&(old_ops->v_may_def_ops));
760   new_ops->vuse_ops = finalize_ssa_vuses (&(old_ops->vuse_ops));
761 }
762
763
764 /* Start the process of building up operands vectors in INFO.  */
765
766 static inline void
767 start_ssa_stmt_operands (void)
768 {
769   gcc_assert (VARRAY_ACTIVE_SIZE (build_defs) == 0);
770   gcc_assert (VARRAY_ACTIVE_SIZE (build_uses) == 0);
771   gcc_assert (VARRAY_ACTIVE_SIZE (build_vuses) == 0);
772   gcc_assert (VARRAY_ACTIVE_SIZE (build_v_may_defs) == 0);
773   gcc_assert (VARRAY_ACTIVE_SIZE (build_v_must_defs) == 0);
774 }
775
776
777 /* Add DEF_P to the list of pointers to operands.  */
778
779 static inline void
780 append_def (tree *def_p)
781 {
782   VARRAY_PUSH_TREE_PTR (build_defs, def_p);
783 }
784
785
786 /* Add USE_P to the list of pointers to operands.  */
787
788 static inline void
789 append_use (tree *use_p)
790 {
791   VARRAY_PUSH_TREE_PTR (build_uses, use_p);
792 }
793
794
795 /* Add a new virtual may def for variable VAR to the build array.  */
796
797 static inline void
798 append_v_may_def (tree var)
799 {
800   var_ann_t ann = get_var_ann (var);
801
802   /* Don't allow duplicate entries.  */
803   if (ann->in_v_may_def_list)
804     return;
805   ann->in_v_may_def_list = 1;
806
807   VARRAY_PUSH_TREE (build_v_may_defs, var);
808 }
809
810
811 /* Add VAR to the list of virtual uses.  */
812
813 static inline void
814 append_vuse (tree var)
815 {
816
817   /* Don't allow duplicate entries.  */
818   if (TREE_CODE (var) != SSA_NAME)
819     {
820       var_ann_t ann = get_var_ann (var);
821
822       if (ann->in_vuse_list || ann->in_v_may_def_list)
823         return;
824       ann->in_vuse_list = 1;
825     }
826
827   VARRAY_PUSH_TREE (build_vuses, var);
828 }
829
830
831 /* Add VAR to the list of virtual must definitions for INFO.  */
832
833 static inline void
834 append_v_must_def (tree var)
835 {
836   unsigned i;
837
838   /* Don't allow duplicate entries.  */
839   for (i = 0; i < VARRAY_ACTIVE_SIZE (build_v_must_defs); i++)
840     if (var == VARRAY_TREE (build_v_must_defs, i))
841       return;
842
843   VARRAY_PUSH_TREE (build_v_must_defs, var);
844 }
845
846 /* Create an operands cache for STMT, returning it in NEW_OPS. OLD_OPS are the
847    original operands, and if ANN is non-null, appropriate stmt flags are set
848    in the stmt's annotation.  Note that some fields in old_ops may 
849    change to NULL, although none of the memory they originally pointed to 
850    will be destroyed.  It is appropriate to call free_stmt_operands() on 
851    the value returned in old_ops.
852
853    The rationale for this: Certain optimizations wish to examine the difference
854    between new_ops and old_ops after processing.  If a set of operands don't
855    change, new_ops will simply assume the pointer in old_ops, and the old_ops
856    pointer will be set to NULL, indicating no memory needs to be cleared.  
857    Usage might appear something like:
858
859        old_ops_copy = old_ops = stmt_ann(stmt)->operands;
860        build_ssa_operands (stmt, NULL, &old_ops, &new_ops);
861           <* compare old_ops_copy and new_ops *>
862        free_ssa_operands (old_ops);                                     */
863
864 static void
865 build_ssa_operands (tree stmt, stmt_ann_t ann, stmt_operands_p old_ops, 
866                     stmt_operands_p new_ops)
867 {
868   enum tree_code code;
869   tree_ann_t saved_ann = stmt->common.ann;
870   
871   /* Replace stmt's annotation with the one passed in for the duration
872      of the operand building process.  This allows "fake" stmts to be built
873      and not be included in other data structures which can be built here.  */
874   stmt->common.ann = (tree_ann_t) ann;
875   
876   /* Initially assume that the statement has no volatile operands, nor
877      makes aliased loads or stores.  */
878   if (ann)
879     {
880       ann->has_volatile_ops = false;
881       ann->makes_aliased_stores = false;
882       ann->makes_aliased_loads = false;
883     }
884
885   start_ssa_stmt_operands ();
886
887   code = TREE_CODE (stmt);
888   switch (code)
889     {
890     case MODIFY_EXPR:
891       /* First get operands from the RHS.  For the LHS, we use a V_MAY_DEF if
892          either only part of LHS is modified or if the RHS might throw,
893          otherwise, use V_MUST_DEF.
894
895          ??? If it might throw, we should represent somehow that it is killed
896          on the fallthrough path.  */
897       {
898         tree lhs = TREE_OPERAND (stmt, 0);
899         int lhs_flags = opf_is_def;
900
901         get_expr_operands (stmt, &TREE_OPERAND (stmt, 1), opf_none);
902
903         /* If the LHS is a VIEW_CONVERT_EXPR, it isn't changing whether
904            or not the entire LHS is modified; that depends on what's
905            inside the VIEW_CONVERT_EXPR.  */
906         if (TREE_CODE (lhs) == VIEW_CONVERT_EXPR)
907           lhs = TREE_OPERAND (lhs, 0);
908
909         if (TREE_CODE (lhs) != ARRAY_REF && TREE_CODE (lhs) != ARRAY_RANGE_REF
910             && TREE_CODE (lhs) != COMPONENT_REF
911             && TREE_CODE (lhs) != BIT_FIELD_REF
912             && TREE_CODE (lhs) != REALPART_EXPR
913             && TREE_CODE (lhs) != IMAGPART_EXPR)
914           lhs_flags |= opf_kill_def;
915
916         get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), lhs_flags);
917       }
918       break;
919
920     case COND_EXPR:
921       get_expr_operands (stmt, &COND_EXPR_COND (stmt), opf_none);
922       break;
923
924     case SWITCH_EXPR:
925       get_expr_operands (stmt, &SWITCH_COND (stmt), opf_none);
926       break;
927
928     case ASM_EXPR:
929       get_asm_expr_operands (stmt);
930       break;
931
932     case RETURN_EXPR:
933       get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), opf_none);
934       break;
935
936     case GOTO_EXPR:
937       get_expr_operands (stmt, &GOTO_DESTINATION (stmt), opf_none);
938       break;
939
940     case LABEL_EXPR:
941       get_expr_operands (stmt, &LABEL_EXPR_LABEL (stmt), opf_none);
942       break;
943
944       /* These nodes contain no variable references.  */
945     case BIND_EXPR:
946     case CASE_LABEL_EXPR:
947     case TRY_CATCH_EXPR:
948     case TRY_FINALLY_EXPR:
949     case EH_FILTER_EXPR:
950     case CATCH_EXPR:
951     case RESX_EXPR:
952       break;
953
954     default:
955       /* Notice that if get_expr_operands tries to use &STMT as the operand
956          pointer (which may only happen for USE operands), we will abort in
957          append_use.  This default will handle statements like empty
958          statements, or CALL_EXPRs that may appear on the RHS of a statement
959          or as statements themselves.  */
960       get_expr_operands (stmt, &stmt, opf_none);
961       break;
962     }
963
964   finalize_ssa_stmt_operands (stmt, old_ops, new_ops);
965   stmt->common.ann = saved_ann;
966 }
967
968
969 /* Free any operands vectors in OPS.  */
970
971 static void 
972 free_ssa_operands (stmt_operands_p ops)
973 {
974   if (ops->def_ops)
975     free_defs (&(ops->def_ops));
976   if (ops->use_ops)
977     free_uses (&(ops->use_ops));
978   if (ops->vuse_ops)
979     free_vuses (&(ops->vuse_ops));
980   if (ops->v_may_def_ops)
981     free_v_may_defs (&(ops->v_may_def_ops));
982   if (ops->v_must_def_ops)
983     free_v_must_defs (&(ops->v_must_def_ops));
984 }
985
986
987 /* Get the operands of statement STMT.  Note that repeated calls to
988    get_stmt_operands for the same statement will do nothing until the
989    statement is marked modified by a call to modify_stmt().  */
990
991 void
992 get_stmt_operands (tree stmt)
993 {
994   stmt_ann_t ann;
995   stmt_operands_t old_operands;
996
997   /* The optimizers cannot handle statements that are nothing but a
998      _DECL.  This indicates a bug in the gimplifier.  */
999   gcc_assert (!SSA_VAR_P (stmt));
1000
1001   /* Ignore error statements.  */
1002   if (TREE_CODE (stmt) == ERROR_MARK)
1003     return;
1004
1005   ann = get_stmt_ann (stmt);
1006
1007   /* If the statement has not been modified, the operands are still valid.  */
1008   if (!ann->modified)
1009     return;
1010
1011   timevar_push (TV_TREE_OPS);
1012
1013   old_operands = ann->operands;
1014   memset (&(ann->operands), 0, sizeof (stmt_operands_t));
1015
1016   build_ssa_operands (stmt, ann, &old_operands, &(ann->operands));
1017   free_ssa_operands (&old_operands);
1018
1019   /* Clear the modified bit for STMT.  Subsequent calls to
1020      get_stmt_operands for this statement will do nothing until the
1021      statement is marked modified by a call to modify_stmt().  */
1022   ann->modified = 0;
1023
1024   timevar_pop (TV_TREE_OPS);
1025 }
1026
1027
1028 /* Recursively scan the expression pointed by EXPR_P in statement referred to
1029    by INFO.  FLAGS is one of the OPF_* constants modifying how to interpret the
1030    operands found.  */
1031
1032 static void
1033 get_expr_operands (tree stmt, tree *expr_p, int flags)
1034 {
1035   enum tree_code code;
1036   enum tree_code_class class;
1037   tree expr = *expr_p;
1038   stmt_ann_t s_ann = stmt_ann (stmt);
1039
1040   if (expr == NULL || expr == error_mark_node)
1041     return;
1042
1043   code = TREE_CODE (expr);
1044   class = TREE_CODE_CLASS (code);
1045
1046   switch (code)
1047     {
1048     case ADDR_EXPR:
1049       /* We could have the address of a component, array member,
1050          etc which has interesting variable references.  */
1051       /* Taking the address of a variable does not represent a
1052          reference to it, but the fact that the stmt takes its address will be
1053          of interest to some passes (e.g. alias resolution).  */
1054       add_stmt_operand (expr_p, s_ann, 0);
1055
1056       /* If the address is invariant, there may be no interesting variable
1057          references inside.  */
1058       if (is_gimple_min_invariant (expr))
1059         return;
1060
1061       /* There should be no VUSEs created, since the referenced objects are
1062          not really accessed.  The only operands that we should find here
1063          are ARRAY_REF indices which will always be real operands (GIMPLE
1064          does not allow non-registers as array indices).  */
1065       flags |= opf_no_vops;
1066
1067       get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1068       return;
1069
1070     case SSA_NAME:
1071     case VAR_DECL:
1072     case PARM_DECL:
1073     case RESULT_DECL:
1074     case CONST_DECL:
1075       /* If we found a variable, add it to DEFS or USES depending
1076          on the operand flags.  */
1077       add_stmt_operand (expr_p, s_ann, flags);
1078       return;
1079
1080     case MISALIGNED_INDIRECT_REF:
1081       get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1082       /* fall through */
1083
1084     case ALIGN_INDIRECT_REF:
1085     case INDIRECT_REF:
1086       get_indirect_ref_operands (stmt, expr, flags);
1087       return;
1088
1089     case ARRAY_REF:
1090     case ARRAY_RANGE_REF:
1091       /* Treat array references as references to the virtual variable
1092          representing the array.  The virtual variable for an ARRAY_REF
1093          is the VAR_DECL for the array.  */
1094
1095       /* Add the virtual variable for the ARRAY_REF to VDEFS or VUSES
1096          according to the value of IS_DEF.  Recurse if the LHS of the
1097          ARRAY_REF node is not a regular variable.  */
1098       if (SSA_VAR_P (TREE_OPERAND (expr, 0)))
1099         add_stmt_operand (expr_p, s_ann, flags);
1100       else
1101         get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1102
1103       get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1104       get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1105       get_expr_operands (stmt, &TREE_OPERAND (expr, 3), opf_none);
1106       return;
1107
1108     case COMPONENT_REF:
1109     case REALPART_EXPR:
1110     case IMAGPART_EXPR:
1111       /* Similarly to arrays, references to compound variables (complex
1112          types and structures/unions) are globbed.
1113
1114          FIXME: This means that
1115
1116                         a.x = 6;
1117                         a.y = 7;
1118                         foo (a.x, a.y);
1119
1120          will not be constant propagated because the two partial
1121          definitions to 'a' will kill each other.  Note that SRA may be
1122          able to fix this problem if 'a' can be scalarized.  */
1123
1124       /* If the LHS of the compound reference is not a regular variable,
1125          recurse to keep looking for more operands in the subexpression.  */
1126       if (SSA_VAR_P (TREE_OPERAND (expr, 0)))
1127         add_stmt_operand (expr_p, s_ann, flags);
1128       else
1129         get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1130
1131       if (code == COMPONENT_REF)
1132         get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1133       return;
1134
1135     case WITH_SIZE_EXPR:
1136       /* WITH_SIZE_EXPR is a pass-through reference to its first argument,
1137          and an rvalue reference to its second argument.  */
1138       get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1139       get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1140       return;
1141
1142     case CALL_EXPR:
1143       get_call_expr_operands (stmt, expr);
1144       return;
1145
1146     case COND_EXPR:
1147     case VEC_COND_EXPR:
1148       get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_none);
1149       get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1150       get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1151       return;
1152
1153     case MODIFY_EXPR:
1154       {
1155         int subflags;
1156         tree op;
1157
1158         get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1159
1160         op = TREE_OPERAND (expr, 0);
1161         if (TREE_CODE (op) == WITH_SIZE_EXPR)
1162           op = TREE_OPERAND (expr, 0);
1163         if (TREE_CODE (op) == ARRAY_REF
1164             || TREE_CODE (op) == ARRAY_RANGE_REF
1165             || TREE_CODE (op) == COMPONENT_REF
1166             || TREE_CODE (op) == REALPART_EXPR
1167             || TREE_CODE (op) == IMAGPART_EXPR)
1168           subflags = opf_is_def;
1169         else
1170           subflags = opf_is_def | opf_kill_def;
1171
1172         get_expr_operands (stmt, &TREE_OPERAND (expr, 0), subflags);
1173         return;
1174       }
1175
1176     case CONSTRUCTOR:
1177       {
1178         /* General aggregate CONSTRUCTORs have been decomposed, but they
1179            are still in use as the COMPLEX_EXPR equivalent for vectors.  */
1180
1181         tree t;
1182         for (t = TREE_OPERAND (expr, 0); t ; t = TREE_CHAIN (t))
1183           get_expr_operands (stmt, &TREE_VALUE (t), opf_none);
1184
1185         return;
1186       }
1187
1188     case TRUTH_NOT_EXPR:
1189     case BIT_FIELD_REF:
1190     case VIEW_CONVERT_EXPR:
1191     do_unary:
1192       get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1193       return;
1194
1195     case TRUTH_AND_EXPR:
1196     case TRUTH_OR_EXPR:
1197     case TRUTH_XOR_EXPR:
1198     case COMPOUND_EXPR:
1199     case OBJ_TYPE_REF:
1200     do_binary:
1201       {
1202         tree op0 = TREE_OPERAND (expr, 0);
1203         tree op1 = TREE_OPERAND (expr, 1);
1204
1205         /* If it would be profitable to swap the operands, then do so to
1206            canonicalize the statement, enabling better optimization.
1207
1208            By placing canonicalization of such expressions here we
1209            transparently keep statements in canonical form, even
1210            when the statement is modified.  */
1211         if (tree_swap_operands_p (op0, op1, false))
1212           {
1213             /* For relationals we need to swap the operands
1214                and change the code.  */
1215             if (code == LT_EXPR
1216                 || code == GT_EXPR
1217                 || code == LE_EXPR
1218                 || code == GE_EXPR)
1219               {
1220                 TREE_SET_CODE (expr, swap_tree_comparison (code));
1221                 TREE_OPERAND (expr, 0) = op1;
1222                 TREE_OPERAND (expr, 1) = op0;
1223               }
1224           
1225             /* For a commutative operator we can just swap the operands.  */
1226             else if (commutative_tree_code (code))
1227               {
1228                 TREE_OPERAND (expr, 0) = op1;
1229                 TREE_OPERAND (expr, 1) = op0;
1230               }
1231           }
1232
1233         get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1234         get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1235         return;
1236       }
1237
1238     case REALIGN_LOAD_EXPR:
1239       {
1240         get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1241         get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1242         get_expr_operands (stmt, &TREE_OPERAND (expr, 2), flags);
1243         return;
1244       }
1245
1246     case BLOCK:
1247     case FUNCTION_DECL:
1248     case EXC_PTR_EXPR:
1249     case FILTER_EXPR:
1250     case LABEL_DECL:
1251       /* Expressions that make no memory references.  */
1252       return;
1253
1254     default:
1255       if (class == tcc_unary)
1256         goto do_unary;
1257       if (class == tcc_binary || class == tcc_comparison)
1258         goto do_binary;
1259       if (class == tcc_constant || class == tcc_type)
1260         return;
1261     }
1262
1263   /* If we get here, something has gone wrong.  */
1264 #ifdef ENABLE_CHECKING
1265   fprintf (stderr, "unhandled expression in get_expr_operands():\n");
1266   debug_tree (expr);
1267   fputs ("\n", stderr);
1268   internal_error ("internal error");
1269 #endif
1270   gcc_unreachable ();
1271 }
1272
1273
1274 /* Scan operands in the ASM_EXPR stmt referred to in INFO.  */
1275
1276 static void
1277 get_asm_expr_operands (tree stmt)
1278 {
1279   stmt_ann_t s_ann = stmt_ann (stmt);
1280   int noutputs = list_length (ASM_OUTPUTS (stmt));
1281   const char **oconstraints
1282     = (const char **) alloca ((noutputs) * sizeof (const char *));
1283   int i;
1284   tree link;
1285   const char *constraint;
1286   bool allows_mem, allows_reg, is_inout;
1287
1288   for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
1289     {
1290       oconstraints[i] = constraint
1291         = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1292       parse_output_constraint (&constraint, i, 0, 0,
1293           &allows_mem, &allows_reg, &is_inout);
1294
1295       /* This should have been split in gimplify_asm_expr.  */
1296       gcc_assert (!allows_reg || !is_inout);
1297
1298       /* Memory operands are addressable.  Note that STMT needs the
1299          address of this operand.  */
1300       if (!allows_reg && allows_mem)
1301         {
1302           tree t = get_base_address (TREE_VALUE (link));
1303           if (t && DECL_P (t))
1304             note_addressable (t, s_ann);
1305         }
1306
1307       get_expr_operands (stmt, &TREE_VALUE (link), opf_is_def);
1308     }
1309
1310   for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
1311     {
1312       constraint
1313         = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1314       parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1315           oconstraints, &allows_mem, &allows_reg);
1316
1317       /* Memory operands are addressable.  Note that STMT needs the
1318          address of this operand.  */
1319       if (!allows_reg && allows_mem)
1320         {
1321           tree t = get_base_address (TREE_VALUE (link));
1322           if (t && DECL_P (t))
1323             note_addressable (t, s_ann);
1324         }
1325
1326       get_expr_operands (stmt, &TREE_VALUE (link), 0);
1327     }
1328
1329
1330   /* Clobber memory for asm ("" : : : "memory");  */
1331   for (link = ASM_CLOBBERS (stmt); link; link = TREE_CHAIN (link))
1332     if (strcmp (TREE_STRING_POINTER (TREE_VALUE (link)), "memory") == 0)
1333       {
1334         unsigned i;
1335         bitmap_iterator bi;
1336
1337         /* Clobber all call-clobbered variables (or .GLOBAL_VAR if we
1338            decided to group them).  */
1339         if (global_var)
1340           add_stmt_operand (&global_var, s_ann, opf_is_def);
1341         else
1342           EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1343               {
1344                 tree var = referenced_var (i);
1345                 add_stmt_operand (&var, s_ann, opf_is_def);
1346               }
1347
1348         /* Now clobber all addressables.  */
1349         EXECUTE_IF_SET_IN_BITMAP (addressable_vars, 0, i, bi)
1350             {
1351               tree var = referenced_var (i);
1352               add_stmt_operand (&var, s_ann, opf_is_def);
1353             }
1354
1355         break;
1356       }
1357 }
1358
1359 /* A subroutine of get_expr_operands to handle INDIRECT_REF,
1360    ALIGN_INDIRECT_REF and MISALIGNED_INDIRECT_REF.  */
1361
1362 static void
1363 get_indirect_ref_operands (tree stmt, tree expr, int flags)
1364 {
1365   tree *pptr = &TREE_OPERAND (expr, 0);
1366   tree ptr = *pptr;
1367   stmt_ann_t s_ann = stmt_ann (stmt);
1368
1369   /* Stores into INDIRECT_REF operands are never killing definitions.  */
1370   flags &= ~opf_kill_def;
1371
1372   if (SSA_VAR_P (ptr))
1373     {
1374       struct ptr_info_def *pi = NULL;
1375
1376       /* If PTR has flow-sensitive points-to information, use it.  */
1377       if (TREE_CODE (ptr) == SSA_NAME
1378           && (pi = SSA_NAME_PTR_INFO (ptr)) != NULL
1379           && pi->name_mem_tag)
1380         {
1381           /* PTR has its own memory tag.  Use it.  */
1382           add_stmt_operand (&pi->name_mem_tag, s_ann, flags);
1383         }
1384       else
1385         {
1386           /* If PTR is not an SSA_NAME or it doesn't have a name
1387              tag, use its type memory tag.  */
1388           var_ann_t v_ann;
1389
1390           /* If we are emitting debugging dumps, display a warning if
1391              PTR is an SSA_NAME with no flow-sensitive alias
1392              information.  That means that we may need to compute
1393              aliasing again.  */
1394           if (dump_file
1395               && TREE_CODE (ptr) == SSA_NAME
1396               && pi == NULL)
1397             {
1398               fprintf (dump_file,
1399                   "NOTE: no flow-sensitive alias info for ");
1400               print_generic_expr (dump_file, ptr, dump_flags);
1401               fprintf (dump_file, " in ");
1402               print_generic_stmt (dump_file, stmt, dump_flags);
1403             }
1404
1405           if (TREE_CODE (ptr) == SSA_NAME)
1406             ptr = SSA_NAME_VAR (ptr);
1407           v_ann = var_ann (ptr);
1408           if (v_ann->type_mem_tag)
1409             add_stmt_operand (&v_ann->type_mem_tag, s_ann, flags);
1410         }
1411     }
1412
1413   /* If a constant is used as a pointer, we can't generate a real
1414      operand for it but we mark the statement volatile to prevent
1415      optimizations from messing things up.  */
1416   else if (TREE_CODE (ptr) == INTEGER_CST)
1417     {
1418       if (s_ann)
1419         s_ann->has_volatile_ops = true;
1420       return;
1421     }
1422
1423   /* Everything else *should* have been folded elsewhere, but users
1424      are smarter than we in finding ways to write invalid code.  We
1425      cannot just abort here.  If we were absolutely certain that we
1426      do handle all valid cases, then we could just do nothing here.
1427      That seems optimistic, so attempt to do something logical... */
1428   else if ((TREE_CODE (ptr) == PLUS_EXPR || TREE_CODE (ptr) == MINUS_EXPR)
1429            && TREE_CODE (TREE_OPERAND (ptr, 0)) == ADDR_EXPR
1430            && TREE_CODE (TREE_OPERAND (ptr, 1)) == INTEGER_CST)
1431     {
1432       /* Make sure we know the object is addressable.  */
1433       pptr = &TREE_OPERAND (ptr, 0);
1434       add_stmt_operand (pptr, s_ann, 0);
1435
1436       /* Mark the object itself with a VUSE.  */
1437       pptr = &TREE_OPERAND (*pptr, 0);
1438       get_expr_operands (stmt, pptr, flags);
1439       return;
1440     }
1441
1442   /* Ok, this isn't even is_gimple_min_invariant.  Something's broke.  */
1443   else
1444     gcc_unreachable ();
1445
1446   /* Add a USE operand for the base pointer.  */
1447   get_expr_operands (stmt, pptr, opf_none);
1448 }
1449
1450 /* A subroutine of get_expr_operands to handle CALL_EXPR.  */
1451
1452 static void
1453 get_call_expr_operands (tree stmt, tree expr)
1454 {
1455   tree op;
1456   int call_flags = call_expr_flags (expr);
1457
1458   /* If aliases have been computed already, add V_MAY_DEF or V_USE
1459      operands for all the symbols that have been found to be
1460      call-clobbered.
1461      
1462      Note that if aliases have not been computed, the global effects
1463      of calls will not be included in the SSA web. This is fine
1464      because no optimizer should run before aliases have been
1465      computed.  By not bothering with virtual operands for CALL_EXPRs
1466      we avoid adding superfluous virtual operands, which can be a
1467      significant compile time sink (See PR 15855).  */
1468   if (aliases_computed_p && !bitmap_empty_p (call_clobbered_vars))
1469     {
1470       /* A 'pure' or a 'const' functions never call clobber anything. 
1471          A 'noreturn' function might, but since we don't return anyway 
1472          there is no point in recording that.  */ 
1473       if (TREE_SIDE_EFFECTS (expr)
1474           && !(call_flags & (ECF_PURE | ECF_CONST | ECF_NORETURN)))
1475         add_call_clobber_ops (stmt);
1476       else if (!(call_flags & ECF_CONST))
1477         add_call_read_ops (stmt);
1478     }
1479
1480   /* Find uses in the called function.  */
1481   get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_none);
1482
1483   for (op = TREE_OPERAND (expr, 1); op; op = TREE_CHAIN (op))
1484     get_expr_operands (stmt, &TREE_VALUE (op), opf_none);
1485
1486   get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1487
1488 }
1489
1490
1491 /* Add *VAR_P to the appropriate operand array for INFO.  FLAGS is as in
1492    get_expr_operands.  If *VAR_P is a GIMPLE register, it will be added to
1493    the statement's real operands, otherwise it is added to virtual
1494    operands.  */
1495
1496 static void
1497 add_stmt_operand (tree *var_p, stmt_ann_t s_ann, int flags)
1498 {
1499   bool is_real_op;
1500   tree var, sym;
1501   var_ann_t v_ann;
1502
1503   var = *var_p;
1504   STRIP_NOPS (var);
1505
1506   /* If the operand is an ADDR_EXPR, add its operand to the list of
1507      variables that have had their address taken in this statement.  */
1508   if (TREE_CODE (var) == ADDR_EXPR)
1509     {
1510       note_addressable (TREE_OPERAND (var, 0), s_ann);
1511       return;
1512     }
1513
1514   /* If the original variable is not a scalar, it will be added to the list
1515      of virtual operands.  In that case, use its base symbol as the virtual
1516      variable representing it.  */
1517   is_real_op = is_gimple_reg (var);
1518   if (!is_real_op && !DECL_P (var))
1519     var = get_virtual_var (var);
1520
1521   /* If VAR is not a variable that we care to optimize, do nothing.  */
1522   if (var == NULL_TREE || !SSA_VAR_P (var))
1523     return;
1524
1525   sym = (TREE_CODE (var) == SSA_NAME ? SSA_NAME_VAR (var) : var);
1526   v_ann = var_ann (sym);
1527
1528   /* Mark statements with volatile operands.  Optimizers should back
1529      off from statements having volatile operands.  */
1530   if (TREE_THIS_VOLATILE (sym) && s_ann)
1531     s_ann->has_volatile_ops = true;
1532
1533   if (is_real_op)
1534     {
1535       /* The variable is a GIMPLE register.  Add it to real operands.  */
1536       if (flags & opf_is_def)
1537         append_def (var_p);
1538       else
1539         append_use (var_p);
1540     }
1541   else
1542     {
1543       varray_type aliases;
1544
1545       /* The variable is not a GIMPLE register.  Add it (or its aliases) to
1546          virtual operands, unless the caller has specifically requested
1547          not to add virtual operands (used when adding operands inside an
1548          ADDR_EXPR expression).  */
1549       if (flags & opf_no_vops)
1550         return;
1551
1552       aliases = v_ann->may_aliases;
1553
1554       if (aliases == NULL)
1555         {
1556           /* The variable is not aliased or it is an alias tag.  */
1557           if (flags & opf_is_def)
1558             {
1559               if (flags & opf_kill_def)
1560                 {
1561                   /* Only regular variables may get a V_MUST_DEF
1562                      operand.  */
1563                   gcc_assert (v_ann->mem_tag_kind == NOT_A_TAG);
1564                   /* V_MUST_DEF for non-aliased, non-GIMPLE register 
1565                     variable definitions.  */
1566                   append_v_must_def (var);
1567                 }
1568               else
1569                 {
1570                   /* Add a V_MAY_DEF for call-clobbered variables and
1571                      memory tags.  */
1572                   append_v_may_def (var);
1573                 }
1574             }
1575           else
1576             {
1577               append_vuse (var);
1578               if (s_ann && v_ann->is_alias_tag)
1579                 s_ann->makes_aliased_loads = 1;
1580             }
1581         }
1582       else
1583         {
1584           size_t i;
1585
1586           /* The variable is aliased.  Add its aliases to the virtual
1587              operands.  */
1588           gcc_assert (VARRAY_ACTIVE_SIZE (aliases) != 0);
1589
1590           if (flags & opf_is_def)
1591             {
1592               /* If the variable is also an alias tag, add a virtual
1593                  operand for it, otherwise we will miss representing
1594                  references to the members of the variable's alias set.
1595                  This fixes the bug in gcc.c-torture/execute/20020503-1.c.  */
1596               if (v_ann->is_alias_tag)
1597                 append_v_may_def (var);
1598
1599               for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
1600                 append_v_may_def (VARRAY_TREE (aliases, i));
1601
1602               if (s_ann)
1603                 s_ann->makes_aliased_stores = 1;
1604             }
1605           else
1606             {
1607               /* Similarly, append a virtual uses for VAR itself, when
1608                  it is an alias tag.  */
1609               if (v_ann->is_alias_tag)
1610                 append_vuse (var);
1611
1612               for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
1613                 append_vuse (VARRAY_TREE (aliases, i));
1614
1615               if (s_ann)
1616                 s_ann->makes_aliased_loads = 1;
1617             }
1618         }
1619     }
1620 }
1621
1622
1623 /* Record that VAR had its address taken in the statement with annotations
1624    S_ANN.  */
1625
1626 static void
1627 note_addressable (tree var, stmt_ann_t s_ann)
1628 {
1629   if (!s_ann)
1630     return;
1631
1632   var = get_base_address (var);
1633   if (var && SSA_VAR_P (var))
1634     {
1635       if (s_ann->addresses_taken == NULL)
1636         s_ann->addresses_taken = BITMAP_GGC_ALLOC ();
1637       bitmap_set_bit (s_ann->addresses_taken, var_ann (var)->uid);
1638     }
1639 }
1640
1641
1642 /* Add clobbering definitions for .GLOBAL_VAR or for each of the call
1643    clobbered variables in the function.  */
1644
1645 static void
1646 add_call_clobber_ops (tree stmt)
1647 {
1648   unsigned i;
1649   tree t;
1650   bitmap_iterator bi;
1651   stmt_ann_t s_ann = stmt_ann (stmt);
1652   struct stmt_ann_d empty_ann;
1653
1654   /* Functions that are not const, pure or never return may clobber
1655      call-clobbered variables.  */
1656   if (s_ann)
1657     s_ann->makes_clobbering_call = true;
1658
1659   /* If we created .GLOBAL_VAR earlier, just use it.  See compute_may_aliases 
1660      for the heuristic used to decide whether to create .GLOBAL_VAR or not.  */
1661   if (global_var)
1662     {
1663       add_stmt_operand (&global_var, s_ann, opf_is_def);
1664       return;
1665     }
1666
1667   /* If cache is valid, copy the elements into the build vectors.  */
1668   if (ssa_call_clobbered_cache_valid)
1669     {
1670       for (i = 0; i < VARRAY_ACTIVE_SIZE (clobbered_vuses); i++)
1671         {
1672           t = VARRAY_TREE (clobbered_vuses, i);
1673           gcc_assert (TREE_CODE (t) != SSA_NAME);
1674           var_ann (t)->in_vuse_list = 1;
1675           VARRAY_PUSH_TREE (build_vuses, t);
1676         }
1677       for (i = 0; i < VARRAY_ACTIVE_SIZE (clobbered_v_may_defs); i++)
1678         {
1679           t = VARRAY_TREE (clobbered_v_may_defs, i);
1680           gcc_assert (TREE_CODE (t) != SSA_NAME);
1681           var_ann (t)->in_v_may_def_list = 1;
1682           VARRAY_PUSH_TREE (build_v_may_defs, t);
1683         }
1684       if (s_ann)
1685         {
1686           s_ann->makes_aliased_loads = clobbered_aliased_loads;
1687           s_ann->makes_aliased_stores = clobbered_aliased_stores;
1688         }
1689       return;
1690     }
1691
1692   memset (&empty_ann, 0, sizeof (struct stmt_ann_d));
1693
1694   /* Add a V_MAY_DEF operand for every call clobbered variable.  */
1695   EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1696     {
1697       tree var = referenced_var (i);
1698       if (TREE_READONLY (var)
1699           && (TREE_STATIC (var) || DECL_EXTERNAL (var)))
1700         add_stmt_operand (&var, &empty_ann, opf_none);
1701       else
1702         add_stmt_operand (&var, &empty_ann, opf_is_def);
1703     }
1704
1705   clobbered_aliased_loads = empty_ann.makes_aliased_loads;
1706   clobbered_aliased_stores = empty_ann.makes_aliased_stores;
1707
1708   /* Set the flags for a stmt's annotation.  */
1709   if (s_ann)
1710     {
1711       s_ann->makes_aliased_loads = empty_ann.makes_aliased_loads;
1712       s_ann->makes_aliased_stores = empty_ann.makes_aliased_stores;
1713     }
1714
1715   /* Prepare empty cache vectors.  */
1716   if (clobbered_v_may_defs)
1717     {
1718       VARRAY_POP_ALL (clobbered_vuses);
1719       VARRAY_POP_ALL (clobbered_v_may_defs);
1720     }
1721   else
1722     {
1723       VARRAY_TREE_INIT (clobbered_v_may_defs, 10, "clobbered_v_may_defs");
1724       VARRAY_TREE_INIT (clobbered_vuses, 10, "clobbered_vuses");
1725     }
1726
1727   /* Now fill the clobbered cache with the values that have been found.  */
1728   for (i = 0; i < VARRAY_ACTIVE_SIZE (build_vuses); i++)
1729     VARRAY_PUSH_TREE (clobbered_vuses, VARRAY_TREE (build_vuses, i));
1730   for (i = 0; i < VARRAY_ACTIVE_SIZE (build_v_may_defs); i++)
1731     VARRAY_PUSH_TREE (clobbered_v_may_defs, VARRAY_TREE (build_v_may_defs, i));
1732
1733   ssa_call_clobbered_cache_valid = true;
1734 }
1735
1736
1737 /* Add VUSE operands for .GLOBAL_VAR or all call clobbered variables in the
1738    function.  */
1739
1740 static void
1741 add_call_read_ops (tree stmt)
1742 {
1743   unsigned i;
1744   tree t;
1745   bitmap_iterator bi;
1746   stmt_ann_t s_ann = stmt_ann (stmt);
1747   struct stmt_ann_d empty_ann;
1748
1749   /* if the function is not pure, it may reference memory.  Add
1750      a VUSE for .GLOBAL_VAR if it has been created.  See add_referenced_var
1751      for the heuristic used to decide whether to create .GLOBAL_VAR.  */
1752   if (global_var)
1753     {
1754       add_stmt_operand (&global_var, s_ann, opf_none);
1755       return;
1756     }
1757   
1758   /* If cache is valid, copy the elements into the build vector.  */
1759   if (ssa_ro_call_cache_valid)
1760     {
1761       for (i = 0; i < VARRAY_ACTIVE_SIZE (ro_call_vuses); i++)
1762         {
1763           t = VARRAY_TREE (ro_call_vuses, i);
1764           gcc_assert (TREE_CODE (t) != SSA_NAME);
1765           var_ann (t)->in_vuse_list = 1;
1766           VARRAY_PUSH_TREE (build_vuses, t);
1767         }
1768       if (s_ann)
1769         s_ann->makes_aliased_loads = ro_call_aliased_loads;
1770       return;
1771     }
1772
1773   memset (&empty_ann, 0, sizeof (struct stmt_ann_d));
1774
1775   /* Add a VUSE for each call-clobbered variable.  */
1776   EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1777     {
1778       tree var = referenced_var (i);
1779       add_stmt_operand (&var, &empty_ann, opf_none);
1780     }
1781
1782   ro_call_aliased_loads = empty_ann.makes_aliased_loads;
1783   if (s_ann)
1784     s_ann->makes_aliased_loads = empty_ann.makes_aliased_loads;
1785
1786   /* Prepare empty cache vectors.  */
1787   if (ro_call_vuses)
1788     VARRAY_POP_ALL (ro_call_vuses);
1789   else
1790     VARRAY_TREE_INIT (ro_call_vuses, 10, "ro_call_vuses");
1791
1792   /* Now fill the clobbered cache with the values that have been found.  */
1793   for (i = 0; i < VARRAY_ACTIVE_SIZE (build_vuses); i++)
1794     VARRAY_PUSH_TREE (ro_call_vuses, VARRAY_TREE (build_vuses, i));
1795
1796   ssa_ro_call_cache_valid = true;
1797 }
1798
1799 /* Copies virtual operands from SRC to DST.  */
1800
1801 void
1802 copy_virtual_operands (tree dst, tree src)
1803 {
1804   unsigned i;
1805   vuse_optype vuses = STMT_VUSE_OPS (src);
1806   v_may_def_optype v_may_defs = STMT_V_MAY_DEF_OPS (src);
1807   v_must_def_optype v_must_defs = STMT_V_MUST_DEF_OPS (src);
1808   vuse_optype *vuses_new = &stmt_ann (dst)->operands.vuse_ops;
1809   v_may_def_optype *v_may_defs_new = &stmt_ann (dst)->operands.v_may_def_ops;
1810   v_must_def_optype *v_must_defs_new = &stmt_ann (dst)->operands.v_must_def_ops;
1811
1812   if (vuses)
1813     {
1814       *vuses_new = allocate_vuse_optype (NUM_VUSES (vuses));
1815       for (i = 0; i < NUM_VUSES (vuses); i++)
1816         SET_VUSE_OP (*vuses_new, i, VUSE_OP (vuses, i));
1817     }
1818
1819   if (v_may_defs)
1820     {
1821       *v_may_defs_new = allocate_v_may_def_optype (NUM_V_MAY_DEFS (v_may_defs));
1822       for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++)
1823         {
1824           SET_V_MAY_DEF_OP (*v_may_defs_new, i, V_MAY_DEF_OP (v_may_defs, i));
1825           SET_V_MAY_DEF_RESULT (*v_may_defs_new, i, 
1826                                 V_MAY_DEF_RESULT (v_may_defs, i));
1827         }
1828     }
1829
1830   if (v_must_defs)
1831     {
1832       *v_must_defs_new = allocate_v_must_def_optype (NUM_V_MUST_DEFS (v_must_defs));
1833       for (i = 0; i < NUM_V_MUST_DEFS (v_must_defs); i++)
1834         {
1835           SET_V_MUST_DEF_RESULT (*v_must_defs_new, i, V_MUST_DEF_RESULT (v_must_defs, i));
1836           SET_V_MUST_DEF_KILL (*v_must_defs_new, i, V_MUST_DEF_KILL (v_must_defs, i));
1837         }
1838     }
1839 }
1840
1841
1842 /* Specifically for use in DOM's expression analysis.  Given a store, we
1843    create an artificial stmt which looks like a load from the store, this can
1844    be used to eliminate redundant loads.  OLD_OPS are the operands from the 
1845    store stmt, and NEW_STMT is the new load which represents a load of the
1846    values stored.  */
1847
1848 void
1849 create_ssa_artficial_load_stmt (stmt_operands_p old_ops, tree new_stmt)
1850 {
1851   stmt_ann_t ann;
1852   tree op;
1853   stmt_operands_t tmp;
1854   unsigned j;
1855
1856   memset (&tmp, 0, sizeof (stmt_operands_t));
1857   ann = get_stmt_ann (new_stmt);
1858
1859   /* Free operands just in case is was an existing stmt.  */
1860   free_ssa_operands (&(ann->operands));
1861
1862   build_ssa_operands (new_stmt, NULL, &tmp, &(ann->operands));
1863   free_vuses (&(ann->operands.vuse_ops));
1864   free_v_may_defs (&(ann->operands.v_may_def_ops));
1865   free_v_must_defs (&(ann->operands.v_must_def_ops));
1866   
1867   /* For each VDEF on the original statement, we want to create a
1868      VUSE of the V_MAY_DEF result or V_MUST_DEF op on the new 
1869      statement.  */
1870   for (j = 0; j < NUM_V_MAY_DEFS (old_ops->v_may_def_ops); j++)
1871     {
1872       op = V_MAY_DEF_RESULT (old_ops->v_may_def_ops, j);
1873       append_vuse (op);
1874     }
1875     
1876   for (j = 0; j < NUM_V_MUST_DEFS (old_ops->v_must_def_ops); j++)
1877     {
1878       op = V_MUST_DEF_RESULT (old_ops->v_must_def_ops, j);
1879       append_vuse (op);
1880     }
1881
1882   /* Now set the vuses for this new stmt.  */
1883   ann->operands.vuse_ops = finalize_ssa_vuses (&(tmp.vuse_ops));
1884 }
1885
1886 #include "gt-tree-ssa-operands.h"