OSDN Git Service

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