OSDN Git Service

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