OSDN Git Service

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