OSDN Git Service

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