OSDN Git Service

PR tree-optimization/22442
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-operands.c
1 /* SSA operands management for trees.
2    Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "function.h"
28 #include "diagnostic.h"
29 #include "tree-flow.h"
30 #include "tree-inline.h"
31 #include "tree-pass.h"
32 #include "ggc.h"
33 #include "timevar.h"
34 #include "toplev.h"
35 #include "langhooks.h"
36
37 /* This file contains the code required to manage the operands cache of the 
38    SSA optimizer.  For every stmt, we maintain an operand cache in the stmt 
39    annotation.  This cache contains operands that will be of interest to 
40    optimizers and other passes wishing to manipulate the IL. 
41
42    The operand type are broken up into REAL and VIRTUAL operands.  The real 
43    operands are represented as pointers into the stmt's operand tree.  Thus 
44    any manipulation of the real operands will be reflected in the actual tree.
45    Virtual operands are represented solely in the cache, although the base 
46    variable for the SSA_NAME may, or may not occur in the stmt's tree.  
47    Manipulation of the virtual operands will not be reflected in the stmt tree.
48
49    The routines in this file are concerned with creating this operand cache 
50    from a stmt tree.
51
52    The operand tree is the parsed by the various get_* routines which look 
53    through the stmt tree for the occurrence of operands which may be of 
54    interest, and calls are made to the append_* routines whenever one is 
55    found.  There are 5 of these routines, each representing one of the 
56    5 types of operands. Defs, Uses, Virtual Uses, Virtual May Defs, and 
57    Virtual Must Defs.
58
59    The append_* routines check for duplication, and simply keep a list of 
60    unique objects for each operand type in the build_* extendable vectors.
61
62    Once the stmt tree is completely parsed, the finalize_ssa_operands() 
63    routine is called, which proceeds to perform the finalization routine 
64    on each of the 5 operand vectors which have been built up.
65
66    If the stmt had a previous operand cache, the finalization routines 
67    attempt to match up the new operands with the old ones.  If it's a perfect 
68    match, the old vector is simply reused.  If it isn't a perfect match, then 
69    a new vector is created and the new operands are placed there.  For 
70    virtual operands, if the previous cache had SSA_NAME version of a 
71    variable, and that same variable occurs in the same operands cache, then 
72    the new cache vector will also get the same SSA_NAME.
73
74   i.e., if a stmt had a VUSE of 'a_5', and 'a' occurs in the new operand 
75   vector for VUSE, then the new vector will also be modified such that 
76   it contains 'a_5' rather than 'a'.
77
78 */
79
80
81 /* Flags to describe operand properties in helpers.  */
82
83 /* By default, operands are loaded.  */
84 #define opf_none        0
85
86 /* Operand is the target of an assignment expression or a 
87    call-clobbered variable  */
88 #define opf_is_def      (1 << 0)
89
90 /* Operand is the target of an assignment expression.  */
91 #define opf_kill_def    (1 << 1)
92
93 /* No virtual operands should be created in the expression.  This is used
94    when traversing ADDR_EXPR nodes which have different semantics than
95    other expressions.  Inside an ADDR_EXPR node, the only operands that we
96    need to consider are indices into arrays.  For instance, &a.b[i] should
97    generate a USE of 'i' but it should not generate a VUSE for 'a' nor a
98    VUSE for 'b'.  */
99 #define opf_no_vops     (1 << 2)
100
101 /* Operand is a "non-specific" kill for call-clobbers and such.  This is used
102    to distinguish "reset the world" events from explicit MODIFY_EXPRs.  */
103 #define opf_non_specific  (1 << 3)
104
105 /* This structure maintain a sorted list of operands which is created by
106    parse_ssa_operand.  */
107 struct opbuild_list_d GTY (())
108 {
109   varray_type vars;     /* The VAR_DECLS tree.  */
110   varray_type uid;      /* The sort value for virtual symbols.  */
111   varray_type next;     /* The next index in the sorted list.  */
112   int first;            /* First element in list.  */
113   unsigned num;         /* Number of elements.  */
114 };
115                                                                                 
116 #define OPBUILD_LAST     -1
117                                                                                 
118
119 /* Array for building all the def operands.  */
120 static GTY (()) struct opbuild_list_d build_defs;
121
122 /* Array for building all the use operands.  */
123 static GTY (()) struct opbuild_list_d build_uses;
124
125 /* Array for building all the v_may_def operands.  */
126 static GTY (()) struct opbuild_list_d build_v_may_defs;
127
128 /* Array for building all the vuse operands.  */
129 static GTY (()) struct opbuild_list_d build_vuses;
130
131 /* Array for building all the v_must_def operands.  */
132 static GTY (()) struct opbuild_list_d build_v_must_defs;
133
134 /* True if the operands for call clobbered vars are cached and valid.  */
135 bool ssa_call_clobbered_cache_valid;
136 bool ssa_ro_call_cache_valid;
137
138 /* These arrays are the cached operand vectors for call clobbered calls.  */
139 static VEC(tree,heap) *clobbered_v_may_defs;
140 static VEC(tree,heap) *clobbered_vuses;
141 static VEC(tree,heap) *ro_call_vuses;
142 static bool clobbered_aliased_loads;
143 static bool clobbered_aliased_stores;
144 static bool ro_call_aliased_loads;
145 static bool ops_active = false;
146
147 static GTY (()) struct ssa_operand_memory_d *operand_memory = NULL;
148 static unsigned operand_memory_index;
149
150 static void get_expr_operands (tree, tree *, int);
151 static void get_asm_expr_operands (tree);
152 static void get_indirect_ref_operands (tree, tree, int);
153 static void get_tmr_operands (tree, tree, int);
154 static void get_call_expr_operands (tree, tree);
155 static inline void append_def (tree *);
156 static inline void append_use (tree *);
157 static void append_v_may_def (tree);
158 static void append_v_must_def (tree);
159 static void add_call_clobber_ops (tree);
160 static void add_call_read_ops (tree);
161 static void add_stmt_operand (tree *, stmt_ann_t, int);
162 static void build_ssa_operands (tree stmt);
163                                                                                 
164 static def_optype_p free_defs = NULL;
165 static use_optype_p free_uses = NULL;
166 static vuse_optype_p free_vuses = NULL;
167 static maydef_optype_p free_maydefs = NULL;
168 static mustdef_optype_p free_mustdefs = NULL;
169
170 /* Initialize a virtual operand build LIST called NAME with NUM elements.  */
171
172 static inline void
173 opbuild_initialize_virtual (struct  opbuild_list_d *list, int num, 
174                            const char *name)
175 {
176   list->first = OPBUILD_LAST;
177   list->num = 0;
178   VARRAY_TREE_INIT (list->vars, num, name);
179   VARRAY_UINT_INIT (list->uid, num, "List UID");
180   VARRAY_INT_INIT (list->next, num, "List NEXT");
181 }
182
183
184 /* Initialize a real operand build LIST called NAME with NUM elements.  */
185
186 static inline void
187 opbuild_initialize_real (struct opbuild_list_d *list, int num, const char *name)
188 {
189   list->first = OPBUILD_LAST;
190   list->num = 0;
191   VARRAY_TREE_PTR_INIT (list->vars, num, name);
192   VARRAY_INT_INIT (list->next, num, "List NEXT");
193   /* The UID field is not needed since we sort based on the pointer value.  */
194   list->uid = NULL;
195 }
196
197
198 /* Free memory used in virtual operand build object LIST.  */
199
200 static inline void
201 opbuild_free (struct opbuild_list_d *list)
202 {
203   list->vars = NULL;
204   list->uid = NULL;
205   list->next = NULL;
206 }
207
208
209 /* Number of elements in an opbuild list.  */
210
211 static inline unsigned
212 opbuild_num_elems (struct opbuild_list_d *list)
213 {
214   return list->num;
215 }
216
217
218 /* Add VAR to the real operand list LIST, keeping it sorted and avoiding
219    duplicates.  The actual sort value is the tree pointer value.  */
220
221 static inline void
222 opbuild_append_real (struct opbuild_list_d *list, tree *var)
223 {
224   int index;
225
226 #ifdef ENABLE_CHECKING
227   /* Ensure the real operand doesn't exist already.  */
228   for (index = list->first; 
229        index != OPBUILD_LAST; 
230        index = VARRAY_INT (list->next, index))
231     gcc_assert (VARRAY_TREE_PTR (list->vars, index) != var);
232 #endif
233
234   /* First item in the list.  */
235   index = VARRAY_ACTIVE_SIZE (list->vars);
236   if (index == 0)
237     list->first = index;
238   else
239     VARRAY_INT (list->next, index - 1) = index;
240   VARRAY_PUSH_INT (list->next, OPBUILD_LAST);
241   VARRAY_PUSH_TREE_PTR (list->vars, var);
242   list->num++;
243 }
244
245
246 /* Add VAR to the virtual operand list LIST, keeping it sorted and avoiding
247    duplicates.  The actual sort value is the DECL UID of the base variable.  */
248
249 static inline void
250 opbuild_append_virtual (struct opbuild_list_d *list, tree var)
251 {
252   int index, curr, last;
253   unsigned int var_uid;
254
255   if (TREE_CODE (var) != SSA_NAME)
256     var_uid = DECL_UID (var);
257   else
258     var_uid = DECL_UID (SSA_NAME_VAR (var));
259
260   index = VARRAY_ACTIVE_SIZE (list->vars);
261
262   if (index == 0)
263     {
264       VARRAY_PUSH_TREE (list->vars, var);
265       VARRAY_PUSH_UINT (list->uid, var_uid);
266       VARRAY_PUSH_INT (list->next, OPBUILD_LAST);
267       list->first = 0;
268       list->num = 1;
269       return;
270     }
271
272   last = OPBUILD_LAST;
273   /* Find the correct spot in the sorted list.  */
274   for (curr = list->first; 
275        curr != OPBUILD_LAST; 
276        last = curr, curr = VARRAY_INT (list->next, curr))
277     {
278       if (VARRAY_UINT (list->uid, curr) > var_uid)
279         break;
280     }
281
282   if (last == OPBUILD_LAST)
283     {
284       /* First item in the list.  */
285       VARRAY_PUSH_INT (list->next, list->first);
286       list->first = index;
287     }
288   else
289     {
290       /* Don't enter duplicates at all.  */
291       if (VARRAY_UINT (list->uid, last) == var_uid)
292         return;
293       
294       VARRAY_PUSH_INT (list->next, VARRAY_INT (list->next, last));
295       VARRAY_INT (list->next, last) = index;
296     }
297   VARRAY_PUSH_TREE (list->vars, var);
298   VARRAY_PUSH_UINT (list->uid, var_uid);
299   list->num++;
300 }
301
302
303 /*  Return the first element index in LIST.  OPBUILD_LAST means there are no 
304     more elements.  */
305
306 static inline int
307 opbuild_first (struct opbuild_list_d *list)
308 {
309   if (list->num > 0)
310     return list->first;
311   else
312     return OPBUILD_LAST;
313 }
314
315
316 /* Return the next element after PREV in LIST.  */
317
318 static inline int
319 opbuild_next (struct opbuild_list_d *list, int prev)
320 {
321   return VARRAY_INT (list->next, prev);
322 }
323
324
325 /* Return the real element at index ELEM in LIST.  */
326
327 static inline tree *
328 opbuild_elem_real (struct opbuild_list_d *list, int elem)
329 {
330   return VARRAY_TREE_PTR (list->vars, elem);
331 }
332
333
334 /* Return the virtual element at index ELEM in LIST.  */
335
336 static inline tree
337 opbuild_elem_virtual (struct opbuild_list_d *list, int elem)
338 {
339   return VARRAY_TREE (list->vars, elem);
340 }
341
342
343 /* Return the virtual element uid at index ELEM in LIST.  */
344 static inline unsigned int
345 opbuild_elem_uid (struct opbuild_list_d *list, int elem)
346 {
347   return VARRAY_UINT (list->uid, elem);
348 }
349
350
351 /* Reset an operand build list.  */
352
353 static inline void
354 opbuild_clear (struct opbuild_list_d *list)
355 {
356   list->first = OPBUILD_LAST;
357   VARRAY_POP_ALL (list->vars);
358   VARRAY_POP_ALL (list->next);
359   if (list->uid)
360     VARRAY_POP_ALL (list->uid);
361   list->num = 0;
362 }
363
364
365 /* Remove ELEM from LIST where PREV is the previous element.  Return the next 
366    element.  */
367
368 static inline int 
369 opbuild_remove_elem (struct opbuild_list_d *list, int elem, int prev)
370 {
371   int ret;
372   if (prev != OPBUILD_LAST)
373     {
374       gcc_assert (VARRAY_INT (list->next, prev) == elem);
375       ret = VARRAY_INT (list->next, prev) = VARRAY_INT (list->next, elem);
376     }
377   else
378     {
379       gcc_assert (list->first == elem);
380       ret = list->first = VARRAY_INT (list->next, elem);
381     }
382   list->num--;
383   return ret;
384 }
385
386
387 /*  Return true if the ssa operands cache is active.  */
388
389 bool
390 ssa_operands_active (void)
391 {
392   return ops_active;
393 }
394
395
396 /* Initialize the operand cache routines.  */
397
398 void
399 init_ssa_operands (void)
400 {
401   opbuild_initialize_real (&build_defs, 5, "build defs");
402   opbuild_initialize_real (&build_uses, 10, "build uses");
403   opbuild_initialize_virtual (&build_vuses, 25, "build_vuses");
404   opbuild_initialize_virtual (&build_v_may_defs, 25, "build_v_may_defs");
405   opbuild_initialize_virtual (&build_v_must_defs, 25, "build_v_must_defs");
406   gcc_assert (operand_memory == NULL);
407   operand_memory_index = SSA_OPERAND_MEMORY_SIZE;
408   ops_active = true;
409 }
410
411
412 /* Dispose of anything required by the operand routines.  */
413
414 void
415 fini_ssa_operands (void)
416 {
417   struct ssa_operand_memory_d *ptr;
418   opbuild_free (&build_defs);
419   opbuild_free (&build_uses);
420   opbuild_free (&build_v_must_defs);
421   opbuild_free (&build_v_may_defs);
422   opbuild_free (&build_vuses);
423   free_defs = NULL;
424   free_uses = NULL;
425   free_vuses = NULL;
426   free_maydefs = NULL;
427   free_mustdefs = NULL;
428   while ((ptr = operand_memory) != NULL)
429     {
430       operand_memory = operand_memory->next;
431       ggc_free (ptr);
432     }
433
434   VEC_free (tree, heap, clobbered_v_may_defs);
435   VEC_free (tree, heap, clobbered_vuses);
436   VEC_free (tree, heap, ro_call_vuses);
437   ops_active = false;
438 }
439
440
441 /* Return memory for operands of SIZE chunks.  */
442                                                                               
443 static inline void *
444 ssa_operand_alloc (unsigned size)
445 {
446   char *ptr;
447   if (operand_memory_index + size >= SSA_OPERAND_MEMORY_SIZE)
448     {
449       struct ssa_operand_memory_d *ptr;
450       ptr = ggc_alloc (sizeof (struct ssa_operand_memory_d));
451       ptr->next = operand_memory;
452       operand_memory = ptr;
453       operand_memory_index = 0;
454     }
455   ptr = &(operand_memory->mem[operand_memory_index]);
456   operand_memory_index += size;
457   return ptr;
458 }
459
460
461 /* Make sure PTR is inn the correct immediate use list.  Since uses are simply
462    pointers into the stmt TREE, there is no way of telling if anyone has
463    changed what this pointer points to via TREE_OPERANDS (exp, 0) = <...>.
464    THe contents are different, but the the pointer is still the same.  This
465    routine will check to make sure PTR is in the correct list, and if it isn't
466    put it in the correct list.  We cannot simply check the previous node 
467    because all nodes in the same stmt might have be changed.  */
468
469 static inline void
470 correct_use_link (use_operand_p ptr, tree stmt)
471 {
472   use_operand_p prev;
473   tree root;
474
475   /*  Fold_stmt () may have changed the stmt pointers.  */
476   if (ptr->stmt != stmt)
477     ptr->stmt = stmt;
478
479   prev = ptr->prev;
480   if (prev)
481     {
482       bool stmt_mod = true;
483       /* Find the first element which isn't a SAFE iterator, is in a different
484          stmt, and is not a a modified stmt,  That node is in the correct list,
485          see if we are too.  */
486
487       while (stmt_mod)
488         {
489           while (prev->stmt == stmt || prev->stmt == NULL)
490             prev = prev->prev;
491           if (prev->use == NULL)
492             stmt_mod = false;
493           else
494             if ((stmt_mod = stmt_modified_p (prev->stmt)))
495               prev = prev->prev;
496         }
497
498       /* Get the ssa_name of the list the node is in.  */
499       if (prev->use == NULL)
500         root = prev->stmt;
501       else
502         root = *(prev->use);
503       /* If it's the right list, simply return.  */
504       if (root == *(ptr->use))
505         return;
506     }
507   /* Its in the wrong list if we reach here.  */
508   delink_imm_use (ptr);
509   link_imm_use (ptr, *(ptr->use));
510 }
511
512
513 #define FINALIZE_OPBUILD                build_defs
514 #define FINALIZE_OPBUILD_BASE(I)        opbuild_elem_real (&build_defs, (I))
515 #define FINALIZE_OPBUILD_ELEM(I)        opbuild_elem_real (&build_defs, (I))
516 #define FINALIZE_FUNC                   finalize_ssa_def_ops
517 #define FINALIZE_ALLOC                  alloc_def
518 #define FINALIZE_FREE                   free_defs
519 #define FINALIZE_TYPE                   struct def_optype_d
520 #define FINALIZE_ELEM(PTR)              ((PTR)->def_ptr)
521 #define FINALIZE_OPS                    DEF_OPS
522 #define FINALIZE_BASE(VAR)              VAR
523 #define FINALIZE_BASE_TYPE              tree *
524 #define FINALIZE_BASE_ZERO              NULL
525 #define FINALIZE_INITIALIZE(PTR, VAL, STMT)     FINALIZE_ELEM (PTR) = (VAL)
526 #include "tree-ssa-opfinalize.h"
527
528
529 /* This routine will create stmt operands for STMT from the def build list.  */
530
531 static void
532 finalize_ssa_defs (tree stmt)
533 {
534   unsigned int num = opbuild_num_elems (&build_defs);
535   /* There should only be a single real definition per assignment.  */
536   gcc_assert ((stmt && TREE_CODE (stmt) != MODIFY_EXPR) || num <= 1);
537
538   /* If there is an old list, often the new list is identical, or close, so
539      find the elements at the beginning that are the same as the vector.  */
540
541   finalize_ssa_def_ops (stmt);
542   opbuild_clear (&build_defs);
543 }
544
545 #define FINALIZE_OPBUILD        build_uses
546 #define FINALIZE_OPBUILD_BASE(I)        opbuild_elem_real (&build_uses, (I))
547 #define FINALIZE_OPBUILD_ELEM(I)        opbuild_elem_real (&build_uses, (I))
548 #define FINALIZE_FUNC           finalize_ssa_use_ops
549 #define FINALIZE_ALLOC          alloc_use
550 #define FINALIZE_FREE           free_uses
551 #define FINALIZE_TYPE           struct use_optype_d
552 #define FINALIZE_ELEM(PTR)      ((PTR)->use_ptr.use)
553 #define FINALIZE_OPS            USE_OPS
554 #define FINALIZE_USE_PTR(PTR)   USE_OP_PTR (PTR)
555 #define FINALIZE_BASE(VAR)      VAR
556 #define FINALIZE_BASE_TYPE      tree *
557 #define FINALIZE_BASE_ZERO      NULL
558 #define FINALIZE_INITIALIZE(PTR, VAL, STMT)                             \
559                                 (PTR)->use_ptr.use = (VAL);             \
560                                 link_imm_use_stmt (&((PTR)->use_ptr),   \
561                                                    *(VAL), (STMT))
562 #include "tree-ssa-opfinalize.h"
563
564 /* Return a new use operand vector for STMT, comparing to OLD_OPS_P.  */
565                                                                               
566 static void
567 finalize_ssa_uses (tree stmt)
568 {
569 #ifdef ENABLE_CHECKING
570   {
571     unsigned x;
572     unsigned num = opbuild_num_elems (&build_uses);
573
574     /* If the pointer to the operand is the statement itself, something is
575        wrong.  It means that we are pointing to a local variable (the 
576        initial call to get_stmt_operands does not pass a pointer to a 
577        statement).  */
578     for (x = 0; x < num; x++)
579       gcc_assert (*(opbuild_elem_real (&build_uses, x)) != stmt);
580   }
581 #endif
582   finalize_ssa_use_ops (stmt);
583   opbuild_clear (&build_uses);
584 }
585                                                                               
586                                                                               
587 /* Return a new v_may_def operand vector for STMT, comparing to OLD_OPS_P.  */                                                                                
588 #define FINALIZE_OPBUILD        build_v_may_defs
589 #define FINALIZE_OPBUILD_ELEM(I)        opbuild_elem_virtual (&build_v_may_defs, (I))
590 #define FINALIZE_OPBUILD_BASE(I)        opbuild_elem_uid (&build_v_may_defs, (I))
591 #define FINALIZE_FUNC           finalize_ssa_v_may_def_ops
592 #define FINALIZE_ALLOC          alloc_maydef
593 #define FINALIZE_FREE           free_maydefs
594 #define FINALIZE_TYPE           struct maydef_optype_d
595 #define FINALIZE_ELEM(PTR)      MAYDEF_RESULT (PTR)
596 #define FINALIZE_OPS            MAYDEF_OPS
597 #define FINALIZE_USE_PTR(PTR)   MAYDEF_OP_PTR (PTR)
598 #define FINALIZE_BASE_ZERO      0
599 #define FINALIZE_BASE(VAR)      ((TREE_CODE (VAR) == SSA_NAME)          \
600                                 ? DECL_UID (SSA_NAME_VAR (VAR)) : DECL_UID ((VAR)))
601 #define FINALIZE_BASE_TYPE      unsigned
602 #define FINALIZE_INITIALIZE(PTR, VAL, STMT)                             \
603                                 (PTR)->def_var = (VAL);                 \
604                                 (PTR)->use_var = (VAL);                 \
605                                 (PTR)->use_ptr.use = &((PTR)->use_var); \
606                                 link_imm_use_stmt (&((PTR)->use_ptr),   \
607                                                    (VAL), (STMT))
608 #include "tree-ssa-opfinalize.h"
609                                                                               
610                                                                               
611 static void
612 finalize_ssa_v_may_defs (tree stmt)
613 {
614   finalize_ssa_v_may_def_ops (stmt);
615 }
616                                                                                
617
618 /* Clear the in_list bits and empty the build array for v_may_defs.  */
619
620 static inline void
621 cleanup_v_may_defs (void)
622 {
623   unsigned x, num;
624   num = opbuild_num_elems (&build_v_may_defs);
625
626   for (x = 0; x < num; x++)
627     {
628       tree t = opbuild_elem_virtual (&build_v_may_defs, x);
629       if (TREE_CODE (t) != SSA_NAME)
630         {
631           var_ann_t ann = var_ann (t);
632           ann->in_v_may_def_list = 0;
633         }
634     }
635   opbuild_clear (&build_v_may_defs);
636 }                                                                             
637
638                                                                               
639 #define FINALIZE_OPBUILD        build_vuses
640 #define FINALIZE_OPBUILD_ELEM(I)        opbuild_elem_virtual (&build_vuses, (I))
641 #define FINALIZE_OPBUILD_BASE(I)        opbuild_elem_uid (&build_vuses, (I))
642 #define FINALIZE_FUNC           finalize_ssa_vuse_ops
643 #define FINALIZE_ALLOC          alloc_vuse
644 #define FINALIZE_FREE           free_vuses
645 #define FINALIZE_TYPE           struct vuse_optype_d
646 #define FINALIZE_ELEM(PTR)      VUSE_OP (PTR)
647 #define FINALIZE_OPS            VUSE_OPS
648 #define FINALIZE_USE_PTR(PTR)   VUSE_OP_PTR (PTR)
649 #define FINALIZE_BASE_ZERO      0
650 #define FINALIZE_BASE(VAR)      ((TREE_CODE (VAR) == SSA_NAME)          \
651                                 ? DECL_UID (SSA_NAME_VAR (VAR)) : DECL_UID ((VAR)))
652 #define FINALIZE_BASE_TYPE      unsigned
653 #define FINALIZE_INITIALIZE(PTR, VAL, STMT)                             \
654                                 (PTR)->use_var = (VAL);                 \
655                                 (PTR)->use_ptr.use = &((PTR)->use_var); \
656                                 link_imm_use_stmt (&((PTR)->use_ptr),   \
657                                                    (VAL), (STMT))
658 #include "tree-ssa-opfinalize.h"
659
660
661 /* Return a new vuse operand vector, comparing to OLD_OPS_P.  */
662                                                                               
663 static void
664 finalize_ssa_vuses (tree stmt)
665 {
666   unsigned num, num_v_may_defs;
667   int vuse_index;
668
669   /* Remove superfluous VUSE operands.  If the statement already has a
670    V_MAY_DEF operation for a variable 'a', then a VUSE for 'a' is not
671    needed because V_MAY_DEFs imply a VUSE of the variable.  For instance,
672    suppose that variable 'a' is aliased:
673
674               # VUSE <a_2>
675               # a_3 = V_MAY_DEF <a_2>
676               a = a + 1;
677
678   The VUSE <a_2> is superfluous because it is implied by the V_MAY_DEF
679   operation.  */
680
681   num = opbuild_num_elems (&build_vuses);
682   num_v_may_defs = opbuild_num_elems (&build_v_may_defs);
683
684   if (num > 0 && num_v_may_defs > 0)
685     {
686       int last = OPBUILD_LAST;
687       vuse_index = opbuild_first (&build_vuses);
688       for ( ; vuse_index != OPBUILD_LAST; )
689         {
690           tree vuse;
691           vuse = opbuild_elem_virtual (&build_vuses, vuse_index);
692           if (TREE_CODE (vuse) != SSA_NAME)
693             {
694               var_ann_t ann = var_ann (vuse);
695               ann->in_vuse_list = 0;
696               if (ann->in_v_may_def_list)
697                 {
698                   vuse_index = opbuild_remove_elem (&build_vuses, vuse_index, 
699                                                     last);
700                   continue;
701                 }
702             }
703           last = vuse_index;
704           vuse_index = opbuild_next (&build_vuses, vuse_index);
705         }
706     }
707   else
708     /* Clear out the in_list bits.  */
709     for (vuse_index = opbuild_first (&build_vuses);
710          vuse_index != OPBUILD_LAST;
711          vuse_index = opbuild_next (&build_vuses, vuse_index))
712       {
713         tree t = opbuild_elem_virtual (&build_vuses, vuse_index);
714         if (TREE_CODE (t) != SSA_NAME)
715           {
716             var_ann_t ann = var_ann (t);
717             ann->in_vuse_list = 0;
718           }
719       }
720
721   finalize_ssa_vuse_ops (stmt);
722   /* The v_may_def build vector wasn't cleaned up because we needed it.  */
723   cleanup_v_may_defs ();
724                                                                               
725   /* Free the vuses build vector.  */
726   opbuild_clear (&build_vuses);
727
728 }
729                                                                               
730 /* Return a new v_must_def operand vector for STMT, comparing to OLD_OPS_P.  */
731                                                                               
732 #define FINALIZE_OPBUILD        build_v_must_defs
733 #define FINALIZE_OPBUILD_ELEM(I)        opbuild_elem_virtual (&build_v_must_defs, (I))
734 #define FINALIZE_OPBUILD_BASE(I)        opbuild_elem_uid (&build_v_must_defs, (I))
735 #define FINALIZE_FUNC           finalize_ssa_v_must_def_ops
736 #define FINALIZE_ALLOC          alloc_mustdef
737 #define FINALIZE_FREE           free_mustdefs
738 #define FINALIZE_TYPE           struct mustdef_optype_d
739 #define FINALIZE_ELEM(PTR)      MUSTDEF_RESULT (PTR)
740 #define FINALIZE_OPS            MUSTDEF_OPS
741 #define FINALIZE_USE_PTR(PTR)   MUSTDEF_KILL_PTR (PTR)
742 #define FINALIZE_BASE_ZERO      0
743 #define FINALIZE_BASE(VAR)      ((TREE_CODE (VAR) == SSA_NAME)          \
744                                 ? DECL_UID (SSA_NAME_VAR (VAR)) : DECL_UID ((VAR)))
745 #define FINALIZE_BASE_TYPE      unsigned
746 #define FINALIZE_INITIALIZE(PTR, VAL, STMT)                             \
747                                 (PTR)->def_var = (VAL);                 \
748                                 (PTR)->kill_var = (VAL);                \
749                                 (PTR)->use_ptr.use = &((PTR)->kill_var);\
750                                 link_imm_use_stmt (&((PTR)->use_ptr),   \
751                                                    (VAL), (STMT))
752 #include "tree-ssa-opfinalize.h"
753
754
755 static void
756 finalize_ssa_v_must_defs (tree stmt)
757 {
758   /* In the presence of subvars, there may be more than one V_MUST_DEF per
759      statement (one for each subvar).  It is a bit expensive to verify that
760      all must-defs in a statement belong to subvars if there is more than one
761      MUST-def, so we don't do it.  Suffice to say, if you reach here without
762      having subvars, and have num >1, you have hit a bug. */
763
764   finalize_ssa_v_must_def_ops (stmt);
765   opbuild_clear (&build_v_must_defs);
766 }
767
768
769 /* Finalize all the build vectors, fill the new ones into INFO.  */
770                                                                               
771 static inline void
772 finalize_ssa_stmt_operands (tree stmt)
773 {
774   finalize_ssa_defs (stmt);
775   finalize_ssa_uses (stmt);
776   finalize_ssa_v_must_defs (stmt);
777   finalize_ssa_v_may_defs (stmt);
778   finalize_ssa_vuses (stmt);
779 }
780
781
782 /* Start the process of building up operands vectors in INFO.  */
783
784 static inline void
785 start_ssa_stmt_operands (void)
786 {
787   gcc_assert (opbuild_num_elems (&build_defs) == 0);
788   gcc_assert (opbuild_num_elems (&build_uses) == 0);
789   gcc_assert (opbuild_num_elems (&build_vuses) == 0);
790   gcc_assert (opbuild_num_elems (&build_v_may_defs) == 0);
791   gcc_assert (opbuild_num_elems (&build_v_must_defs) == 0);
792 }
793
794
795 /* Add DEF_P to the list of pointers to operands.  */
796
797 static inline void
798 append_def (tree *def_p)
799 {
800   opbuild_append_real (&build_defs, def_p);
801 }
802
803
804 /* Add USE_P to the list of pointers to operands.  */
805
806 static inline void
807 append_use (tree *use_p)
808 {
809   opbuild_append_real (&build_uses, use_p);
810 }
811
812
813 /* Add a new virtual may def for variable VAR to the build array.  */
814
815 static inline void
816 append_v_may_def (tree var)
817 {
818   if (TREE_CODE (var) != SSA_NAME)
819     {
820       var_ann_t ann = get_var_ann (var);
821
822       /* Don't allow duplicate entries.  */
823       if (ann->in_v_may_def_list)
824         return;
825       ann->in_v_may_def_list = 1;
826     }
827
828   opbuild_append_virtual (&build_v_may_defs, var);
829 }
830
831
832 /* Add VAR to the list of virtual uses.  */
833
834 static inline void
835 append_vuse (tree var)
836 {
837
838   /* Don't allow duplicate entries.  */
839   if (TREE_CODE (var) != SSA_NAME)
840     {
841       var_ann_t ann = get_var_ann (var);
842
843       if (ann->in_vuse_list || ann->in_v_may_def_list)
844         return;
845       ann->in_vuse_list = 1;
846     }
847
848   opbuild_append_virtual (&build_vuses, var);
849 }
850
851
852 /* Add VAR to the list of virtual must definitions for INFO.  */
853
854 static inline void
855 append_v_must_def (tree var)
856 {
857   unsigned i;
858
859   /* Don't allow duplicate entries.  */
860   for (i = 0; i < opbuild_num_elems (&build_v_must_defs); i++)
861     if (var == opbuild_elem_virtual (&build_v_must_defs, i))
862       return;
863
864   opbuild_append_virtual (&build_v_must_defs, var);
865 }
866
867
868 /* Parse STMT looking for operands.  OLD_OPS is the original stmt operand
869    cache for STMT, if it existed before.  When finished, the various build_*
870    operand vectors will have potential operands. in them.  */
871                                                                                 
872 static void
873 parse_ssa_operands (tree stmt)
874 {
875   enum tree_code code;
876
877   code = TREE_CODE (stmt);
878   switch (code)
879     {
880     case MODIFY_EXPR:
881       /* First get operands from the RHS.  For the LHS, we use a V_MAY_DEF if
882          either only part of LHS is modified or if the RHS might throw,
883          otherwise, use V_MUST_DEF.
884
885          ??? If it might throw, we should represent somehow that it is killed
886          on the fallthrough path.  */
887       {
888         tree lhs = TREE_OPERAND (stmt, 0);
889         int lhs_flags = opf_is_def;
890
891         get_expr_operands (stmt, &TREE_OPERAND (stmt, 1), opf_none);
892
893         /* If the LHS is a VIEW_CONVERT_EXPR, it isn't changing whether
894            or not the entire LHS is modified; that depends on what's
895            inside the VIEW_CONVERT_EXPR.  */
896         if (TREE_CODE (lhs) == VIEW_CONVERT_EXPR)
897           lhs = TREE_OPERAND (lhs, 0);
898
899         if (TREE_CODE (lhs) != ARRAY_REF
900             && TREE_CODE (lhs) != ARRAY_RANGE_REF
901             && TREE_CODE (lhs) != BIT_FIELD_REF
902             && TREE_CODE (lhs) != REALPART_EXPR
903             && TREE_CODE (lhs) != IMAGPART_EXPR)
904           lhs_flags |= opf_kill_def;
905
906         get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), lhs_flags);
907       }
908       break;
909
910     case COND_EXPR:
911       get_expr_operands (stmt, &COND_EXPR_COND (stmt), opf_none);
912       break;
913
914     case SWITCH_EXPR:
915       get_expr_operands (stmt, &SWITCH_COND (stmt), opf_none);
916       break;
917
918     case ASM_EXPR:
919       get_asm_expr_operands (stmt);
920       break;
921
922     case RETURN_EXPR:
923       get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), opf_none);
924       break;
925
926     case GOTO_EXPR:
927       get_expr_operands (stmt, &GOTO_DESTINATION (stmt), opf_none);
928       break;
929
930     case LABEL_EXPR:
931       get_expr_operands (stmt, &LABEL_EXPR_LABEL (stmt), opf_none);
932       break;
933
934       /* These nodes contain no variable references.  */
935     case BIND_EXPR:
936     case CASE_LABEL_EXPR:
937     case TRY_CATCH_EXPR:
938     case TRY_FINALLY_EXPR:
939     case EH_FILTER_EXPR:
940     case CATCH_EXPR:
941     case RESX_EXPR:
942       break;
943
944     default:
945       /* Notice that if get_expr_operands tries to use &STMT as the operand
946          pointer (which may only happen for USE operands), we will fail in
947          append_use.  This default will handle statements like empty
948          statements, or CALL_EXPRs that may appear on the RHS of a statement
949          or as statements themselves.  */
950       get_expr_operands (stmt, &stmt, opf_none);
951       break;
952     }
953 }
954
955 /* Create an operands cache for STMT, returning it in NEW_OPS. OLD_OPS are the
956    original operands, and if ANN is non-null, appropriate stmt flags are set
957    in the stmt's annotation.  If ANN is NULL, this is not considered a "real"
958    stmt, and none of the operands will be entered into their respective
959    immediate uses tables.  This is to allow stmts to be processed when they
960    are not actually in the CFG.
961
962    Note that some fields in old_ops may change to NULL, although none of the
963    memory they originally pointed to will be destroyed.  It is appropriate
964    to call free_stmt_operands() on the value returned in old_ops.
965
966    The rationale for this: Certain optimizations wish to examine the difference
967    between new_ops and old_ops after processing.  If a set of operands don't
968    change, new_ops will simply assume the pointer in old_ops, and the old_ops
969    pointer will be set to NULL, indicating no memory needs to be cleared.  
970    Usage might appear something like:
971
972        old_ops_copy = old_ops = stmt_ann(stmt)->operands;
973        build_ssa_operands (stmt, NULL, &old_ops, &new_ops);
974           <* compare old_ops_copy and new_ops *>
975        free_ssa_operands (old_ops);                                     */
976
977 static void
978 build_ssa_operands (tree stmt)
979 {
980   stmt_ann_t ann = get_stmt_ann (stmt);
981   
982   /* Initially assume that the statement has no volatile operands, nor
983      makes aliased loads or stores.  */
984   if (ann)
985     {
986       ann->has_volatile_ops = false;
987       ann->makes_aliased_stores = false;
988       ann->makes_aliased_loads = false;
989     }
990
991   start_ssa_stmt_operands ();
992
993   parse_ssa_operands (stmt);
994
995   finalize_ssa_stmt_operands (stmt);
996 }
997
998
999 /* Free any operands vectors in OPS.  */
1000 void 
1001 free_ssa_operands (stmt_operands_p ops)
1002 {
1003   ops->def_ops = NULL;
1004   ops->use_ops = NULL;
1005   ops->maydef_ops = NULL;
1006   ops->mustdef_ops = NULL;
1007   ops->vuse_ops = NULL;
1008 }
1009
1010
1011 /* Get the operands of statement STMT.  Note that repeated calls to
1012    get_stmt_operands for the same statement will do nothing until the
1013    statement is marked modified by a call to mark_stmt_modified().  */
1014
1015 void
1016 update_stmt_operands (tree stmt)
1017 {
1018   stmt_ann_t ann = get_stmt_ann (stmt);
1019   /* If get_stmt_operands is called before SSA is initialized, dont
1020   do anything.  */
1021   if (!ssa_operands_active ())
1022     return;
1023   /* The optimizers cannot handle statements that are nothing but a
1024      _DECL.  This indicates a bug in the gimplifier.  */
1025   gcc_assert (!SSA_VAR_P (stmt));
1026
1027   gcc_assert (ann->modified);
1028
1029   timevar_push (TV_TREE_OPS);
1030
1031   build_ssa_operands (stmt);
1032
1033   /* Clear the modified bit for STMT.  Subsequent calls to
1034      get_stmt_operands for this statement will do nothing until the
1035      statement is marked modified by a call to mark_stmt_modified().  */
1036   ann->modified = 0;
1037
1038   timevar_pop (TV_TREE_OPS);
1039 }
1040
1041   
1042 /* Copies virtual operands from SRC to DST.  */
1043
1044 void
1045 copy_virtual_operands (tree dest, tree src)
1046 {
1047   tree t;
1048   ssa_op_iter iter, old_iter;
1049   use_operand_p use_p, u2;
1050   def_operand_p def_p, d2;
1051
1052   build_ssa_operands (dest);
1053
1054   /* Copy all the virtual fields.  */
1055   FOR_EACH_SSA_TREE_OPERAND (t, src, iter, SSA_OP_VUSE)
1056     append_vuse (t);
1057   FOR_EACH_SSA_TREE_OPERAND (t, src, iter, SSA_OP_VMAYDEF)
1058     append_v_may_def (t);
1059   FOR_EACH_SSA_TREE_OPERAND (t, src, iter, SSA_OP_VMUSTDEF)
1060     append_v_must_def (t);
1061
1062   if (opbuild_num_elems (&build_vuses) == 0
1063       && opbuild_num_elems (&build_v_may_defs) == 0
1064       && opbuild_num_elems (&build_v_must_defs) == 0)
1065     return;
1066
1067   /* Now commit the virtual operands to this stmt.  */
1068   finalize_ssa_v_must_defs (dest);
1069   finalize_ssa_v_may_defs (dest);
1070   finalize_ssa_vuses (dest);
1071
1072   /* Finally, set the field to the same values as then originals.  */
1073
1074   
1075   t = op_iter_init_tree (&old_iter, src, SSA_OP_VUSE);
1076   FOR_EACH_SSA_USE_OPERAND (use_p, dest, iter, SSA_OP_VUSE)
1077     {
1078       gcc_assert (!op_iter_done (&old_iter));
1079       SET_USE (use_p, t);
1080       t = op_iter_next_tree (&old_iter);
1081     }
1082   gcc_assert (op_iter_done (&old_iter));
1083
1084   op_iter_init_maydef (&old_iter, src, &u2, &d2);
1085   FOR_EACH_SSA_MAYDEF_OPERAND (def_p, use_p, dest, iter)
1086     {
1087       gcc_assert (!op_iter_done (&old_iter));
1088       SET_USE (use_p, USE_FROM_PTR (u2));
1089       SET_DEF (def_p, DEF_FROM_PTR (d2));
1090       op_iter_next_maymustdef (&u2, &d2, &old_iter);
1091     }
1092   gcc_assert (op_iter_done (&old_iter));
1093
1094   op_iter_init_mustdef (&old_iter, src, &u2, &d2);
1095   FOR_EACH_SSA_MUSTDEF_OPERAND (def_p, use_p, dest, iter)
1096     {
1097       gcc_assert (!op_iter_done (&old_iter));
1098       SET_USE (use_p, USE_FROM_PTR (u2));
1099       SET_DEF (def_p, DEF_FROM_PTR (d2));
1100       op_iter_next_maymustdef (&u2, &d2, &old_iter);
1101     }
1102   gcc_assert (op_iter_done (&old_iter));
1103
1104 }
1105
1106
1107 /* Specifically for use in DOM's expression analysis.  Given a store, we
1108    create an artificial stmt which looks like a load from the store, this can
1109    be used to eliminate redundant loads.  OLD_OPS are the operands from the 
1110    store stmt, and NEW_STMT is the new load which represents a load of the
1111    values stored.  */
1112
1113 void
1114 create_ssa_artficial_load_stmt (tree new_stmt, tree old_stmt)
1115 {
1116   stmt_ann_t ann;
1117   tree op;
1118   ssa_op_iter iter;
1119   use_operand_p use_p;
1120   unsigned x;
1121
1122   ann = get_stmt_ann (new_stmt);
1123
1124   /* process the stmt looking for operands.  */
1125   start_ssa_stmt_operands ();
1126   parse_ssa_operands (new_stmt);
1127
1128   for (x = 0; x < opbuild_num_elems (&build_vuses); x++)
1129     {
1130       tree t = opbuild_elem_virtual (&build_vuses, x);
1131       if (TREE_CODE (t) != SSA_NAME)
1132         {
1133           var_ann_t ann = var_ann (t);
1134           ann->in_vuse_list = 0;
1135         }
1136     }
1137    
1138   for (x = 0; x < opbuild_num_elems (&build_v_may_defs); x++)
1139     {
1140       tree t = opbuild_elem_virtual (&build_v_may_defs, x);
1141       if (TREE_CODE (t) != SSA_NAME)
1142         {
1143           var_ann_t ann = var_ann (t);
1144           ann->in_v_may_def_list = 0;
1145         }
1146     }
1147   /* Remove any virtual operands that were found.  */
1148   opbuild_clear (&build_v_may_defs);
1149   opbuild_clear (&build_v_must_defs);
1150   opbuild_clear (&build_vuses);
1151
1152   /* For each VDEF on the original statement, we want to create a
1153      VUSE of the V_MAY_DEF result or V_MUST_DEF op on the new 
1154      statement.  */
1155   FOR_EACH_SSA_TREE_OPERAND (op, old_stmt, iter, 
1156                              (SSA_OP_VMAYDEF | SSA_OP_VMUSTDEF))
1157     append_vuse (op);
1158     
1159   /* Now build the operands for this new stmt.  */
1160   finalize_ssa_stmt_operands (new_stmt);
1161
1162   /* All uses in this fake stmt must not be in the immediate use lists.  */
1163   FOR_EACH_SSA_USE_OPERAND (use_p, new_stmt, iter, SSA_OP_ALL_USES)
1164     delink_imm_use (use_p);
1165 }
1166
1167 static void
1168 swap_tree_operands (tree stmt, tree *exp0, tree *exp1)
1169 {
1170   tree op0, op1;
1171   op0 = *exp0;
1172   op1 = *exp1;
1173
1174   /* If the operand cache is active, attempt to preserve the relative positions
1175      of these two operands in their respective immediate use lists.  */
1176   if (ssa_operands_active () && op0 != op1)
1177     {
1178       use_optype_p use0, use1, ptr;
1179       use0 = use1 = NULL;
1180       /* Find the 2 operands in the cache, if they are there.  */
1181       for (ptr = USE_OPS (stmt); ptr; ptr = ptr->next)
1182         if (USE_OP_PTR (ptr)->use == exp0)
1183           {
1184             use0 = ptr;
1185             break;
1186           }
1187       for (ptr = USE_OPS (stmt); ptr; ptr = ptr->next)
1188         if (USE_OP_PTR (ptr)->use == exp1)
1189           {
1190             use1 = ptr;
1191             break;
1192           }
1193       /* If both uses don't have operand entries, there isn't much we can do
1194          at this point.  Presumably we dont need to worry about it.  */
1195       if (use0 && use1)
1196         {
1197           tree *tmp = USE_OP_PTR (use1)->use;
1198           USE_OP_PTR (use1)->use = USE_OP_PTR (use0)->use;
1199           USE_OP_PTR (use0)->use = tmp;
1200         }
1201     }
1202
1203   /* Now swap the data.  */
1204   *exp0 = op1;
1205   *exp1 = op0;
1206 }
1207
1208
1209 /* Recursively scan the expression pointed by EXPR_P in statement referred to
1210    by INFO.  FLAGS is one of the OPF_* constants modifying how to interpret the
1211    operands found.  */
1212
1213 static void
1214 get_expr_operands (tree stmt, tree *expr_p, int flags)
1215 {
1216   enum tree_code code;
1217   enum tree_code_class class;
1218   tree expr = *expr_p;
1219   stmt_ann_t s_ann = stmt_ann (stmt);
1220
1221   if (expr == NULL)
1222     return;
1223
1224   code = TREE_CODE (expr);
1225   class = TREE_CODE_CLASS (code);
1226
1227   switch (code)
1228     {
1229     case ADDR_EXPR:
1230       /* We could have the address of a component, array member,
1231          etc which has interesting variable references.  */
1232       /* Taking the address of a variable does not represent a
1233          reference to it, but the fact that the stmt takes its address will be
1234          of interest to some passes (e.g. alias resolution).  */
1235       add_stmt_operand (expr_p, s_ann, 0);
1236
1237       /* If the address is invariant, there may be no interesting variable
1238          references inside.  */
1239       if (is_gimple_min_invariant (expr))
1240         return;
1241
1242       /* There should be no VUSEs created, since the referenced objects are
1243          not really accessed.  The only operands that we should find here
1244          are ARRAY_REF indices which will always be real operands (GIMPLE
1245          does not allow non-registers as array indices).  */
1246       flags |= opf_no_vops;
1247
1248       get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1249       return;
1250
1251     case SSA_NAME:
1252     case VAR_DECL:
1253     case PARM_DECL:
1254     case RESULT_DECL:
1255     case CONST_DECL:
1256       {
1257         subvar_t svars;
1258         
1259         /* Add the subvars for a variable if it has subvars, to DEFS or USES.
1260            Otherwise, add the variable itself.  
1261            Whether it goes to USES or DEFS depends on the operand flags.  */
1262         if (var_can_have_subvars (expr)
1263             && (svars = get_subvars_for_var (expr)))
1264           {
1265             subvar_t sv;
1266             for (sv = svars; sv; sv = sv->next)
1267               add_stmt_operand (&sv->var, s_ann, flags);
1268           }
1269         else
1270           {
1271             add_stmt_operand (expr_p, s_ann, flags);
1272           }
1273         return;
1274       }
1275     case MISALIGNED_INDIRECT_REF:
1276       get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1277       /* fall through */
1278
1279     case ALIGN_INDIRECT_REF:
1280     case INDIRECT_REF:
1281       get_indirect_ref_operands (stmt, expr, flags);
1282       return;
1283
1284     case TARGET_MEM_REF:
1285       get_tmr_operands (stmt, expr, flags);
1286       return;
1287
1288     case ARRAY_REF:
1289     case ARRAY_RANGE_REF:
1290       /* Treat array references as references to the virtual variable
1291          representing the array.  The virtual variable for an ARRAY_REF
1292          is the VAR_DECL for the array.  */
1293
1294       /* Add the virtual variable for the ARRAY_REF to VDEFS or VUSES
1295          according to the value of IS_DEF.  Recurse if the LHS of the
1296          ARRAY_REF node is not a regular variable.  */
1297       if (SSA_VAR_P (TREE_OPERAND (expr, 0)))
1298         add_stmt_operand (expr_p, s_ann, flags);
1299       else
1300         get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1301
1302       get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1303       get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1304       get_expr_operands (stmt, &TREE_OPERAND (expr, 3), opf_none);
1305       return;
1306
1307     case COMPONENT_REF:
1308     case REALPART_EXPR:
1309     case IMAGPART_EXPR:
1310       {
1311         tree ref;
1312         unsigned HOST_WIDE_INT offset, size;
1313         /* This component ref becomes an access to all of the subvariables
1314            it can touch,  if we can determine that, but *NOT* the real one.
1315            If we can't determine which fields we could touch, the recursion
1316            will eventually get to a variable and add *all* of its subvars, or
1317            whatever is the minimum correct subset.  */
1318
1319         ref = okay_component_ref_for_subvars (expr, &offset, &size);
1320         if (ref)
1321           {       
1322             subvar_t svars = get_subvars_for_var (ref);
1323             subvar_t sv;
1324             for (sv = svars; sv; sv = sv->next)
1325               {
1326                 bool exact;             
1327                 if (overlap_subvar (offset, size, sv, &exact))
1328                   {
1329                     bool subvar_flags = flags;
1330                     if (!exact)
1331                       subvar_flags &= ~opf_kill_def;
1332                     add_stmt_operand (&sv->var, s_ann, subvar_flags);
1333                   }
1334               }
1335           }
1336         else
1337           get_expr_operands (stmt, &TREE_OPERAND (expr, 0), 
1338                              flags & ~opf_kill_def);
1339         
1340         if (code == COMPONENT_REF)
1341           {
1342             if (s_ann && TREE_THIS_VOLATILE (TREE_OPERAND (expr, 1)))
1343               s_ann->has_volatile_ops = true; 
1344             get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1345           }
1346         return;
1347       }
1348     case WITH_SIZE_EXPR:
1349       /* WITH_SIZE_EXPR is a pass-through reference to its first argument,
1350          and an rvalue reference to its second argument.  */
1351       get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1352       get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1353       return;
1354
1355     case CALL_EXPR:
1356       get_call_expr_operands (stmt, expr);
1357       return;
1358
1359     case COND_EXPR:
1360     case VEC_COND_EXPR:
1361       get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_none);
1362       get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1363       get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1364       return;
1365
1366     case MODIFY_EXPR:
1367       {
1368         int subflags;
1369         tree op;
1370
1371         get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1372
1373         op = TREE_OPERAND (expr, 0);
1374         if (TREE_CODE (op) == WITH_SIZE_EXPR)
1375           op = TREE_OPERAND (expr, 0);
1376         if (TREE_CODE (op) == ARRAY_REF
1377             || TREE_CODE (op) == ARRAY_RANGE_REF
1378             || TREE_CODE (op) == REALPART_EXPR
1379             || TREE_CODE (op) == IMAGPART_EXPR)
1380           subflags = opf_is_def;
1381         else
1382           subflags = opf_is_def | opf_kill_def;
1383
1384         get_expr_operands (stmt, &TREE_OPERAND (expr, 0), subflags);
1385         return;
1386       }
1387
1388     case CONSTRUCTOR:
1389       {
1390         /* General aggregate CONSTRUCTORs have been decomposed, but they
1391            are still in use as the COMPLEX_EXPR equivalent for vectors.  */
1392
1393         tree t;
1394         for (t = TREE_OPERAND (expr, 0); t ; t = TREE_CHAIN (t))
1395           get_expr_operands (stmt, &TREE_VALUE (t), opf_none);
1396
1397         return;
1398       }
1399
1400     case TRUTH_NOT_EXPR:
1401     case BIT_FIELD_REF:
1402     case VIEW_CONVERT_EXPR:
1403     do_unary:
1404       get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1405       return;
1406
1407     case TRUTH_AND_EXPR:
1408     case TRUTH_OR_EXPR:
1409     case TRUTH_XOR_EXPR:
1410     case COMPOUND_EXPR:
1411     case OBJ_TYPE_REF:
1412     case ASSERT_EXPR:
1413     do_binary:
1414       {
1415         tree op0 = TREE_OPERAND (expr, 0);
1416         tree op1 = TREE_OPERAND (expr, 1);
1417
1418         /* If it would be profitable to swap the operands, then do so to
1419            canonicalize the statement, enabling better optimization.
1420
1421            By placing canonicalization of such expressions here we
1422            transparently keep statements in canonical form, even
1423            when the statement is modified.  */
1424         if (tree_swap_operands_p (op0, op1, false))
1425           {
1426             /* For relationals we need to swap the operands
1427                and change the code.  */
1428             if (code == LT_EXPR
1429                 || code == GT_EXPR
1430                 || code == LE_EXPR
1431                 || code == GE_EXPR)
1432               {
1433                 TREE_SET_CODE (expr, swap_tree_comparison (code));
1434                 swap_tree_operands (stmt,
1435                                     &TREE_OPERAND (expr, 0),                    
1436                                     &TREE_OPERAND (expr, 1));
1437               }
1438           
1439             /* For a commutative operator we can just swap the operands.  */
1440             else if (commutative_tree_code (code))
1441               {
1442                 swap_tree_operands (stmt,
1443                                     &TREE_OPERAND (expr, 0),                    
1444                                     &TREE_OPERAND (expr, 1));
1445               }
1446           }
1447
1448         get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1449         get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1450         return;
1451       }
1452
1453     case REALIGN_LOAD_EXPR:
1454       {
1455         get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1456         get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1457         get_expr_operands (stmt, &TREE_OPERAND (expr, 2), flags);
1458         return;
1459       }
1460
1461     case BLOCK:
1462     case FUNCTION_DECL:
1463     case EXC_PTR_EXPR:
1464     case FILTER_EXPR:
1465     case LABEL_DECL:
1466       /* Expressions that make no memory references.  */
1467       return;
1468
1469     default:
1470       if (class == tcc_unary)
1471         goto do_unary;
1472       if (class == tcc_binary || class == tcc_comparison)
1473         goto do_binary;
1474       if (class == tcc_constant || class == tcc_type)
1475         return;
1476     }
1477
1478   /* If we get here, something has gone wrong.  */
1479 #ifdef ENABLE_CHECKING
1480   fprintf (stderr, "unhandled expression in get_expr_operands():\n");
1481   debug_tree (expr);
1482   fputs ("\n", stderr);
1483   internal_error ("internal error");
1484 #endif
1485   gcc_unreachable ();
1486 }
1487
1488
1489 /* Scan operands in the ASM_EXPR stmt referred to in INFO.  */
1490
1491 static void
1492 get_asm_expr_operands (tree stmt)
1493 {
1494   stmt_ann_t s_ann = stmt_ann (stmt);
1495   int noutputs = list_length (ASM_OUTPUTS (stmt));
1496   const char **oconstraints
1497     = (const char **) alloca ((noutputs) * sizeof (const char *));
1498   int i;
1499   tree link;
1500   const char *constraint;
1501   bool allows_mem, allows_reg, is_inout;
1502
1503   for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
1504     {
1505       oconstraints[i] = constraint
1506         = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1507       parse_output_constraint (&constraint, i, 0, 0,
1508           &allows_mem, &allows_reg, &is_inout);
1509
1510       /* This should have been split in gimplify_asm_expr.  */
1511       gcc_assert (!allows_reg || !is_inout);
1512
1513       /* Memory operands are addressable.  Note that STMT needs the
1514          address of this operand.  */
1515       if (!allows_reg && allows_mem)
1516         {
1517           tree t = get_base_address (TREE_VALUE (link));
1518           if (t && DECL_P (t) && s_ann)
1519             add_to_addressable_set (t, &s_ann->addresses_taken);
1520         }
1521
1522       get_expr_operands (stmt, &TREE_VALUE (link), opf_is_def);
1523     }
1524
1525   for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
1526     {
1527       constraint
1528         = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1529       parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1530           oconstraints, &allows_mem, &allows_reg);
1531
1532       /* Memory operands are addressable.  Note that STMT needs the
1533          address of this operand.  */
1534       if (!allows_reg && allows_mem)
1535         {
1536           tree t = get_base_address (TREE_VALUE (link));
1537           if (t && DECL_P (t) && s_ann)
1538             add_to_addressable_set (t, &s_ann->addresses_taken);
1539         }
1540
1541       get_expr_operands (stmt, &TREE_VALUE (link), 0);
1542     }
1543
1544
1545   /* Clobber memory for asm ("" : : : "memory");  */
1546   for (link = ASM_CLOBBERS (stmt); link; link = TREE_CHAIN (link))
1547     if (strcmp (TREE_STRING_POINTER (TREE_VALUE (link)), "memory") == 0)
1548       {
1549         unsigned i;
1550         bitmap_iterator bi;
1551
1552         /* Clobber all call-clobbered variables (or .GLOBAL_VAR if we
1553            decided to group them).  */
1554         if (global_var)
1555           add_stmt_operand (&global_var, s_ann, opf_is_def);
1556         else
1557           EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1558             {
1559               tree var = referenced_var (i);
1560               add_stmt_operand (&var, s_ann, opf_is_def | opf_non_specific);
1561             }
1562
1563         /* Now clobber all addressables.  */
1564         EXECUTE_IF_SET_IN_BITMAP (addressable_vars, 0, i, bi)
1565             {
1566               tree var = referenced_var (i);
1567
1568               /* Subvars are explicitly represented in this list, so
1569                  we don't need the original to be added to the clobber
1570                  ops, but the original *will* be in this list because 
1571                  we keep the addressability of the original
1572                  variable up-to-date so we don't screw up the rest of
1573                  the backend.  */
1574               if (var_can_have_subvars (var)
1575                   && get_subvars_for_var (var) != NULL)
1576                 continue;               
1577
1578               add_stmt_operand (&var, s_ann, opf_is_def | opf_non_specific);
1579             }
1580
1581         break;
1582       }
1583 }
1584
1585 /* A subroutine of get_expr_operands to handle INDIRECT_REF,
1586    ALIGN_INDIRECT_REF and MISALIGNED_INDIRECT_REF.  */
1587
1588 static void
1589 get_indirect_ref_operands (tree stmt, tree expr, int flags)
1590 {
1591   tree *pptr = &TREE_OPERAND (expr, 0);
1592   tree ptr = *pptr;
1593   stmt_ann_t s_ann = stmt_ann (stmt);
1594
1595   /* Stores into INDIRECT_REF operands are never killing definitions.  */
1596   flags &= ~opf_kill_def;
1597
1598   if (SSA_VAR_P (ptr))
1599     {
1600       struct ptr_info_def *pi = NULL;
1601
1602       /* If PTR has flow-sensitive points-to information, use it.  */
1603       if (TREE_CODE (ptr) == SSA_NAME
1604           && (pi = SSA_NAME_PTR_INFO (ptr)) != NULL
1605           && pi->name_mem_tag)
1606         {
1607           /* PTR has its own memory tag.  Use it.  */
1608           add_stmt_operand (&pi->name_mem_tag, s_ann, flags);
1609         }
1610       else
1611         {
1612           /* If PTR is not an SSA_NAME or it doesn't have a name
1613              tag, use its type memory tag.  */
1614           var_ann_t v_ann;
1615
1616           /* If we are emitting debugging dumps, display a warning if
1617              PTR is an SSA_NAME with no flow-sensitive alias
1618              information.  That means that we may need to compute
1619              aliasing again.  */
1620           if (dump_file
1621               && TREE_CODE (ptr) == SSA_NAME
1622               && pi == NULL)
1623             {
1624               fprintf (dump_file,
1625                   "NOTE: no flow-sensitive alias info for ");
1626               print_generic_expr (dump_file, ptr, dump_flags);
1627               fprintf (dump_file, " in ");
1628               print_generic_stmt (dump_file, stmt, dump_flags);
1629             }
1630
1631           if (TREE_CODE (ptr) == SSA_NAME)
1632             ptr = SSA_NAME_VAR (ptr);
1633           v_ann = var_ann (ptr);
1634           if (v_ann->type_mem_tag)
1635             add_stmt_operand (&v_ann->type_mem_tag, s_ann, flags);
1636         }
1637     }
1638
1639   /* If a constant is used as a pointer, we can't generate a real
1640      operand for it but we mark the statement volatile to prevent
1641      optimizations from messing things up.  */
1642   else if (TREE_CODE (ptr) == INTEGER_CST)
1643     {
1644       if (s_ann)
1645         s_ann->has_volatile_ops = true;
1646       return;
1647     }
1648
1649   /* Everything else *should* have been folded elsewhere, but users
1650      are smarter than we in finding ways to write invalid code.  We
1651      cannot just assert here.  If we were absolutely certain that we
1652      do handle all valid cases, then we could just do nothing here.
1653      That seems optimistic, so attempt to do something logical... */
1654   else if ((TREE_CODE (ptr) == PLUS_EXPR || TREE_CODE (ptr) == MINUS_EXPR)
1655            && TREE_CODE (TREE_OPERAND (ptr, 0)) == ADDR_EXPR
1656            && TREE_CODE (TREE_OPERAND (ptr, 1)) == INTEGER_CST)
1657     {
1658       /* Make sure we know the object is addressable.  */
1659       pptr = &TREE_OPERAND (ptr, 0);
1660       add_stmt_operand (pptr, s_ann, 0);
1661
1662       /* Mark the object itself with a VUSE.  */
1663       pptr = &TREE_OPERAND (*pptr, 0);
1664       get_expr_operands (stmt, pptr, flags);
1665       return;
1666     }
1667
1668   /* Ok, this isn't even is_gimple_min_invariant.  Something's broke.  */
1669   else
1670     gcc_unreachable ();
1671
1672   /* Add a USE operand for the base pointer.  */
1673   get_expr_operands (stmt, pptr, opf_none);
1674 }
1675
1676 /* A subroutine of get_expr_operands to handle TARGET_MEM_REF.  */
1677
1678 static void
1679 get_tmr_operands (tree stmt, tree expr, int flags)
1680 {
1681   tree tag = TMR_TAG (expr);
1682
1683   /* First record the real operands.  */
1684   get_expr_operands (stmt, &TMR_BASE (expr), opf_none);
1685   get_expr_operands (stmt, &TMR_INDEX (expr), opf_none);
1686
1687   /* MEM_REFs should never be killing.  */
1688   flags &= ~opf_kill_def;
1689
1690   if (TMR_SYMBOL (expr))
1691     {
1692       stmt_ann_t ann = stmt_ann (stmt);
1693       add_to_addressable_set (TMR_SYMBOL (expr), &ann->addresses_taken);
1694     }
1695
1696   if (tag)
1697     add_stmt_operand (&tag, stmt_ann (stmt), flags);
1698   else
1699     /* Something weird, so ensure that we will be careful.  */
1700     stmt_ann (stmt)->has_volatile_ops = true;
1701 }
1702
1703 /* A subroutine of get_expr_operands to handle CALL_EXPR.  */
1704
1705 static void
1706 get_call_expr_operands (tree stmt, tree expr)
1707 {
1708   tree op;
1709   int call_flags = call_expr_flags (expr);
1710
1711   /* If aliases have been computed already, add V_MAY_DEF or V_USE
1712      operands for all the symbols that have been found to be
1713      call-clobbered.
1714      
1715      Note that if aliases have not been computed, the global effects
1716      of calls will not be included in the SSA web. This is fine
1717      because no optimizer should run before aliases have been
1718      computed.  By not bothering with virtual operands for CALL_EXPRs
1719      we avoid adding superfluous virtual operands, which can be a
1720      significant compile time sink (See PR 15855).  */
1721   if (aliases_computed_p
1722       && !bitmap_empty_p (call_clobbered_vars)
1723       && !(call_flags & ECF_NOVOPS))
1724     {
1725       /* A 'pure' or a 'const' function never call-clobbers anything. 
1726          A 'noreturn' function might, but since we don't return anyway 
1727          there is no point in recording that.  */ 
1728       if (TREE_SIDE_EFFECTS (expr)
1729           && !(call_flags & (ECF_PURE | ECF_CONST | ECF_NORETURN)))
1730         add_call_clobber_ops (stmt);
1731       else if (!(call_flags & ECF_CONST))
1732         add_call_read_ops (stmt);
1733     }
1734
1735   /* Find uses in the called function.  */
1736   get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_none);
1737
1738   for (op = TREE_OPERAND (expr, 1); op; op = TREE_CHAIN (op))
1739     get_expr_operands (stmt, &TREE_VALUE (op), opf_none);
1740
1741   get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1742
1743 }
1744
1745
1746 /* Add *VAR_P to the appropriate operand array for INFO.  FLAGS is as in
1747    get_expr_operands.  If *VAR_P is a GIMPLE register, it will be added to
1748    the statement's real operands, otherwise it is added to virtual
1749    operands.  */
1750
1751 static void
1752 add_stmt_operand (tree *var_p, stmt_ann_t s_ann, int flags)
1753 {
1754   bool is_real_op;
1755   tree var, sym;
1756   var_ann_t v_ann;
1757
1758   var = *var_p;
1759   STRIP_NOPS (var);
1760
1761   /* If the operand is an ADDR_EXPR, add its operand to the list of
1762      variables that have had their address taken in this statement.  */
1763   if (TREE_CODE (var) == ADDR_EXPR && s_ann)
1764     {
1765       add_to_addressable_set (TREE_OPERAND (var, 0), &s_ann->addresses_taken);
1766       return;
1767     }
1768
1769   /* If the original variable is not a scalar, it will be added to the list
1770      of virtual operands.  In that case, use its base symbol as the virtual
1771      variable representing it.  */
1772   is_real_op = is_gimple_reg (var);
1773   if (!is_real_op && !DECL_P (var))
1774     var = get_virtual_var (var);
1775
1776   /* If VAR is not a variable that we care to optimize, do nothing.  */
1777   if (var == NULL_TREE || !SSA_VAR_P (var))
1778     return;
1779
1780   sym = (TREE_CODE (var) == SSA_NAME ? SSA_NAME_VAR (var) : var);
1781   v_ann = var_ann (sym);
1782
1783   /* Mark statements with volatile operands.  Optimizers should back
1784      off from statements having volatile operands.  */
1785   if (TREE_THIS_VOLATILE (sym) && s_ann)
1786     s_ann->has_volatile_ops = true;
1787
1788   /* If the variable cannot be modified and this is a V_MAY_DEF change
1789      it into a VUSE.  This happens when read-only variables are marked
1790      call-clobbered and/or aliased to writable variables.  So we only
1791      check that this only happens on non-specific stores.
1792
1793      Note that if this is a specific store, i.e. associated with a
1794      modify_expr, then we can't suppress the V_DEF, lest we run into
1795      validation problems.
1796
1797      This can happen when programs cast away const, leaving us with a
1798      store to read-only memory.  If the statement is actually executed
1799      at runtime, then the program is ill formed.  If the statement is
1800      not executed then all is well.  At the very least, we cannot ICE.  */
1801   if ((flags & opf_non_specific) && unmodifiable_var_p (var))
1802     {
1803       gcc_assert (!is_real_op);
1804       flags &= ~(opf_is_def | opf_kill_def);
1805     }
1806
1807   if (is_real_op)
1808     {
1809       /* The variable is a GIMPLE register.  Add it to real operands.  */
1810       if (flags & opf_is_def)
1811         append_def (var_p);
1812       else
1813         append_use (var_p);
1814     }
1815   else
1816     {
1817       varray_type aliases;
1818
1819       /* The variable is not a GIMPLE register.  Add it (or its aliases) to
1820          virtual operands, unless the caller has specifically requested
1821          not to add virtual operands (used when adding operands inside an
1822          ADDR_EXPR expression).  */
1823       if (flags & opf_no_vops)
1824         return;
1825
1826       aliases = v_ann->may_aliases;
1827
1828       if (aliases == NULL)
1829         {
1830           /* The variable is not aliased or it is an alias tag.  */
1831           if (flags & opf_is_def)
1832             {
1833               if (flags & opf_kill_def)
1834                 {
1835                   /* Only regular variables or struct fields may get a
1836                      V_MUST_DEF operand.  */
1837                   gcc_assert (v_ann->mem_tag_kind == NOT_A_TAG 
1838                               || v_ann->mem_tag_kind == STRUCT_FIELD);
1839                   /* V_MUST_DEF for non-aliased, non-GIMPLE register 
1840                     variable definitions.  */
1841                   append_v_must_def (var);
1842                 }
1843               else
1844                 {
1845                   /* Add a V_MAY_DEF for call-clobbered variables and
1846                      memory tags.  */
1847                   append_v_may_def (var);
1848                 }
1849             }
1850           else
1851             {
1852               append_vuse (var);
1853               if (s_ann && v_ann->is_alias_tag)
1854                 s_ann->makes_aliased_loads = 1;
1855             }
1856         }
1857       else
1858         {
1859           size_t i;
1860
1861           /* The variable is aliased.  Add its aliases to the virtual
1862              operands.  */
1863           gcc_assert (VARRAY_ACTIVE_SIZE (aliases) != 0);
1864
1865           if (flags & opf_is_def)
1866             {
1867               /* If the variable is also an alias tag, add a virtual
1868                  operand for it, otherwise we will miss representing
1869                  references to the members of the variable's alias set.
1870                  This fixes the bug in gcc.c-torture/execute/20020503-1.c.  */
1871               if (v_ann->is_alias_tag)
1872                 append_v_may_def (var);
1873
1874               for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
1875                 append_v_may_def (VARRAY_TREE (aliases, i));
1876
1877               if (s_ann)
1878                 s_ann->makes_aliased_stores = 1;
1879             }
1880           else
1881             {
1882               /* Similarly, append a virtual uses for VAR itself, when
1883                  it is an alias tag.  */
1884               if (v_ann->is_alias_tag)
1885                 append_vuse (var);
1886
1887               for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
1888                 append_vuse (VARRAY_TREE (aliases, i));
1889
1890               if (s_ann)
1891                 s_ann->makes_aliased_loads = 1;
1892             }
1893         }
1894     }
1895 }
1896
1897   
1898 /* Add the base address of REF to the set *ADDRESSES_TAKEN.  If
1899    *ADDRESSES_TAKEN is NULL, a new set is created.  REF may be
1900    a single variable whose address has been taken or any other valid
1901    GIMPLE memory reference (structure reference, array, etc).  If the
1902    base address of REF is a decl that has sub-variables, also add all
1903    of its sub-variables.  */
1904
1905 void
1906 add_to_addressable_set (tree ref, bitmap *addresses_taken)
1907 {
1908   tree var;
1909   subvar_t svars;
1910
1911   gcc_assert (addresses_taken);
1912
1913   /* Note that it is *NOT OKAY* to use the target of a COMPONENT_REF
1914      as the only thing we take the address of.  If VAR is a structure,
1915      taking the address of a field means that the whole structure may
1916      be referenced using pointer arithmetic.  See PR 21407 and the
1917      ensuing mailing list discussion.  */
1918   var = get_base_address (ref);
1919   if (var && SSA_VAR_P (var))
1920     {
1921       if (*addresses_taken == NULL)
1922         *addresses_taken = BITMAP_GGC_ALLOC ();      
1923       
1924       if (var_can_have_subvars (var)
1925           && (svars = get_subvars_for_var (var)))
1926         {
1927           subvar_t sv;
1928           for (sv = svars; sv; sv = sv->next)
1929             {
1930               bitmap_set_bit (*addresses_taken, DECL_UID (sv->var));
1931               TREE_ADDRESSABLE (sv->var) = 1;
1932             }
1933         }
1934       else
1935         {
1936           bitmap_set_bit (*addresses_taken, DECL_UID (var));
1937           TREE_ADDRESSABLE (var) = 1;
1938         }
1939     }
1940 }
1941
1942
1943 /* Add clobbering definitions for .GLOBAL_VAR or for each of the call
1944    clobbered variables in the function.  */
1945
1946 static void
1947 add_call_clobber_ops (tree stmt)
1948 {
1949   int i;
1950   unsigned u;
1951   tree t;
1952   bitmap_iterator bi;
1953   stmt_ann_t s_ann = stmt_ann (stmt);
1954   struct stmt_ann_d empty_ann;
1955
1956   /* Functions that are not const, pure or never return may clobber
1957      call-clobbered variables.  */
1958   if (s_ann)
1959     s_ann->makes_clobbering_call = true;
1960
1961   /* If we created .GLOBAL_VAR earlier, just use it.  See compute_may_aliases 
1962      for the heuristic used to decide whether to create .GLOBAL_VAR or not.  */
1963   if (global_var)
1964     {
1965       add_stmt_operand (&global_var, s_ann, opf_is_def);
1966       return;
1967     }
1968
1969   /* If cache is valid, copy the elements into the build vectors.  */
1970   if (ssa_call_clobbered_cache_valid)
1971     {
1972       /* Process the caches in reverse order so we are always inserting at
1973          the head of the list.  */
1974       for (i = VEC_length (tree, clobbered_vuses) - 1; i >=0; i--)
1975         {
1976           t = VEC_index (tree, clobbered_vuses, i);
1977           gcc_assert (TREE_CODE (t) != SSA_NAME);
1978           var_ann (t)->in_vuse_list = 1;
1979           opbuild_append_virtual (&build_vuses, t);
1980         }
1981       for (i = VEC_length (tree, clobbered_v_may_defs) - 1; i >= 0; i--)
1982         {
1983           t = VEC_index (tree, clobbered_v_may_defs, i);
1984           gcc_assert (TREE_CODE (t) != SSA_NAME);
1985           var_ann (t)->in_v_may_def_list = 1;
1986           opbuild_append_virtual (&build_v_may_defs, t);
1987         }
1988       if (s_ann)
1989         {
1990           s_ann->makes_aliased_loads = clobbered_aliased_loads;
1991           s_ann->makes_aliased_stores = clobbered_aliased_stores;
1992         }
1993       return;
1994     }
1995
1996   memset (&empty_ann, 0, sizeof (struct stmt_ann_d));
1997
1998   /* Add a V_MAY_DEF operand for every call clobbered variable.  */
1999   EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, u, bi)
2000     {
2001       tree var = referenced_var (u);
2002       if (unmodifiable_var_p (var))
2003         add_stmt_operand (&var, &empty_ann, opf_none);
2004       else
2005         add_stmt_operand (&var, &empty_ann, opf_is_def | opf_non_specific);
2006     }
2007
2008   clobbered_aliased_loads = empty_ann.makes_aliased_loads;
2009   clobbered_aliased_stores = empty_ann.makes_aliased_stores;
2010
2011   /* Set the flags for a stmt's annotation.  */
2012   if (s_ann)
2013     {
2014       s_ann->makes_aliased_loads = empty_ann.makes_aliased_loads;
2015       s_ann->makes_aliased_stores = empty_ann.makes_aliased_stores;
2016     }
2017
2018   /* Prepare empty cache vectors.  */
2019   VEC_truncate (tree, clobbered_vuses, 0);
2020   VEC_truncate (tree, clobbered_v_may_defs, 0);
2021
2022   /* Now fill the clobbered cache with the values that have been found.  */
2023   for (i = opbuild_first (&build_vuses);
2024        i != OPBUILD_LAST;
2025        i = opbuild_next (&build_vuses, i))
2026     VEC_safe_push (tree, heap, clobbered_vuses,
2027                    opbuild_elem_virtual (&build_vuses, i));
2028
2029   gcc_assert (opbuild_num_elems (&build_vuses) 
2030               == VEC_length (tree, clobbered_vuses));
2031
2032   for (i = opbuild_first (&build_v_may_defs);
2033        i != OPBUILD_LAST;
2034        i = opbuild_next (&build_v_may_defs, i))
2035     VEC_safe_push (tree, heap, clobbered_v_may_defs, 
2036                    opbuild_elem_virtual (&build_v_may_defs, i));
2037
2038   gcc_assert (opbuild_num_elems (&build_v_may_defs) 
2039               == VEC_length (tree, clobbered_v_may_defs));
2040
2041   ssa_call_clobbered_cache_valid = true;
2042 }
2043
2044
2045 /* Add VUSE operands for .GLOBAL_VAR or all call clobbered variables in the
2046    function.  */
2047
2048 static void
2049 add_call_read_ops (tree stmt)
2050 {
2051   int i;
2052   unsigned u;
2053   tree t;
2054   bitmap_iterator bi;
2055   stmt_ann_t s_ann = stmt_ann (stmt);
2056   struct stmt_ann_d empty_ann;
2057
2058   /* if the function is not pure, it may reference memory.  Add
2059      a VUSE for .GLOBAL_VAR if it has been created.  See add_referenced_var
2060      for the heuristic used to decide whether to create .GLOBAL_VAR.  */
2061   if (global_var)
2062     {
2063       add_stmt_operand (&global_var, s_ann, opf_none);
2064       return;
2065     }
2066   
2067   /* If cache is valid, copy the elements into the build vector.  */
2068   if (ssa_ro_call_cache_valid)
2069     {
2070       for (i = VEC_length (tree, ro_call_vuses) - 1; i >=0 ; i--)
2071         {
2072           /* Process the caches in reverse order so we are always inserting at
2073              the head of the list.  */
2074           t = VEC_index (tree, ro_call_vuses, i);
2075           gcc_assert (TREE_CODE (t) != SSA_NAME);
2076           var_ann (t)->in_vuse_list = 1;
2077           opbuild_append_virtual (&build_vuses, t);
2078         }
2079       if (s_ann)
2080         s_ann->makes_aliased_loads = ro_call_aliased_loads;
2081       return;
2082     }
2083
2084   memset (&empty_ann, 0, sizeof (struct stmt_ann_d));
2085
2086   /* Add a VUSE for each call-clobbered variable.  */
2087   EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, u, bi)
2088     {
2089       tree var = referenced_var (u);
2090       add_stmt_operand (&var, &empty_ann, opf_none | opf_non_specific);
2091     }
2092
2093   ro_call_aliased_loads = empty_ann.makes_aliased_loads;
2094   if (s_ann)
2095     s_ann->makes_aliased_loads = empty_ann.makes_aliased_loads;
2096
2097   /* Prepare empty cache vectors.  */
2098   VEC_truncate (tree, ro_call_vuses, 0);
2099
2100   /* Now fill the clobbered cache with the values that have been found.  */
2101   for (i = opbuild_first (&build_vuses);
2102        i != OPBUILD_LAST;
2103        i = opbuild_next (&build_vuses, i))
2104     VEC_safe_push (tree, heap, ro_call_vuses,
2105                    opbuild_elem_virtual (&build_vuses, i));
2106
2107   gcc_assert (opbuild_num_elems (&build_vuses) 
2108               == VEC_length (tree, ro_call_vuses));
2109
2110   ssa_ro_call_cache_valid = true;
2111 }
2112
2113
2114 /* Scan the immediate_use list for VAR making sure its linked properly.
2115    return RTUE iof there is a problem.  */
2116
2117 bool
2118 verify_imm_links (FILE *f, tree var)
2119 {
2120   use_operand_p ptr, prev, list;
2121   int count;
2122
2123   gcc_assert (TREE_CODE (var) == SSA_NAME);
2124
2125   list = &(SSA_NAME_IMM_USE_NODE (var));
2126   gcc_assert (list->use == NULL);
2127
2128   if (list->prev == NULL)
2129     {
2130       gcc_assert (list->next == NULL);
2131       return false;
2132     }
2133
2134   prev = list;
2135   count = 0;
2136   for (ptr = list->next; ptr != list; )
2137     {
2138       if (prev != ptr->prev)
2139         goto error;
2140       
2141       if (ptr->use == NULL)
2142         goto error; /* 2 roots, or SAFE guard node.  */
2143       else if (*(ptr->use) != var)
2144         goto error;
2145
2146       prev = ptr;
2147       ptr = ptr->next;
2148       /* Avoid infinite loops.  */
2149       if (count++ > 30000)
2150         goto error;
2151     }
2152
2153   /* Verify list in the other direction.  */
2154   prev = list;
2155   for (ptr = list->prev; ptr != list; )
2156     {
2157       if (prev != ptr->next)
2158         goto error;
2159       prev = ptr;
2160       ptr = ptr->prev;
2161       if (count-- < 0)
2162         goto error;
2163     }
2164
2165   if (count != 0)
2166     goto error;
2167
2168   return false;
2169
2170  error:
2171   if (ptr->stmt && stmt_modified_p (ptr->stmt))
2172     {
2173       fprintf (f, " STMT MODIFIED. - <%p> ", (void *)ptr->stmt);
2174       print_generic_stmt (f, ptr->stmt, TDF_SLIM);
2175     }
2176   fprintf (f, " IMM ERROR : (use_p : tree - %p:%p)", (void *)ptr, 
2177            (void *)ptr->use);
2178   print_generic_expr (f, USE_FROM_PTR (ptr), TDF_SLIM);
2179   fprintf(f, "\n");
2180   return true;
2181 }
2182
2183
2184 /* Dump all the immediate uses to FILE.  */
2185
2186 void
2187 dump_immediate_uses_for (FILE *file, tree var)
2188 {
2189   imm_use_iterator iter;
2190   use_operand_p use_p;
2191
2192   gcc_assert (var && TREE_CODE (var) == SSA_NAME);
2193
2194   print_generic_expr (file, var, TDF_SLIM);
2195   fprintf (file, " : -->");
2196   if (has_zero_uses (var))
2197     fprintf (file, " no uses.\n");
2198   else
2199     if (has_single_use (var))
2200       fprintf (file, " single use.\n");
2201     else
2202       fprintf (file, "%d uses.\n", num_imm_uses (var));
2203
2204   FOR_EACH_IMM_USE_FAST (use_p, iter, var)
2205     {
2206       if (!is_gimple_reg (USE_FROM_PTR (use_p)))
2207         print_generic_stmt (file, USE_STMT (use_p), TDF_VOPS);
2208       else
2209         print_generic_stmt (file, USE_STMT (use_p), TDF_SLIM);
2210     }
2211   fprintf(file, "\n");
2212 }
2213
2214 /* Dump all the immediate uses to FILE.  */
2215
2216 void
2217 dump_immediate_uses (FILE *file)
2218 {
2219   tree var;
2220   unsigned int x;
2221
2222   fprintf (file, "Immediate_uses: \n\n");
2223   for (x = 1; x < num_ssa_names; x++)
2224     {
2225       var = ssa_name(x);
2226       if (!var)
2227         continue;
2228       dump_immediate_uses_for (file, var);
2229     }
2230 }
2231
2232
2233 /* Dump def-use edges on stderr.  */
2234
2235 void
2236 debug_immediate_uses (void)
2237 {
2238   dump_immediate_uses (stderr);
2239 }
2240
2241 /* Dump def-use edges on stderr.  */
2242
2243 void
2244 debug_immediate_uses_for (tree var)
2245 {
2246   dump_immediate_uses_for (stderr, var);
2247 }
2248 #include "gt-tree-ssa-operands.h"