tree-ssa-operands.c (add_vars_for_offset): Fix exit test of loop adding SFTs as virtu...
[platform/upstream/gcc.git] / gcc / tree-ssa-operands.c
1 /* SSA operands management for trees.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007 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 3, 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 COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "flags.h"
26 #include "function.h"
27 #include "diagnostic.h"
28 #include "tree-flow.h"
29 #include "tree-inline.h"
30 #include "tree-pass.h"
31 #include "ggc.h"
32 #include "timevar.h"
33 #include "toplev.h"
34 #include "langhooks.h"
35 #include "ipa-reference.h"
36
37 /* This file contains the code required to manage the operands cache of the 
38    SSA optimizer.  For every stmt, we maintain an operand cache in the stmt 
39    annotation.  This cache contains operands that will be of interest to 
40    optimizers and other passes wishing to manipulate the IL. 
41
42    The operand type are broken up into REAL and VIRTUAL operands.  The real 
43    operands are represented as pointers into the stmt's operand tree.  Thus 
44    any manipulation of the real operands will be reflected in the actual tree.
45    Virtual operands are represented solely in the cache, although the base 
46    variable for the SSA_NAME may, or may not occur in the stmt's tree.  
47    Manipulation of the virtual operands will not be reflected in the stmt tree.
48
49    The routines in this file are concerned with creating this operand cache 
50    from a stmt tree.
51
52    The operand tree is the parsed by the various get_* routines which look 
53    through the stmt tree for the occurrence of operands which may be of 
54    interest, and calls are made to the append_* routines whenever one is 
55    found.  There are 4 of these routines, each representing one of the 
56    4 types of operands. Defs, Uses, Virtual Uses, and Virtual May Defs.
57
58    The append_* routines check for duplication, and simply keep a list of 
59    unique objects for each operand type in the build_* extendable vectors.
60
61    Once the stmt tree is completely parsed, the finalize_ssa_operands() 
62    routine is called, which proceeds to perform the finalization routine 
63    on each of the 4 operand vectors which have been built up.
64
65    If the stmt had a previous operand cache, the finalization routines 
66    attempt to match up the new operands with the old ones.  If it's a perfect 
67    match, the old vector is simply reused.  If it isn't a perfect match, then 
68    a new vector is created and the new operands are placed there.  For 
69    virtual operands, if the previous cache had SSA_NAME version of a 
70    variable, and that same variable occurs in the same operands cache, then 
71    the new cache vector will also get the same SSA_NAME.
72
73   i.e., if a stmt had a VUSE of 'a_5', and 'a' occurs in the new operand 
74   vector for VUSE, then the new vector will also be modified such that 
75   it contains 'a_5' rather than 'a'.  */
76
77
78 /* Structure storing statistics on how many call clobbers we have, and
79    how many where avoided.  */
80
81 static struct 
82 {
83   /* Number of call-clobbered ops we attempt to add to calls in
84      add_call_clobbered_mem_symbols.  */
85   unsigned int clobbered_vars;
86
87   /* Number of write-clobbers (VDEFs) avoided by using
88      not_written information.  */
89   unsigned int static_write_clobbers_avoided;
90
91   /* Number of reads (VUSEs) avoided by using not_read information.  */
92   unsigned int static_read_clobbers_avoided;
93   
94   /* Number of write-clobbers avoided because the variable can't escape to
95      this call.  */
96   unsigned int unescapable_clobbers_avoided;
97
98   /* Number of read-only uses we attempt to add to calls in
99      add_call_read_mem_symbols.  */
100   unsigned int readonly_clobbers;
101
102   /* Number of read-only uses we avoid using not_read information.  */
103   unsigned int static_readonly_clobbers_avoided;
104 } clobber_stats;
105
106
107 /* Flags to describe operand properties in helpers.  */
108
109 /* By default, operands are loaded.  */
110 #define opf_use         0
111
112 /* Operand is the target of an assignment expression or a 
113    call-clobbered variable.  */
114 #define opf_def         (1 << 0)
115
116 /* No virtual operands should be created in the expression.  This is used
117    when traversing ADDR_EXPR nodes which have different semantics than
118    other expressions.  Inside an ADDR_EXPR node, the only operands that we
119    need to consider are indices into arrays.  For instance, &a.b[i] should
120    generate a USE of 'i' but it should not generate a VUSE for 'a' nor a
121    VUSE for 'b'.  */
122 #define opf_no_vops     (1 << 1)
123
124 /* Operand is an implicit reference.  This is used to distinguish
125    explicit assignments in the form of GIMPLE_MODIFY_STMT from
126    clobbering sites like function calls or ASM_EXPRs.  */
127 #define opf_implicit    (1 << 2)
128
129 /* Array for building all the def operands.  */
130 static VEC(tree,heap) *build_defs;
131
132 /* Array for building all the use operands.  */
133 static VEC(tree,heap) *build_uses;
134
135 /* Set for building all the VDEF operands.  */
136 static VEC(tree,heap) *build_vdefs;
137
138 /* Set for building all the VUSE operands.  */
139 static VEC(tree,heap) *build_vuses;
140
141 /* Bitmap obstack for our datastructures that needs to survive across   
142    compilations of multiple functions.  */
143 static bitmap_obstack operands_bitmap_obstack;
144
145 /* Set for building all the loaded symbols.  */
146 static bitmap build_loads;
147
148 /* Set for building all the stored symbols.  */
149 static bitmap build_stores;
150
151 static void get_expr_operands (tree, tree *, int);
152
153 /* Number of functions with initialized ssa_operands.  */
154 static int n_initialized = 0;
155
156 /* Statement change buffer.  Data structure used to record state
157    information for statements.  This is used to determine what needs
158    to be done in order to update the SSA web after a statement is
159    modified by a pass.  If STMT is a statement that has just been
160    created, or needs to be folded via fold_stmt, or anything that
161    changes its physical structure then the pass should:
162
163    1- Call push_stmt_changes (&stmt) to record the current state of
164       STMT before any modifications are made.
165
166    2- Make all appropriate modifications to the statement.
167
168    3- Call pop_stmt_changes (&stmt) to find new symbols that
169       need to be put in SSA form, SSA name mappings for names that
170       have disappeared, recompute invariantness for address
171       expressions, cleanup EH information, etc.
172
173    If it is possible to determine that the statement was not modified,
174    instead of calling pop_stmt_changes it is quicker to call
175    discard_stmt_changes to avoid the expensive and unnecessary operand
176    re-scan and change comparison.  */
177
178 struct scb_d
179 {
180   /* Pointer to the statement being modified.  */
181   tree *stmt_p;
182
183   /* If the statement references memory these are the sets of symbols
184      loaded and stored by the statement.  */
185   bitmap loads;
186   bitmap stores;
187 };
188
189 typedef struct scb_d *scb_t;
190 DEF_VEC_P(scb_t);
191 DEF_VEC_ALLOC_P(scb_t,heap);
192
193 /* Stack of statement change buffers (SCB).  Every call to
194    push_stmt_changes pushes a new buffer onto the stack.  Calls to
195    pop_stmt_changes pop a buffer off of the stack and compute the set
196    of changes for the popped statement.  */
197 static VEC(scb_t,heap) *scb_stack;
198
199 /* Return the DECL_UID of the base variable of T.  */
200
201 static inline unsigned
202 get_name_decl (const_tree t)
203 {
204   if (TREE_CODE (t) != SSA_NAME)
205     return DECL_UID (t);
206   else
207     return DECL_UID (SSA_NAME_VAR (t));
208 }
209
210
211 /* Comparison function for qsort used in operand_build_sort_virtual.  */
212
213 static int
214 operand_build_cmp (const void *p, const void *q)
215 {
216   const_tree const e1 = *((const_tree const *)p);
217   const_tree const e2 = *((const_tree const *)q);
218   const unsigned int u1 = get_name_decl (e1);
219   const unsigned int u2 = get_name_decl (e2);
220
221   /* We want to sort in ascending order.  They can never be equal.  */
222 #ifdef ENABLE_CHECKING
223   gcc_assert (u1 != u2);
224 #endif
225   return (u1 > u2 ? 1 : -1);
226 }
227
228
229 /* Sort the virtual operands in LIST from lowest DECL_UID to highest.  */
230
231 static inline void
232 operand_build_sort_virtual (VEC(tree,heap) *list)
233 {
234   int num = VEC_length (tree, list);
235
236   if (num < 2)
237     return;
238
239   if (num == 2)
240     {
241       if (get_name_decl (VEC_index (tree, list, 0)) 
242           > get_name_decl (VEC_index (tree, list, 1)))
243         {  
244           /* Swap elements if in the wrong order.  */
245           tree tmp = VEC_index (tree, list, 0);
246           VEC_replace (tree, list, 0, VEC_index (tree, list, 1));
247           VEC_replace (tree, list, 1, tmp);
248         }
249       return;
250     }
251
252   /* There are 3 or more elements, call qsort.  */
253   qsort (VEC_address (tree, list), 
254          VEC_length (tree, list), 
255          sizeof (tree),
256          operand_build_cmp);
257 }
258
259
260 /*  Return true if the SSA operands cache is active.  */
261
262 bool
263 ssa_operands_active (void)
264 {
265   return cfun->gimple_df && gimple_ssa_operands (cfun)->ops_active;
266 }
267
268
269 /* VOPs are of variable sized, so the free list maps "free buckets" to the 
270    following table:  
271     bucket   # operands
272     ------   ----------
273         0       1
274         1       2
275           ...
276         15      16
277         16      17-24
278         17      25-32
279         18      31-40
280           ...
281         29      121-128
282    Any VOPs larger than this are simply added to the largest bucket when they
283    are freed.  */
284
285
286 /* Return the number of operands used in bucket BUCKET.  */
287
288 static inline int
289 vop_free_bucket_size (int bucket)
290 {
291 #ifdef ENABLE_CHECKING
292   gcc_assert (bucket >= 0 && bucket < NUM_VOP_FREE_BUCKETS);
293 #endif
294   if (bucket < 16)
295     return bucket + 1;
296   return (bucket - 13) * 8;
297 }
298
299
300 /* For a vop of NUM operands, return the bucket NUM belongs to.  If NUM is 
301    beyond the end of the bucket table, return -1.  */
302
303 static inline int 
304 vop_free_bucket_index (int num)
305 {
306   gcc_assert (num > 0 && NUM_VOP_FREE_BUCKETS > 16);
307
308   /* Sizes 1 through 16 use buckets 0-15.  */
309   if (num <= 16)
310     return num - 1;
311   /* Buckets 16 - NUM_VOP_FREE_BUCKETS represent 8 unit chunks.  */
312   num = 14 + (num - 1) / 8;
313   if (num >= NUM_VOP_FREE_BUCKETS)
314     return -1;
315   else
316     return num;
317 }
318
319
320 /* Initialize the VOP free buckets.  */
321
322 static inline void
323 init_vop_buckets (void)
324 {
325   int x;
326
327   for (x = 0; x < NUM_VOP_FREE_BUCKETS; x++)
328     gimple_ssa_operands (cfun)->vop_free_buckets[x] = NULL;
329 }
330
331
332 /* Add PTR to the appropriate VOP bucket.  */
333
334 static inline void
335 add_vop_to_freelist (voptype_p ptr)
336 {
337   int bucket = vop_free_bucket_index (VUSE_VECT_NUM_ELEM (ptr->usev));
338
339   /* Too large, use the largest bucket so its not a complete throw away.  */
340   if (bucket == -1)
341     bucket = NUM_VOP_FREE_BUCKETS - 1;
342
343   ptr->next = gimple_ssa_operands (cfun)->vop_free_buckets[bucket];
344   gimple_ssa_operands (cfun)->vop_free_buckets[bucket] = ptr;
345 }
346  
347
348 /* These are the sizes of the operand memory  buffer which gets allocated each 
349    time more operands space is required.  The final value is the amount that is
350    allocated every time after that.  */
351   
352 #define OP_SIZE_INIT    0
353 #define OP_SIZE_1       30
354 #define OP_SIZE_2       110
355 #define OP_SIZE_3       511
356
357 /* Initialize the operand cache routines.  */
358
359 void
360 init_ssa_operands (void)
361 {
362   if (!n_initialized++)
363     {
364       build_defs = VEC_alloc (tree, heap, 5);
365       build_uses = VEC_alloc (tree, heap, 10);
366       build_vuses = VEC_alloc (tree, heap, 25);
367       build_vdefs = VEC_alloc (tree, heap, 25);
368       bitmap_obstack_initialize (&operands_bitmap_obstack);
369       build_loads = BITMAP_ALLOC (&operands_bitmap_obstack);
370       build_stores = BITMAP_ALLOC (&operands_bitmap_obstack);
371       scb_stack = VEC_alloc (scb_t, heap, 20);
372     }
373
374   gcc_assert (gimple_ssa_operands (cfun)->operand_memory == NULL);
375   gcc_assert (gimple_ssa_operands (cfun)->mpt_table == NULL);
376   gimple_ssa_operands (cfun)->operand_memory_index
377      = gimple_ssa_operands (cfun)->ssa_operand_mem_size;
378   gimple_ssa_operands (cfun)->ops_active = true;
379   memset (&clobber_stats, 0, sizeof (clobber_stats));
380   init_vop_buckets ();
381   gimple_ssa_operands (cfun)->ssa_operand_mem_size = OP_SIZE_INIT;
382 }
383
384
385 /* Dispose of anything required by the operand routines.  */
386
387 void
388 fini_ssa_operands (void)
389 {
390   struct ssa_operand_memory_d *ptr;
391   unsigned ix;
392   tree mpt;
393
394   if (!--n_initialized)
395     {
396       VEC_free (tree, heap, build_defs);
397       VEC_free (tree, heap, build_uses);
398       VEC_free (tree, heap, build_vdefs);
399       VEC_free (tree, heap, build_vuses);
400       BITMAP_FREE (build_loads);
401       BITMAP_FREE (build_stores);
402
403       /* The change buffer stack had better be empty.  */
404       gcc_assert (VEC_length (scb_t, scb_stack) == 0);
405       VEC_free (scb_t, heap, scb_stack);
406       scb_stack = NULL;
407     }
408
409   gimple_ssa_operands (cfun)->free_defs = NULL;
410   gimple_ssa_operands (cfun)->free_uses = NULL;
411
412   while ((ptr = gimple_ssa_operands (cfun)->operand_memory) != NULL)
413     {
414       gimple_ssa_operands (cfun)->operand_memory
415         = gimple_ssa_operands (cfun)->operand_memory->next;
416       ggc_free (ptr);
417     }
418
419   for (ix = 0;
420        VEC_iterate (tree, gimple_ssa_operands (cfun)->mpt_table, ix, mpt);
421        ix++)
422     {
423       if (mpt)
424         BITMAP_FREE (MPT_SYMBOLS (mpt));
425     }
426
427   VEC_free (tree, heap, gimple_ssa_operands (cfun)->mpt_table);
428
429   gimple_ssa_operands (cfun)->ops_active = false;
430
431   if (!n_initialized)
432     bitmap_obstack_release (&operands_bitmap_obstack);
433   if (dump_file && (dump_flags & TDF_STATS))
434     {
435       fprintf (dump_file, "Original clobbered vars:           %d\n",
436                clobber_stats.clobbered_vars);
437       fprintf (dump_file, "Static write clobbers avoided:     %d\n",
438                clobber_stats.static_write_clobbers_avoided);
439       fprintf (dump_file, "Static read clobbers avoided:      %d\n",
440                clobber_stats.static_read_clobbers_avoided);
441       fprintf (dump_file, "Unescapable clobbers avoided:      %d\n",
442                clobber_stats.unescapable_clobbers_avoided);
443       fprintf (dump_file, "Original read-only clobbers:       %d\n",
444                clobber_stats.readonly_clobbers);
445       fprintf (dump_file, "Static read-only clobbers avoided: %d\n",
446                clobber_stats.static_readonly_clobbers_avoided);
447     }
448 }
449
450
451 /* Return memory for operands of SIZE chunks.  */
452                                                                               
453 static inline void *
454 ssa_operand_alloc (unsigned size)
455 {
456   char *ptr;
457
458   if (gimple_ssa_operands (cfun)->operand_memory_index + size
459       >= gimple_ssa_operands (cfun)->ssa_operand_mem_size)
460     {
461       struct ssa_operand_memory_d *ptr;
462
463       if (gimple_ssa_operands (cfun)->ssa_operand_mem_size == OP_SIZE_INIT)
464         gimple_ssa_operands (cfun)->ssa_operand_mem_size
465            = OP_SIZE_1 * sizeof (struct voptype_d);
466       else
467         if (gimple_ssa_operands (cfun)->ssa_operand_mem_size
468             == OP_SIZE_1 * sizeof (struct voptype_d))
469           gimple_ssa_operands (cfun)->ssa_operand_mem_size
470              = OP_SIZE_2 * sizeof (struct voptype_d);
471         else
472           gimple_ssa_operands (cfun)->ssa_operand_mem_size
473              = OP_SIZE_3 * sizeof (struct voptype_d);
474
475       /* Go right to the maximum size if the request is too large.  */
476       if (size > gimple_ssa_operands (cfun)->ssa_operand_mem_size)
477         gimple_ssa_operands (cfun)->ssa_operand_mem_size
478           = OP_SIZE_3 * sizeof (struct voptype_d);
479
480       /* Fail if there is not enough space.  If there are this many operands
481          required, first make sure there isn't a different problem causing this
482          many operands.  If the decision is that this is OK, then we can 
483          specially allocate a buffer just for this request.  */
484       gcc_assert (size <= gimple_ssa_operands (cfun)->ssa_operand_mem_size);
485
486       ptr = (struct ssa_operand_memory_d *) 
487               ggc_alloc (sizeof (struct ssa_operand_memory_d) 
488                          + gimple_ssa_operands (cfun)->ssa_operand_mem_size - 1);
489       ptr->next = gimple_ssa_operands (cfun)->operand_memory;
490       gimple_ssa_operands (cfun)->operand_memory = ptr;
491       gimple_ssa_operands (cfun)->operand_memory_index = 0;
492     }
493   ptr = &(gimple_ssa_operands (cfun)->operand_memory
494           ->mem[gimple_ssa_operands (cfun)->operand_memory_index]);
495   gimple_ssa_operands (cfun)->operand_memory_index += size;
496   return ptr;
497 }
498
499
500 /* Allocate a DEF operand.  */
501
502 static inline struct def_optype_d *
503 alloc_def (void)
504 {
505   struct def_optype_d *ret;
506   if (gimple_ssa_operands (cfun)->free_defs)
507     {
508       ret = gimple_ssa_operands (cfun)->free_defs;
509       gimple_ssa_operands (cfun)->free_defs
510         = gimple_ssa_operands (cfun)->free_defs->next;
511     }
512   else
513     ret = (struct def_optype_d *)
514           ssa_operand_alloc (sizeof (struct def_optype_d));
515   return ret;
516 }
517
518
519 /* Allocate a USE operand.  */
520
521 static inline struct use_optype_d *
522 alloc_use (void)
523 {
524   struct use_optype_d *ret;
525   if (gimple_ssa_operands (cfun)->free_uses)
526     {
527       ret = gimple_ssa_operands (cfun)->free_uses;
528       gimple_ssa_operands (cfun)->free_uses
529         = gimple_ssa_operands (cfun)->free_uses->next;
530     }
531   else
532     ret = (struct use_optype_d *)
533           ssa_operand_alloc (sizeof (struct use_optype_d));
534   return ret;
535 }
536
537
538 /* Allocate a vop with NUM elements.  */
539
540 static inline struct voptype_d *
541 alloc_vop (int num)
542 {
543   struct voptype_d *ret = NULL;
544   int alloc_size = 0;
545
546   int bucket = vop_free_bucket_index (num);
547   if (bucket != -1)
548     {
549       /* If there is a free operand, use it.  */
550       if (gimple_ssa_operands (cfun)->vop_free_buckets[bucket] != NULL)
551         {
552           ret = gimple_ssa_operands (cfun)->vop_free_buckets[bucket];
553           gimple_ssa_operands (cfun)->vop_free_buckets[bucket] = 
554                   gimple_ssa_operands (cfun)->vop_free_buckets[bucket]->next;
555         }
556       else
557         alloc_size = vop_free_bucket_size(bucket);
558     }
559   else
560     alloc_size = num;
561
562   if (alloc_size > 0)
563     ret = (struct voptype_d *)ssa_operand_alloc (
564         sizeof (struct voptype_d) + (alloc_size - 1) * sizeof (vuse_element_t));
565
566   VUSE_VECT_NUM_ELEM (ret->usev) = num;
567   return ret;
568 }
569
570
571 /* This routine makes sure that PTR is in an immediate use list, and makes
572    sure the stmt pointer is set to the current stmt.  */
573
574 static inline void
575 set_virtual_use_link (use_operand_p ptr, tree stmt)
576 {
577   /*  fold_stmt may have changed the stmt pointers.  */
578   if (ptr->stmt != stmt)
579     ptr->stmt = stmt;
580
581   /* If this use isn't in a list, add it to the correct list.  */
582   if (!ptr->prev)
583     link_imm_use (ptr, *(ptr->use));
584 }
585
586
587 /* Adds OP to the list of defs after LAST.  */
588
589 static inline def_optype_p 
590 add_def_op (tree *op, def_optype_p last)
591 {
592   def_optype_p new_def;
593
594   new_def = alloc_def ();
595   DEF_OP_PTR (new_def) = op;
596   last->next = new_def;
597   new_def->next = NULL;
598   return new_def;
599 }
600
601
602 /* Adds OP to the list of uses of statement STMT after LAST.  */
603
604 static inline use_optype_p
605 add_use_op (tree stmt, tree *op, use_optype_p last)
606 {
607   use_optype_p new_use;
608
609   new_use = alloc_use ();
610   USE_OP_PTR (new_use)->use = op;
611   link_imm_use_stmt (USE_OP_PTR (new_use), *op, stmt);
612   last->next = new_use;
613   new_use->next = NULL;
614   return new_use;
615 }
616
617
618 /* Return a virtual op pointer with NUM elements which are all initialized to OP
619    and are linked into the immediate uses for STMT.  The new vop is appended
620    after PREV.  */
621
622 static inline voptype_p
623 add_vop (tree stmt, tree op, int num, voptype_p prev)
624 {
625   voptype_p new_vop;
626   int x;
627
628   new_vop = alloc_vop (num);
629   for (x = 0; x < num; x++)
630     {
631       VUSE_OP_PTR (new_vop, x)->prev = NULL;
632       SET_VUSE_OP (new_vop, x, op);
633       VUSE_OP_PTR (new_vop, x)->use = &new_vop->usev.uses[x].use_var;
634       link_imm_use_stmt (VUSE_OP_PTR (new_vop, x),
635                          new_vop->usev.uses[x].use_var, stmt);
636     }
637
638   if (prev)
639     prev->next = new_vop;
640   new_vop->next = NULL;
641   return new_vop;
642 }
643
644
645 /* Adds OP to the list of vuses of statement STMT after LAST, and moves
646    LAST to the new element.  */
647
648 static inline voptype_p
649 add_vuse_op (tree stmt, tree op, int num, voptype_p last)
650 {
651   voptype_p new_vop = add_vop (stmt, op, num, last);
652   VDEF_RESULT (new_vop) = NULL_TREE;
653   return new_vop;
654 }
655
656
657 /* Adds OP to the list of vdefs of statement STMT after LAST, and moves
658    LAST to the new element.  */
659
660 static inline voptype_p
661 add_vdef_op (tree stmt, tree op, int num, voptype_p last)
662 {
663   voptype_p new_vop = add_vop (stmt, op, num, last);
664   VDEF_RESULT (new_vop) = op;
665   return new_vop;
666 }
667   
668
669 /* Takes elements from build_defs and turns them into def operands of STMT.
670    TODO -- Make build_defs VEC of tree *.  */
671
672 static inline void
673 finalize_ssa_defs (tree stmt)
674 {
675   unsigned new_i;
676   struct def_optype_d new_list;
677   def_optype_p old_ops, last;
678   unsigned int num = VEC_length (tree, build_defs);
679
680   /* There should only be a single real definition per assignment.  */
681   gcc_assert ((stmt && TREE_CODE (stmt) != GIMPLE_MODIFY_STMT) || num <= 1);
682
683   new_list.next = NULL;
684   last = &new_list;
685
686   old_ops = DEF_OPS (stmt);
687
688   new_i = 0;
689
690   /* Check for the common case of 1 def that hasn't changed.  */
691   if (old_ops && old_ops->next == NULL && num == 1
692       && (tree *) VEC_index (tree, build_defs, 0) == DEF_OP_PTR (old_ops))
693     return;
694
695   /* If there is anything in the old list, free it.  */
696   if (old_ops)
697     {
698       old_ops->next = gimple_ssa_operands (cfun)->free_defs;
699       gimple_ssa_operands (cfun)->free_defs = old_ops;
700     }
701
702   /* If there is anything remaining in the build_defs list, simply emit it.  */
703   for ( ; new_i < num; new_i++)
704     last = add_def_op ((tree *) VEC_index (tree, build_defs, new_i), last);
705
706   /* Now set the stmt's operands.  */
707   DEF_OPS (stmt) = new_list.next;
708
709 #ifdef ENABLE_CHECKING
710   {
711     def_optype_p ptr;
712     unsigned x = 0;
713     for (ptr = DEF_OPS (stmt); ptr; ptr = ptr->next)
714       x++;
715
716     gcc_assert (x == num);
717   }
718 #endif
719 }
720
721
722 /* Takes elements from build_uses and turns them into use operands of STMT.
723    TODO -- Make build_uses VEC of tree *.  */
724
725 static inline void
726 finalize_ssa_uses (tree stmt)
727 {
728   unsigned new_i;
729   struct use_optype_d new_list;
730   use_optype_p old_ops, ptr, last;
731
732 #ifdef ENABLE_CHECKING
733   {
734     unsigned x;
735     unsigned num = VEC_length (tree, build_uses);
736
737     /* If the pointer to the operand is the statement itself, something is
738        wrong.  It means that we are pointing to a local variable (the 
739        initial call to update_stmt_operands does not pass a pointer to a 
740        statement).  */
741     for (x = 0; x < num; x++)
742       gcc_assert (*((tree *)VEC_index (tree, build_uses, x)) != stmt);
743   }
744 #endif
745
746   new_list.next = NULL;
747   last = &new_list;
748
749   old_ops = USE_OPS (stmt);
750
751   /* If there is anything in the old list, free it.  */
752   if (old_ops)
753     {
754       for (ptr = old_ops; ptr; ptr = ptr->next)
755         delink_imm_use (USE_OP_PTR (ptr));
756       old_ops->next = gimple_ssa_operands (cfun)->free_uses;
757       gimple_ssa_operands (cfun)->free_uses = old_ops;
758     }
759
760   /* Now create nodes for all the new nodes.  */
761   for (new_i = 0; new_i < VEC_length (tree, build_uses); new_i++)
762     last = add_use_op (stmt, 
763                        (tree *) VEC_index (tree, build_uses, new_i), 
764                        last);
765
766   /* Now set the stmt's operands.  */
767   USE_OPS (stmt) = new_list.next;
768
769 #ifdef ENABLE_CHECKING
770   {
771     unsigned x = 0;
772     for (ptr = USE_OPS (stmt); ptr; ptr = ptr->next)
773       x++;
774
775     gcc_assert (x == VEC_length (tree, build_uses));
776   }
777 #endif
778 }
779
780
781 /* Takes elements from BUILD_VDEFS and turns them into vdef operands of
782    STMT.  FIXME, for now VDEF operators should have a single operand
783    in their RHS.  */
784
785 static inline void
786 finalize_ssa_vdefs (tree stmt)
787 {
788   unsigned new_i;
789   struct voptype_d new_list;
790   voptype_p old_ops, ptr, last;
791   stmt_ann_t ann = stmt_ann (stmt);
792
793   /* Set the symbols referenced by STMT.  */
794   if (!bitmap_empty_p (build_stores))
795     {
796       if (ann->operands.stores == NULL)
797         ann->operands.stores = BITMAP_ALLOC (&operands_bitmap_obstack);
798
799       bitmap_copy (ann->operands.stores, build_stores);
800     }
801   else
802     BITMAP_FREE (ann->operands.stores);
803
804   /* If aliases have not been computed, do not instantiate a virtual
805      operator on STMT.  Initially, we only compute the SSA form on
806      GIMPLE registers.  The virtual SSA form is only computed after
807      alias analysis, so virtual operators will remain unrenamed and
808      the verifier will complain.  However, alias analysis needs to
809      access symbol load/store information, so we need to compute
810      those.  */
811   if (!gimple_aliases_computed_p (cfun))
812     return;
813
814   new_list.next = NULL;
815   last = &new_list;
816
817   old_ops = VDEF_OPS (stmt);
818   new_i = 0;
819   while (old_ops && new_i < VEC_length (tree, build_vdefs))
820     {
821       tree op = VEC_index (tree, build_vdefs, new_i);
822       unsigned new_uid = get_name_decl (op);
823       unsigned old_uid = get_name_decl (VDEF_RESULT (old_ops));
824
825       /* FIXME, for now each VDEF operator should have at most one
826          operand in their RHS.  */
827       gcc_assert (VDEF_NUM (old_ops) == 1);
828
829       if (old_uid == new_uid)
830         {
831           /* If the symbols are the same, reuse the existing operand.  */
832           last->next = old_ops;
833           last = old_ops;
834           old_ops = old_ops->next;
835           last->next = NULL;
836           set_virtual_use_link (VDEF_OP_PTR (last, 0), stmt);
837           new_i++;
838         }
839       else if (old_uid < new_uid)
840         {
841           /* If old is less than new, old goes to the free list.  */
842           voptype_p next;
843           delink_imm_use (VDEF_OP_PTR (old_ops, 0));
844           next = old_ops->next;
845           add_vop_to_freelist (old_ops);
846           old_ops = next;
847         }
848       else
849         {
850           /* This is a new operand.  */
851           last = add_vdef_op (stmt, op, 1, last);
852           new_i++;
853         }
854     }
855
856   /* If there is anything remaining in BUILD_VDEFS, simply emit it.  */
857   for ( ; new_i < VEC_length (tree, build_vdefs); new_i++)
858     last = add_vdef_op (stmt, VEC_index (tree, build_vdefs, new_i), 1, last);
859
860   /* If there is anything in the old list, free it.  */
861   if (old_ops)
862     {
863       for (ptr = old_ops; ptr; ptr = last)
864         {
865           last = ptr->next;
866           delink_imm_use (VDEF_OP_PTR (ptr, 0));
867           add_vop_to_freelist (ptr);
868         }
869     }
870
871   /* Now set STMT's operands.  */
872   VDEF_OPS (stmt) = new_list.next;
873
874 #ifdef ENABLE_CHECKING
875   {
876     unsigned x = 0;
877     for (ptr = VDEF_OPS (stmt); ptr; ptr = ptr->next)
878       x++;
879
880     gcc_assert (x == VEC_length (tree, build_vdefs));
881   }
882 #endif
883 }
884
885
886 /* Takes elements from BUILD_VUSES and turns them into VUSE operands of
887    STMT.  */
888
889 static inline void
890 finalize_ssa_vuse_ops (tree stmt)
891 {
892   unsigned new_i, old_i;
893   voptype_p old_ops, last;
894   VEC(tree,heap) *new_ops;
895   stmt_ann_t ann;
896
897   /* Set the symbols referenced by STMT.  */
898   ann = stmt_ann (stmt);
899   if (!bitmap_empty_p (build_loads))
900     {
901       if (ann->operands.loads == NULL)
902         ann->operands.loads = BITMAP_ALLOC (&operands_bitmap_obstack);
903
904       bitmap_copy (ann->operands.loads, build_loads);
905     }
906   else
907     BITMAP_FREE (ann->operands.loads);
908
909   /* If aliases have not been computed, do not instantiate a virtual
910      operator on STMT.  Initially, we only compute the SSA form on
911      GIMPLE registers.  The virtual SSA form is only computed after
912      alias analysis, so virtual operators will remain unrenamed and
913      the verifier will complain.  However, alias analysis needs to
914      access symbol load/store information, so we need to compute
915      those.  */
916   if (!gimple_aliases_computed_p (cfun))
917     return;
918
919   /* STMT should have at most one VUSE operator.  */
920   old_ops = VUSE_OPS (stmt);
921   gcc_assert (old_ops == NULL || old_ops->next == NULL);
922
923   new_ops = NULL;
924   new_i = old_i = 0;
925   while (old_ops
926          && old_i < VUSE_NUM (old_ops)
927          && new_i < VEC_length (tree, build_vuses))
928     {
929       tree new_op = VEC_index (tree, build_vuses, new_i);
930       tree old_op = VUSE_OP (old_ops, old_i);
931       unsigned new_uid = get_name_decl (new_op);
932       unsigned old_uid = get_name_decl (old_op);
933
934       if (old_uid == new_uid)
935         {
936           /* If the symbols are the same, reuse the existing operand.  */
937           VEC_safe_push (tree, heap, new_ops, old_op);
938           new_i++;
939           old_i++;
940         }
941       else if (old_uid < new_uid)
942         {
943           /* If OLD_UID is less than NEW_UID, the old operand has
944              disappeared, skip to the next old operand.  */
945           old_i++;
946         }
947       else
948         {
949           /* This is a new operand.  */
950           VEC_safe_push (tree, heap, new_ops, new_op);
951           new_i++;
952         }
953     }
954
955   /* If there is anything remaining in the build_vuses list, simply emit it.  */
956   for ( ; new_i < VEC_length (tree, build_vuses); new_i++)
957     VEC_safe_push (tree, heap, new_ops, VEC_index (tree, build_vuses, new_i));
958
959   /* If there is anything in the old list, free it.  */
960   if (old_ops)
961     {
962       for (old_i = 0; old_i < VUSE_NUM (old_ops); old_i++)
963         delink_imm_use (VUSE_OP_PTR (old_ops, old_i));
964       add_vop_to_freelist (old_ops);
965       VUSE_OPS (stmt) = NULL;
966     }
967
968   /* If there are any operands, instantiate a VUSE operator for STMT.  */
969   if (new_ops)
970     {
971       tree op;
972       unsigned i;
973
974       last = add_vuse_op (stmt, NULL, VEC_length (tree, new_ops), NULL);
975
976       for (i = 0; VEC_iterate (tree, new_ops, i, op); i++)
977         SET_USE (VUSE_OP_PTR (last, (int) i), op);
978
979       VUSE_OPS (stmt) = last;
980       VEC_free (tree, heap, new_ops);
981     }
982
983 #ifdef ENABLE_CHECKING
984   {
985     unsigned x;
986     
987     if (VUSE_OPS (stmt))
988       {
989         gcc_assert (VUSE_OPS (stmt)->next == NULL);
990         x = VUSE_NUM (VUSE_OPS (stmt));
991       }
992     else
993       x = 0;
994
995     gcc_assert (x == VEC_length (tree, build_vuses));
996   }
997 #endif
998 }
999
1000 /* Return a new VUSE operand vector for STMT.  */
1001                                                                               
1002 static void
1003 finalize_ssa_vuses (tree stmt)
1004 {
1005   unsigned num, num_vdefs;
1006   unsigned vuse_index;
1007
1008   /* Remove superfluous VUSE operands.  If the statement already has a
1009      VDEF operator for a variable 'a', then a VUSE for 'a' is not
1010      needed because VDEFs imply a VUSE of the variable.  For instance,
1011      suppose that variable 'a' is pointed-to by p and q:
1012
1013               # VUSE <a_2>
1014               # a_3 = VDEF <a_2>
1015               *p = *q;
1016
1017      The VUSE <a_2> is superfluous because it is implied by the
1018      VDEF operator.  */
1019   num = VEC_length (tree, build_vuses);
1020   num_vdefs = VEC_length (tree, build_vdefs);
1021
1022   if (num > 0 && num_vdefs > 0)
1023     for (vuse_index = 0; vuse_index < VEC_length (tree, build_vuses); )
1024       {
1025         tree vuse;
1026         vuse = VEC_index (tree, build_vuses, vuse_index);
1027         if (TREE_CODE (vuse) != SSA_NAME)
1028           {
1029             var_ann_t ann = var_ann (vuse);
1030             ann->in_vuse_list = 0;
1031             if (ann->in_vdef_list)
1032               {
1033                 VEC_ordered_remove (tree, build_vuses, vuse_index);
1034                 continue;
1035               }
1036           }
1037         vuse_index++;
1038       }
1039
1040   finalize_ssa_vuse_ops (stmt);
1041 }
1042
1043
1044 /* Clear the in_list bits and empty the build array for VDEFs and
1045    VUSEs.  */
1046
1047 static inline void
1048 cleanup_build_arrays (void)
1049 {
1050   unsigned i;
1051   tree t;
1052
1053   for (i = 0; VEC_iterate (tree, build_vdefs, i, t); i++)
1054     if (TREE_CODE (t) != SSA_NAME)
1055       var_ann (t)->in_vdef_list = false;
1056
1057   for (i = 0; VEC_iterate (tree, build_vuses, i, t); i++)
1058     if (TREE_CODE (t) != SSA_NAME)
1059       var_ann (t)->in_vuse_list = false;
1060
1061   VEC_truncate (tree, build_vdefs, 0);
1062   VEC_truncate (tree, build_vuses, 0);
1063   VEC_truncate (tree, build_defs, 0);
1064   VEC_truncate (tree, build_uses, 0);
1065   bitmap_clear (build_loads);
1066   bitmap_clear (build_stores);
1067 }
1068
1069
1070 /* Finalize all the build vectors, fill the new ones into INFO.  */
1071                                                                               
1072 static inline void
1073 finalize_ssa_stmt_operands (tree stmt)
1074 {
1075   finalize_ssa_defs (stmt);
1076   finalize_ssa_uses (stmt);
1077   finalize_ssa_vdefs (stmt);
1078   finalize_ssa_vuses (stmt);
1079   cleanup_build_arrays ();
1080 }
1081
1082
1083 /* Start the process of building up operands vectors in INFO.  */
1084
1085 static inline void
1086 start_ssa_stmt_operands (void)
1087 {
1088   gcc_assert (VEC_length (tree, build_defs) == 0);
1089   gcc_assert (VEC_length (tree, build_uses) == 0);
1090   gcc_assert (VEC_length (tree, build_vuses) == 0);
1091   gcc_assert (VEC_length (tree, build_vdefs) == 0);
1092   gcc_assert (bitmap_empty_p (build_loads));
1093   gcc_assert (bitmap_empty_p (build_stores));
1094 }
1095
1096
1097 /* Add DEF_P to the list of pointers to operands.  */
1098
1099 static inline void
1100 append_def (tree *def_p)
1101 {
1102   VEC_safe_push (tree, heap, build_defs, (tree) def_p);
1103 }
1104
1105
1106 /* Add USE_P to the list of pointers to operands.  */
1107
1108 static inline void
1109 append_use (tree *use_p)
1110 {
1111   VEC_safe_push (tree, heap, build_uses, (tree) use_p);
1112 }
1113
1114
1115 /* Add VAR to the set of variables that require a VDEF operator.  */
1116
1117 static inline void
1118 append_vdef (tree var)
1119 {
1120   tree sym;
1121
1122   if (TREE_CODE (var) != SSA_NAME)
1123     {
1124       tree mpt;
1125       var_ann_t ann;
1126
1127       /* If VAR belongs to a memory partition, use it instead of VAR.  */
1128       mpt = memory_partition (var);
1129       if (mpt)
1130         var = mpt;
1131
1132       /* Don't allow duplicate entries.  */
1133       ann = get_var_ann (var);
1134       if (ann->in_vdef_list)
1135         return;
1136
1137       ann->in_vdef_list = true;
1138       sym = var;
1139     }
1140   else
1141     sym = SSA_NAME_VAR (var);
1142
1143   VEC_safe_push (tree, heap, build_vdefs, var);
1144   bitmap_set_bit (build_stores, DECL_UID (sym));
1145 }
1146
1147
1148 /* Add VAR to the set of variables that require a VUSE operator.  */
1149
1150 static inline void
1151 append_vuse (tree var)
1152 {
1153   tree sym;
1154
1155   if (TREE_CODE (var) != SSA_NAME)
1156     {
1157       tree mpt;
1158       var_ann_t ann;
1159
1160       /* If VAR belongs to a memory partition, use it instead of VAR.  */
1161       mpt = memory_partition (var);
1162       if (mpt)
1163         var = mpt;
1164
1165       /* Don't allow duplicate entries.  */
1166       ann = get_var_ann (var);
1167       if (ann->in_vuse_list)
1168         return;
1169       else if (ann->in_vdef_list)
1170        {
1171          /* We don't want a vuse if we already have a vdef, but we must
1172             still put this in build_loads.  */
1173          bitmap_set_bit (build_loads, DECL_UID (var));
1174          return;
1175        }
1176
1177       ann->in_vuse_list = true;
1178       sym = var;
1179     }
1180   else
1181     sym = SSA_NAME_VAR (var);
1182
1183   VEC_safe_push (tree, heap, build_vuses, var);
1184   bitmap_set_bit (build_loads, DECL_UID (sym));
1185 }
1186
1187
1188 /* REF is a tree that contains the entire pointer dereference
1189    expression, if available, or NULL otherwise.  ALIAS is the variable
1190    we are asking if REF can access.  OFFSET and SIZE come from the
1191    memory access expression that generated this virtual operand.
1192
1193    XXX: We should handle the NO_ALIAS attributes here.  */
1194
1195 static bool
1196 access_can_touch_variable (tree ref, tree alias, HOST_WIDE_INT offset,
1197                            HOST_WIDE_INT size)
1198 {
1199   bool offsetgtz = offset > 0;
1200   unsigned HOST_WIDE_INT uoffset = (unsigned HOST_WIDE_INT) offset;
1201   tree base = ref ? get_base_address (ref) : NULL;
1202
1203   /* If ALIAS is .GLOBAL_VAR then the memory reference REF must be
1204      using a call-clobbered memory tag.  By definition, call-clobbered
1205      memory tags can always touch .GLOBAL_VAR.  */
1206   if (alias == gimple_global_var (cfun))
1207     return true;
1208
1209   /* If ref is a TARGET_MEM_REF, just return true, as we can't really
1210      disambiguate them right now.  */
1211   if (ref && TREE_CODE (ref) == TARGET_MEM_REF)
1212     return true;
1213   
1214   /* If ALIAS is an SFT, it can't be touched if the offset     
1215      and size of the access is not overlapping with the SFT offset and
1216      size.  This is only true if we are accessing through a pointer
1217      to a type that is the same as SFT_PARENT_VAR.  Otherwise, we may
1218      be accessing through a pointer to some substruct of the
1219      structure, and if we try to prune there, we will have the wrong
1220      offset, and get the wrong answer.
1221      i.e., we can't prune without more work if we have something like
1222
1223      struct gcc_target
1224      {
1225        struct asm_out
1226        {
1227          const char *byte_op;
1228          struct asm_int_op
1229          {    
1230            const char *hi;
1231          } aligned_op;
1232        } asm_out;
1233      } targetm;
1234      
1235      foo = &targetm.asm_out.aligned_op;
1236      return foo->hi;
1237
1238      SFT.1, which represents hi, will have SFT_OFFSET=32 because in
1239      terms of SFT_PARENT_VAR, that is where it is.
1240      However, the access through the foo pointer will be at offset 0.  */
1241   if (size != -1
1242       && TREE_CODE (alias) == STRUCT_FIELD_TAG
1243       && base
1244       && TREE_TYPE (base) == TREE_TYPE (SFT_PARENT_VAR (alias))
1245       && !overlap_subvar (offset, size, alias, NULL))
1246     {
1247 #ifdef ACCESS_DEBUGGING
1248       fprintf (stderr, "Access to ");
1249       print_generic_expr (stderr, ref, 0);
1250       fprintf (stderr, " may not touch ");
1251       print_generic_expr (stderr, alias, 0);
1252       fprintf (stderr, " in function %s\n", get_name (current_function_decl));
1253 #endif
1254       return false;
1255     }
1256
1257   /* Without strict aliasing, it is impossible for a component access
1258      through a pointer to touch a random variable, unless that
1259      variable *is* a structure or a pointer.
1260
1261      That is, given p->c, and some random global variable b,
1262      there is no legal way that p->c could be an access to b.
1263      
1264      Without strict aliasing on, we consider it legal to do something
1265      like:
1266
1267      struct foos { int l; };
1268      int foo;
1269      static struct foos *getfoo(void);
1270      int main (void)
1271      {
1272        struct foos *f = getfoo();
1273        f->l = 1;
1274        foo = 2;
1275        if (f->l == 1)
1276          abort();
1277        exit(0);
1278      }
1279      static struct foos *getfoo(void)     
1280      { return (struct foos *)&foo; }
1281      
1282      (taken from 20000623-1.c)
1283
1284      The docs also say/imply that access through union pointers
1285      is legal (but *not* if you take the address of the union member,
1286      i.e. the inverse), such that you can do
1287
1288      typedef union {
1289        int d;
1290      } U;
1291
1292      int rv;
1293      void breakme()
1294      {
1295        U *rv0;
1296        U *pretmp = (U*)&rv;
1297        rv0 = pretmp;
1298        rv0->d = 42;    
1299      }
1300      To implement this, we just punt on accesses through union
1301      pointers entirely.
1302
1303      Another case we have to allow is accessing a variable
1304      through an array access at offset zero.  This happens from
1305      code generated by the fortran frontend like
1306
1307      char[1:1] & my_char_ref;
1308      char my_char;
1309      my_char_ref_1 = (char[1:1] &) &my_char;
1310      D.874_2 = (*my_char_ref_1)[1]{lb: 1 sz: 1};
1311   */
1312   else if (ref 
1313            && flag_strict_aliasing
1314            && TREE_CODE (ref) != INDIRECT_REF
1315            && !MTAG_P (alias)
1316            && base
1317            && (TREE_CODE (base) != INDIRECT_REF
1318                || TREE_CODE (TREE_TYPE (base)) != UNION_TYPE)
1319            && (TREE_CODE (base) != INDIRECT_REF
1320                || TREE_CODE (ref) != ARRAY_REF
1321                || offset != 0
1322                || (DECL_SIZE (alias)
1323                    && TREE_CODE (DECL_SIZE (alias)) == INTEGER_CST
1324                    && size != -1
1325                    && (unsigned HOST_WIDE_INT)size
1326                       != TREE_INT_CST_LOW (DECL_SIZE (alias))))
1327            && !AGGREGATE_TYPE_P (TREE_TYPE (alias))
1328            && TREE_CODE (TREE_TYPE (alias)) != COMPLEX_TYPE
1329            && !var_ann (alias)->is_heapvar
1330            /* When the struct has may_alias attached to it, we need not to
1331               return true.  */
1332            && get_alias_set (base))
1333     {
1334 #ifdef ACCESS_DEBUGGING
1335       fprintf (stderr, "Access to ");
1336       print_generic_expr (stderr, ref, 0);
1337       fprintf (stderr, " may not touch ");
1338       print_generic_expr (stderr, alias, 0);
1339       fprintf (stderr, " in function %s\n", get_name (current_function_decl));
1340 #endif
1341       return false;
1342     }
1343
1344   /* If the offset of the access is greater than the size of one of
1345      the possible aliases, it can't be touching that alias, because it
1346      would be past the end of the structure.  */
1347   else if (ref
1348            && flag_strict_aliasing
1349            && TREE_CODE (ref) != INDIRECT_REF
1350            && !MTAG_P (alias)
1351            && !POINTER_TYPE_P (TREE_TYPE (alias))
1352            && offsetgtz
1353            && DECL_SIZE (alias)
1354            && TREE_CODE (DECL_SIZE (alias)) == INTEGER_CST
1355            && uoffset > TREE_INT_CST_LOW (DECL_SIZE (alias)))
1356     {
1357 #ifdef ACCESS_DEBUGGING
1358       fprintf (stderr, "Access to ");
1359       print_generic_expr (stderr, ref, 0);
1360       fprintf (stderr, " may not touch ");
1361       print_generic_expr (stderr, alias, 0);
1362       fprintf (stderr, " in function %s\n", get_name (current_function_decl));
1363 #endif
1364       return false;
1365     }      
1366
1367   return true;
1368 }
1369
1370 /* Add the actual variables FULL_REF can access, given a member of
1371    full_ref's points-to set VAR, where FULL_REF is an access of SIZE at
1372    OFFSET from var. IS_CALL_SITE is true if this is a call, and IS_DEF
1373    is true if this is supposed to be a vdef, and false if this should
1374    be a VUSE.
1375
1376    The real purpose of this function is to take a points-to set for a
1377    pointer to a structure, say
1378
1379    struct s {
1380      int a;
1381      int b;
1382    } foo, *foop = &foo;
1383
1384    and discover which variables an access, such as foop->b, can alias.
1385    
1386    This is necessary because foop only actually points to foo's first
1387    member, so that is all the points-to set contains.  However, an access
1388    to foop->a may be touching some single SFT if we have created some
1389    SFT's for a structure.  */
1390
1391 static bool
1392 add_vars_for_offset (tree var,
1393                      unsigned HOST_WIDE_INT offset, unsigned HOST_WIDE_INT size,
1394                      bool is_def)
1395 {
1396   bool added = false;
1397   tree subvar;
1398   subvar_t sv;
1399   unsigned int i;
1400
1401   /* Adjust offset by the pointed-to location.  */
1402   offset += SFT_OFFSET (var);
1403
1404   /* Add all subvars of var that overlap with the access.
1405      Binary search for the first relevant SFT.  */
1406   sv = get_subvars_for_var (SFT_PARENT_VAR (var));
1407   if (!get_first_overlapping_subvar (sv, offset, size, &i))
1408     return false;
1409
1410   for (; VEC_iterate (tree, sv, i, subvar); ++i)
1411     {
1412       if (SFT_OFFSET (subvar) > offset
1413           && size <= SFT_OFFSET (subvar) - offset)
1414         break;
1415
1416       if (is_def)
1417         append_vdef (subvar);
1418       else
1419         append_vuse (subvar);
1420       added = true;
1421     }
1422
1423   return added;
1424 }
1425
1426 /* Add VAR to the virtual operands array.  FLAGS is as in
1427    get_expr_operands.  FULL_REF is a tree that contains the entire
1428    pointer dereference expression, if available, or NULL otherwise.
1429    OFFSET and SIZE come from the memory access expression that
1430    generated this virtual operand.  IS_CALL_SITE is true if the
1431    affected statement is a call site.  */
1432
1433 static void
1434 add_virtual_operand (tree var, stmt_ann_t s_ann, int flags,
1435                      tree full_ref, HOST_WIDE_INT offset,
1436                      HOST_WIDE_INT size, bool is_call_site)
1437 {
1438   bitmap aliases = NULL;
1439   tree sym;
1440   var_ann_t v_ann;
1441   
1442   sym = (TREE_CODE (var) == SSA_NAME ? SSA_NAME_VAR (var) : var);
1443   v_ann = var_ann (sym);
1444   
1445   /* Mark the statement as having memory operands.  */
1446   s_ann->references_memory = true;
1447
1448   /* If the variable cannot be modified and this is a VDEF change
1449      it into a VUSE.  This happens when read-only variables are marked
1450      call-clobbered and/or aliased to writable variables.  So we only
1451      check that this only happens on non-specific stores.
1452
1453      Note that if this is a specific store, i.e. associated with a
1454      GIMPLE_MODIFY_STMT, then we can't suppress the VDEF, lest we run
1455      into validation problems.
1456
1457      This can happen when programs cast away const, leaving us with a
1458      store to read-only memory.  If the statement is actually executed
1459      at runtime, then the program is ill formed.  If the statement is
1460      not executed then all is well.  At the very least, we cannot ICE.  */
1461   if ((flags & opf_implicit) && unmodifiable_var_p (var))
1462     flags &= ~opf_def;
1463   
1464   /* The variable is not a GIMPLE register.  Add it (or its aliases) to
1465      virtual operands, unless the caller has specifically requested
1466      not to add virtual operands (used when adding operands inside an
1467      ADDR_EXPR expression).  */
1468   if (flags & opf_no_vops)
1469     return;
1470   
1471   if (MTAG_P (var))
1472     aliases = MTAG_ALIASES (var);
1473
1474   if (aliases == NULL)
1475     {
1476       if (!gimple_aliases_computed_p (cfun)
1477           && (flags & opf_def))
1478         s_ann->has_volatile_ops = true;
1479
1480       /* The variable is not aliased or it is an alias tag.  */
1481       if (flags & opf_def)
1482         append_vdef (var);
1483       else
1484         append_vuse (var);
1485     }
1486   else
1487     {
1488       bitmap_iterator bi;
1489       unsigned int i;
1490       bool none_added = true;
1491       
1492       /* The variable is aliased.  Add its aliases to the virtual
1493          operands.  */
1494       gcc_assert (!bitmap_empty_p (aliases));
1495
1496       EXECUTE_IF_SET_IN_BITMAP (aliases, 0, i, bi)
1497         {
1498           tree al = referenced_var (i);
1499
1500           /* For SFTs we have to consider all subvariables of the parent var
1501              if it is a potential points-to location.  */
1502           if (TREE_CODE (al) == STRUCT_FIELD_TAG
1503               && TREE_CODE (var) == NAME_MEMORY_TAG)
1504             none_added &= !add_vars_for_offset (al, offset, size,
1505                                                 flags & opf_def);
1506           else
1507             {
1508               /* Call-clobbered tags may have non-call-clobbered
1509                  symbols in their alias sets.  Ignore them if we are
1510                  adding VOPs for a call site.  */
1511               if (is_call_site && !is_call_clobbered (al))
1512                  continue;
1513
1514               /* If we do not know the full reference tree or if the access is
1515                  unspecified [0, -1], we cannot prune it.  Otherwise try doing
1516                  so using access_can_touch_variable.  */
1517               if (full_ref
1518                   && !(offset == 0 && size == -1)
1519                   && !access_can_touch_variable (full_ref, al, offset, size))
1520                 continue;
1521
1522               if (flags & opf_def)
1523                 append_vdef (al);
1524               else
1525                 append_vuse (al);
1526               none_added = false;
1527             }
1528         }
1529
1530       if (flags & opf_def)
1531         {
1532           /* If the variable is also an alias tag, add a virtual
1533              operand for it, otherwise we will miss representing
1534              references to the members of the variable's alias set.          
1535              This fixes the bug in gcc.c-torture/execute/20020503-1.c.
1536              
1537              It is also necessary to add bare defs on clobbers for
1538              SMT's, so that bare SMT uses caused by pruning all the
1539              aliases will link up properly with calls.   In order to
1540              keep the number of these bare defs we add down to the
1541              minimum necessary, we keep track of which SMT's were used
1542              alone in statement vdefs or VUSEs.  */
1543           if (none_added
1544               || (TREE_CODE (var) == SYMBOL_MEMORY_TAG
1545                   && is_call_site))
1546             append_vdef (var);
1547         }
1548       else
1549         {
1550           /* Even if no aliases have been added, we still need to
1551              establish def-use and use-def chains, lest
1552              transformations think that this is not a memory
1553              reference.  For an example of this scenario, see
1554              testsuite/g++.dg/opt/cleanup1.C.  */
1555           if (none_added)
1556             append_vuse (var);
1557         }
1558     }
1559 }
1560
1561
1562 /* Add *VAR_P to the appropriate operand array for S_ANN.  FLAGS is as in
1563    get_expr_operands.  If *VAR_P is a GIMPLE register, it will be added to
1564    the statement's real operands, otherwise it is added to virtual
1565    operands.  */
1566
1567 static void
1568 add_stmt_operand (tree *var_p, stmt_ann_t s_ann, int flags)
1569 {
1570   tree var, sym;
1571   var_ann_t v_ann;
1572
1573   gcc_assert (SSA_VAR_P (*var_p) && s_ann);
1574
1575   var = *var_p;
1576   sym = (TREE_CODE (var) == SSA_NAME ? SSA_NAME_VAR (var) : var);
1577   v_ann = var_ann (sym);
1578
1579   /* Mark statements with volatile operands.  */
1580   if (TREE_THIS_VOLATILE (sym))
1581     s_ann->has_volatile_ops = true;
1582
1583   if (is_gimple_reg (sym))
1584     {
1585       /* The variable is a GIMPLE register.  Add it to real operands.  */
1586       if (flags & opf_def)
1587         append_def (var_p);
1588       else
1589         append_use (var_p);
1590     }
1591   else
1592     add_virtual_operand (var, s_ann, flags, NULL_TREE, 0, -1, false);
1593 }
1594
1595 /* Subroutine of get_indirect_ref_operands.  ADDR is the address
1596    that is dereferenced, the meaning of the rest of the arguments
1597    is the same as in get_indirect_ref_operands.  */
1598
1599 static void
1600 get_addr_dereference_operands (tree stmt, tree *addr, int flags,
1601                                                       tree full_ref,
1602                                                        HOST_WIDE_INT offset, HOST_WIDE_INT size,
1603                                                        bool recurse_on_base)
1604   {
1605  tree ptr = *addr;
1606   stmt_ann_t s_ann = stmt_ann (stmt);
1607
1608   s_ann->references_memory = true;
1609
1610   if (SSA_VAR_P (ptr))
1611     {
1612       struct ptr_info_def *pi = NULL;
1613
1614       /* If PTR has flow-sensitive points-to information, use it.  */
1615       if (TREE_CODE (ptr) == SSA_NAME
1616           && (pi = SSA_NAME_PTR_INFO (ptr)) != NULL
1617           && pi->name_mem_tag)
1618         {
1619           /* PTR has its own memory tag.  Use it.  */
1620           add_virtual_operand (pi->name_mem_tag, s_ann, flags,
1621                                full_ref, offset, size, false);
1622         }
1623       else
1624         {
1625           /* If PTR is not an SSA_NAME or it doesn't have a name
1626              tag, use its symbol memory tag.  */
1627           var_ann_t v_ann;
1628
1629           /* If we are emitting debugging dumps, display a warning if
1630              PTR is an SSA_NAME with no flow-sensitive alias
1631              information.  That means that we may need to compute
1632              aliasing again.  */
1633           if (dump_file
1634               && TREE_CODE (ptr) == SSA_NAME
1635               && pi == NULL)
1636             {
1637               fprintf (dump_file,
1638                   "NOTE: no flow-sensitive alias info for ");
1639               print_generic_expr (dump_file, ptr, dump_flags);
1640               fprintf (dump_file, " in ");
1641               print_generic_stmt (dump_file, stmt, dump_flags);
1642             }
1643
1644           if (TREE_CODE (ptr) == SSA_NAME)
1645             ptr = SSA_NAME_VAR (ptr);
1646           v_ann = var_ann (ptr);
1647
1648           /* If we don't know what this pointer points to then we have
1649              to make sure to not prune virtual operands based on offset
1650              and size.  */
1651           if (v_ann->symbol_mem_tag)
1652             add_virtual_operand (v_ann->symbol_mem_tag, s_ann, flags,
1653                                  full_ref, 0, -1, false);
1654
1655           /* Aliasing information is missing; mark statement as
1656              volatile so we won't optimize it out too actively.  */
1657           else if (!gimple_aliases_computed_p (cfun)
1658                    && (flags & opf_def))
1659             s_ann->has_volatile_ops = true;
1660         }
1661     }
1662   else if (TREE_CODE (ptr) == INTEGER_CST)
1663     {
1664       /* If a constant is used as a pointer, we can't generate a real
1665          operand for it but we mark the statement volatile to prevent
1666          optimizations from messing things up.  */
1667       s_ann->has_volatile_ops = true;
1668       return;
1669     }
1670   else
1671     {
1672       /* Ok, this isn't even is_gimple_min_invariant.  Something's broke.  */
1673       gcc_unreachable ();
1674     }
1675
1676   /* If requested, add a USE operand for the base pointer.  */
1677   if (recurse_on_base)
1678     get_expr_operands (stmt, addr, opf_use);
1679 }
1680
1681 /* A subroutine of get_expr_operands to handle INDIRECT_REF,
1682    ALIGN_INDIRECT_REF and MISALIGNED_INDIRECT_REF.  
1683
1684    STMT is the statement being processed, EXPR is the INDIRECT_REF
1685       that got us here.
1686    
1687    FLAGS is as in get_expr_operands.
1688
1689    FULL_REF contains the full pointer dereference expression, if we
1690       have it, or NULL otherwise.
1691
1692    OFFSET and SIZE are the location of the access inside the
1693       dereferenced pointer, if known.
1694
1695    RECURSE_ON_BASE should be set to true if we want to continue
1696       calling get_expr_operands on the base pointer, and false if
1697       something else will do it for us.  */
1698
1699 static void
1700 get_indirect_ref_operands (tree stmt, tree expr, int flags,
1701                                                     tree full_ref,
1702                                                     HOST_WIDE_INT offset, HOST_WIDE_INT size,
1703                                                     bool recurse_on_base)
1704 {
1705   tree *pptr = &TREE_OPERAND (expr, 0);
1706   stmt_ann_t s_ann = stmt_ann (stmt);
1707
1708   if (TREE_THIS_VOLATILE (expr))
1709     s_ann->has_volatile_ops = true; 
1710
1711   get_addr_dereference_operands (stmt, pptr, flags, full_ref,
1712                                                                   offset, size, recurse_on_base);
1713 }
1714
1715 /* A subroutine of get_expr_operands to handle TARGET_MEM_REF.  */
1716
1717 static void
1718 get_tmr_operands (tree stmt, tree expr, int flags)
1719 {
1720   tree tag;
1721   stmt_ann_t s_ann = stmt_ann (stmt);
1722
1723   /* This statement references memory.  */
1724   s_ann->references_memory = 1;
1725
1726   /* First record the real operands.  */
1727   get_expr_operands (stmt, &TMR_BASE (expr), opf_use);
1728   get_expr_operands (stmt, &TMR_INDEX (expr), opf_use);
1729
1730   if (TMR_SYMBOL (expr))
1731     add_to_addressable_set (TMR_SYMBOL (expr), &s_ann->addresses_taken);
1732
1733   tag = TMR_TAG (expr);
1734   if (!tag)
1735     {
1736       /* Something weird, so ensure that we will be careful.  */
1737       s_ann->has_volatile_ops = true;
1738       return;
1739     }
1740   if (!MTAG_P (tag))
1741     {
1742       get_expr_operands (stmt, &tag, flags);
1743       return;
1744     }
1745
1746   add_virtual_operand (tag, s_ann, flags, expr, 0, -1, false);
1747 }
1748
1749
1750 /* Add clobbering definitions for .GLOBAL_VAR or for each of the call
1751    clobbered variables in the function.  */
1752
1753 static void
1754 add_call_clobber_ops (tree stmt, tree callee)
1755 {
1756   unsigned u;
1757   bitmap_iterator bi;
1758   stmt_ann_t s_ann = stmt_ann (stmt);
1759   bitmap not_read_b, not_written_b;
1760   
1761   /* If we created .GLOBAL_VAR earlier, just use it.  */
1762   if (gimple_global_var (cfun))
1763     {
1764       tree var = gimple_global_var (cfun);
1765       add_virtual_operand (var, s_ann, opf_def, NULL, 0, -1, true);
1766       return;
1767     }
1768
1769   /* Get info for local and module level statics.  There is a bit
1770      set for each static if the call being processed does not read
1771      or write that variable.  */
1772   not_read_b = callee ? ipa_reference_get_not_read_global (callee) : NULL; 
1773   not_written_b = callee ? ipa_reference_get_not_written_global (callee) : NULL; 
1774
1775   /* Add a VDEF operand for every call clobbered variable.  */
1776   EXECUTE_IF_SET_IN_BITMAP (gimple_call_clobbered_vars (cfun), 0, u, bi)
1777     {
1778       tree var = referenced_var_lookup (u);
1779       unsigned int escape_mask = var_ann (var)->escape_mask;
1780       tree real_var = var;
1781       bool not_read;
1782       bool not_written;
1783       
1784       /* Not read and not written are computed on regular vars, not
1785          subvars, so look at the parent var if this is an SFT. */
1786       if (TREE_CODE (var) == STRUCT_FIELD_TAG)
1787         real_var = SFT_PARENT_VAR (var);
1788
1789       not_read = not_read_b
1790                  ? bitmap_bit_p (not_read_b, DECL_UID (real_var))
1791                  : false;
1792
1793       not_written = not_written_b
1794                     ? bitmap_bit_p (not_written_b, DECL_UID (real_var))
1795                     : false;
1796       gcc_assert (!unmodifiable_var_p (var));
1797       
1798       clobber_stats.clobbered_vars++;
1799
1800       /* See if this variable is really clobbered by this function.  */
1801
1802       /* Trivial case: Things escaping only to pure/const are not
1803          clobbered by non-pure-const, and only read by pure/const. */
1804       if ((escape_mask & ~(ESCAPE_TO_PURE_CONST)) == 0)
1805         {
1806           tree call = get_call_expr_in (stmt);
1807           if (call_expr_flags (call) & (ECF_CONST | ECF_PURE))
1808             {
1809               add_virtual_operand (var, s_ann, opf_use, NULL, 0, -1, true);
1810               clobber_stats.unescapable_clobbers_avoided++;
1811               continue;
1812             }
1813           else
1814             {
1815               clobber_stats.unescapable_clobbers_avoided++;
1816               continue;
1817             }
1818         }
1819             
1820       if (not_written)
1821         {
1822           clobber_stats.static_write_clobbers_avoided++;
1823           if (!not_read)
1824             add_virtual_operand (var, s_ann, opf_use, NULL, 0, -1, true);
1825           else
1826             clobber_stats.static_read_clobbers_avoided++;
1827         }
1828       else
1829         add_virtual_operand (var, s_ann, opf_def, NULL, 0, -1, true);
1830     }
1831 }
1832
1833
1834 /* Add VUSE operands for .GLOBAL_VAR or all call clobbered variables in the
1835    function.  */
1836
1837 static void
1838 add_call_read_ops (tree stmt, tree callee)
1839 {
1840   unsigned u;
1841   bitmap_iterator bi;
1842   stmt_ann_t s_ann = stmt_ann (stmt);
1843   bitmap not_read_b;
1844
1845   /* if the function is not pure, it may reference memory.  Add
1846      a VUSE for .GLOBAL_VAR if it has been created.  See add_referenced_var
1847      for the heuristic used to decide whether to create .GLOBAL_VAR.  */
1848   if (gimple_global_var (cfun))
1849     {
1850       tree var = gimple_global_var (cfun);
1851       add_virtual_operand (var, s_ann, opf_use, NULL, 0, -1, true);
1852       return;
1853     }
1854   
1855   not_read_b = callee ? ipa_reference_get_not_read_global (callee) : NULL; 
1856
1857   /* Add a VUSE for each call-clobbered variable.  */
1858   EXECUTE_IF_SET_IN_BITMAP (gimple_call_clobbered_vars (cfun), 0, u, bi)
1859     {
1860       tree var = referenced_var (u);
1861       tree real_var = var;
1862       bool not_read;
1863       
1864       clobber_stats.readonly_clobbers++;
1865
1866       /* Not read and not written are computed on regular vars, not
1867          subvars, so look at the parent var if this is an SFT. */
1868
1869       if (TREE_CODE (var) == STRUCT_FIELD_TAG)
1870         real_var = SFT_PARENT_VAR (var);
1871
1872       not_read = not_read_b ? bitmap_bit_p (not_read_b, DECL_UID (real_var))
1873                             : false;
1874       
1875       if (not_read)
1876         {
1877           clobber_stats.static_readonly_clobbers_avoided++;
1878           continue;
1879         }
1880             
1881       add_virtual_operand (var, s_ann, opf_use, NULL, 0, -1, true);
1882     }
1883 }
1884
1885
1886 /* A subroutine of get_expr_operands to handle CALL_EXPR.  */
1887
1888 static void
1889 get_call_expr_operands (tree stmt, tree expr)
1890 {
1891   int call_flags = call_expr_flags (expr);
1892   int i, nargs;
1893   stmt_ann_t ann = stmt_ann (stmt);
1894
1895   ann->references_memory = true;
1896
1897   /* If aliases have been computed already, add VDEF or VUSE
1898      operands for all the symbols that have been found to be
1899      call-clobbered.  */
1900   if (gimple_aliases_computed_p (cfun)
1901       && !(call_flags & ECF_NOVOPS))
1902     {
1903       /* A 'pure' or a 'const' function never call-clobbers anything. 
1904          A 'noreturn' function might, but since we don't return anyway 
1905          there is no point in recording that.  */ 
1906       if (TREE_SIDE_EFFECTS (expr)
1907           && !(call_flags & (ECF_PURE | ECF_CONST | ECF_NORETURN)))
1908         add_call_clobber_ops (stmt, get_callee_fndecl (expr));
1909       else if (!(call_flags & ECF_CONST))
1910         add_call_read_ops (stmt, get_callee_fndecl (expr));
1911     }
1912
1913   /* Find uses in the called function.  */
1914   get_expr_operands (stmt, &CALL_EXPR_FN (expr), opf_use);
1915   nargs = call_expr_nargs (expr);
1916   for (i = 0; i < nargs; i++)
1917     get_expr_operands (stmt, &CALL_EXPR_ARG (expr, i), opf_use);
1918
1919   get_expr_operands (stmt, &CALL_EXPR_STATIC_CHAIN (expr), opf_use);
1920 }
1921
1922
1923 /* Scan operands in the ASM_EXPR stmt referred to in INFO.  */
1924
1925 static void
1926 get_asm_expr_operands (tree stmt)
1927 {
1928   stmt_ann_t s_ann;
1929   int i, noutputs;
1930   const char **oconstraints;
1931   const char *constraint;
1932   bool allows_mem, allows_reg, is_inout;
1933   tree link;
1934
1935   s_ann = stmt_ann (stmt);
1936   noutputs = list_length (ASM_OUTPUTS (stmt));
1937   oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
1938
1939   /* Gather all output operands.  */
1940   for (i = 0, link = ASM_OUTPUTS (stmt); link; i++, link = TREE_CHAIN (link))
1941     {
1942       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1943       oconstraints[i] = constraint;
1944       parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
1945                                &allows_reg, &is_inout);
1946
1947       /* This should have been split in gimplify_asm_expr.  */
1948       gcc_assert (!allows_reg || !is_inout);
1949
1950       /* Memory operands are addressable.  Note that STMT needs the
1951          address of this operand.  */
1952       if (!allows_reg && allows_mem)
1953         {
1954           tree t = get_base_address (TREE_VALUE (link));
1955           if (t && DECL_P (t) && s_ann)
1956             add_to_addressable_set (t, &s_ann->addresses_taken);
1957         }
1958
1959       get_expr_operands (stmt, &TREE_VALUE (link), opf_def);
1960     }
1961
1962   /* Gather all input operands.  */
1963   for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
1964     {
1965       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1966       parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
1967                               &allows_mem, &allows_reg);
1968
1969       /* Memory operands are addressable.  Note that STMT needs the
1970          address of this operand.  */
1971       if (!allows_reg && allows_mem)
1972         {
1973           tree t = get_base_address (TREE_VALUE (link));
1974           if (t && DECL_P (t) && s_ann)
1975             add_to_addressable_set (t, &s_ann->addresses_taken);
1976         }
1977
1978       get_expr_operands (stmt, &TREE_VALUE (link), 0);
1979     }
1980
1981   /* Clobber all memory and addressable symbols for asm ("" : : : "memory");  */
1982   for (link = ASM_CLOBBERS (stmt); link; link = TREE_CHAIN (link))
1983     if (strcmp (TREE_STRING_POINTER (TREE_VALUE (link)), "memory") == 0)
1984       {
1985         unsigned i;
1986         bitmap_iterator bi;
1987
1988         s_ann->references_memory = true;
1989
1990         EXECUTE_IF_SET_IN_BITMAP (gimple_call_clobbered_vars (cfun), 0, i, bi)
1991           {
1992             tree var = referenced_var (i);
1993             add_stmt_operand (&var, s_ann, opf_def | opf_implicit);
1994           }
1995
1996         EXECUTE_IF_SET_IN_BITMAP (gimple_addressable_vars (cfun), 0, i, bi)
1997           {
1998             tree var = referenced_var (i);
1999
2000             /* Subvars are explicitly represented in this list, so we
2001                don't need the original to be added to the clobber ops,
2002                but the original *will* be in this list because we keep
2003                the addressability of the original variable up-to-date
2004                to avoid confusing the back-end.  */
2005             if (var_can_have_subvars (var)
2006                 && get_subvars_for_var (var) != NULL)
2007               continue;         
2008
2009             add_stmt_operand (&var, s_ann, opf_def | opf_implicit);
2010           }
2011         break;
2012       }
2013 }
2014
2015
2016 /* Scan operands for the assignment expression EXPR in statement STMT.  */
2017
2018 static void
2019 get_modify_stmt_operands (tree stmt, tree expr)
2020 {
2021   /* First get operands from the RHS.  */
2022   get_expr_operands (stmt, &GIMPLE_STMT_OPERAND (expr, 1), opf_use);
2023
2024   /* For the LHS, use a regular definition (opf_def) for GIMPLE
2025      registers.  If the LHS is a store to memory, we will need
2026      a preserving definition (VDEF).
2027
2028      Preserving definitions are those that modify a part of an
2029      aggregate object for which no subvars have been computed (or the
2030      reference does not correspond exactly to one of them). Stores
2031      through a pointer are also represented with VDEF operators.
2032
2033      We used to distinguish between preserving and killing definitions.
2034      We always emit preserving definitions now.  */
2035   get_expr_operands (stmt, &GIMPLE_STMT_OPERAND (expr, 0), opf_def);
2036 }
2037
2038
2039 /* Recursively scan the expression pointed to by EXPR_P in statement
2040    STMT.  FLAGS is one of the OPF_* constants modifying how to
2041    interpret the operands found.  */
2042
2043 static void
2044 get_expr_operands (tree stmt, tree *expr_p, int flags)
2045 {
2046   enum tree_code code;
2047   enum tree_code_class codeclass;
2048   tree expr = *expr_p;
2049   stmt_ann_t s_ann = stmt_ann (stmt);
2050
2051   if (expr == NULL)
2052     return;
2053
2054   code = TREE_CODE (expr);
2055   codeclass = TREE_CODE_CLASS (code);
2056
2057   switch (code)
2058     {
2059     case ADDR_EXPR:
2060       /* Taking the address of a variable does not represent a
2061          reference to it, but the fact that the statement takes its
2062          address will be of interest to some passes (e.g. alias
2063          resolution).  */
2064       add_to_addressable_set (TREE_OPERAND (expr, 0), &s_ann->addresses_taken);
2065
2066       /* If the address is invariant, there may be no interesting
2067          variable references inside.  */
2068       if (is_gimple_min_invariant (expr))
2069         return;
2070
2071       /* Otherwise, there may be variables referenced inside but there
2072          should be no VUSEs created, since the referenced objects are
2073          not really accessed.  The only operands that we should find
2074          here are ARRAY_REF indices which will always be real operands
2075          (GIMPLE does not allow non-registers as array indices).  */
2076       flags |= opf_no_vops;
2077       get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
2078       return;
2079
2080     case SSA_NAME:
2081     case STRUCT_FIELD_TAG:
2082     case SYMBOL_MEMORY_TAG:
2083     case NAME_MEMORY_TAG:
2084      add_stmt_operand (expr_p, s_ann, flags);
2085      return;
2086
2087     case VAR_DECL:
2088     case PARM_DECL:
2089     case RESULT_DECL:
2090       {
2091         subvar_t svars;
2092         
2093         /* Add the subvars for a variable, if it has subvars, to DEFS
2094            or USES.  Otherwise, add the variable itself.  Whether it
2095            goes to USES or DEFS depends on the operand flags.  */
2096         if (var_can_have_subvars (expr)
2097             && (svars = get_subvars_for_var (expr)))
2098           {
2099             unsigned int i;
2100             tree subvar;
2101             for (i = 0; VEC_iterate (tree, svars, i, subvar); ++i)
2102               add_stmt_operand (&subvar, s_ann, flags);
2103           }
2104         else
2105           add_stmt_operand (expr_p, s_ann, flags);
2106
2107         return;
2108       }
2109
2110     case MISALIGNED_INDIRECT_REF:
2111       get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
2112       /* fall through */
2113
2114     case ALIGN_INDIRECT_REF:
2115     case INDIRECT_REF:
2116       get_indirect_ref_operands (stmt, expr, flags, expr, 0, -1, true);
2117       return;
2118
2119     case TARGET_MEM_REF:
2120       get_tmr_operands (stmt, expr, flags);
2121       return;
2122
2123     case ARRAY_REF:
2124     case ARRAY_RANGE_REF:
2125     case COMPONENT_REF:
2126     case REALPART_EXPR:
2127     case IMAGPART_EXPR:
2128       {
2129         tree ref;
2130         HOST_WIDE_INT offset, size, maxsize;
2131         bool none = true;
2132
2133         if (TREE_THIS_VOLATILE (expr))
2134           s_ann->has_volatile_ops = true;
2135
2136         /* This component reference becomes an access to all of the
2137            subvariables it can touch, if we can determine that, but
2138            *NOT* the real one.  If we can't determine which fields we
2139            could touch, the recursion will eventually get to a
2140            variable and add *all* of its subvars, or whatever is the
2141            minimum correct subset.  */
2142         ref = get_ref_base_and_extent (expr, &offset, &size, &maxsize);
2143         if (SSA_VAR_P (ref) && get_subvars_for_var (ref))
2144           {
2145             subvar_t svars = get_subvars_for_var (ref);
2146             unsigned int i;
2147             tree subvar;
2148
2149             for (i = 0; VEC_iterate (tree, svars, i, subvar); ++i)
2150               {
2151                 bool exact;             
2152
2153                 if (overlap_subvar (offset, maxsize, subvar, &exact))
2154                   {
2155                     int subvar_flags = flags;
2156                     none = false;
2157                     add_stmt_operand (&subvar, s_ann, subvar_flags);
2158                   }
2159               }
2160
2161             if (!none)
2162               flags |= opf_no_vops;
2163
2164             if ((DECL_P (ref) && TREE_THIS_VOLATILE (ref))
2165                 || (TREE_CODE (ref) == SSA_NAME
2166                     && TREE_THIS_VOLATILE (SSA_NAME_VAR (ref))))
2167               s_ann->has_volatile_ops = true;
2168           }
2169         else if (TREE_CODE (ref) == INDIRECT_REF)
2170           {
2171             get_indirect_ref_operands (stmt, ref, flags, expr, offset,
2172                                        maxsize, false);
2173             flags |= opf_no_vops;
2174           }
2175
2176         /* Even if we found subvars above we need to ensure to see
2177            immediate uses for d in s.a[d].  In case of s.a having
2178            a subvar or we would miss it otherwise.  */
2179         get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
2180         
2181         if (code == COMPONENT_REF)
2182           {
2183             if (TREE_THIS_VOLATILE (TREE_OPERAND (expr, 1)))
2184               s_ann->has_volatile_ops = true; 
2185             get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_use);
2186           }
2187         else if (code == ARRAY_REF || code == ARRAY_RANGE_REF)
2188           {
2189             get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_use);
2190             get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_use);
2191             get_expr_operands (stmt, &TREE_OPERAND (expr, 3), opf_use);
2192           }
2193
2194         return;
2195       }
2196
2197     case WITH_SIZE_EXPR:
2198       /* WITH_SIZE_EXPR is a pass-through reference to its first argument,
2199          and an rvalue reference to its second argument.  */
2200       get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_use);
2201       get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
2202       return;
2203
2204     case CALL_EXPR:
2205       get_call_expr_operands (stmt, expr);
2206       return;
2207
2208     case COND_EXPR:
2209     case VEC_COND_EXPR:
2210       get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_use);
2211       get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_use);
2212       get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_use);
2213       return;
2214
2215     case GIMPLE_MODIFY_STMT:
2216       get_modify_stmt_operands (stmt, expr);
2217       return;
2218
2219     case CONSTRUCTOR:
2220       {
2221         /* General aggregate CONSTRUCTORs have been decomposed, but they
2222            are still in use as the COMPLEX_EXPR equivalent for vectors.  */
2223         constructor_elt *ce;
2224         unsigned HOST_WIDE_INT idx;
2225
2226         for (idx = 0;
2227              VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (expr), idx, ce);
2228              idx++)
2229           get_expr_operands (stmt, &ce->value, opf_use);
2230
2231         return;
2232       }
2233
2234     case BIT_FIELD_REF:
2235     case TRUTH_NOT_EXPR:
2236     case VIEW_CONVERT_EXPR:
2237     do_unary:
2238       get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
2239       return;
2240
2241     case TRUTH_AND_EXPR:
2242     case TRUTH_OR_EXPR:
2243     case TRUTH_XOR_EXPR:
2244     case COMPOUND_EXPR:
2245     case OBJ_TYPE_REF:
2246     case ASSERT_EXPR:
2247     do_binary:
2248       {
2249         get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
2250         get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
2251         return;
2252       }
2253
2254     case DOT_PROD_EXPR:
2255     case REALIGN_LOAD_EXPR:
2256       {
2257         get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
2258         get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
2259         get_expr_operands (stmt, &TREE_OPERAND (expr, 2), flags);
2260         return;
2261       }
2262
2263     case CHANGE_DYNAMIC_TYPE_EXPR:
2264       get_expr_operands (stmt, &CHANGE_DYNAMIC_TYPE_LOCATION (expr), opf_use);
2265       return;
2266
2267     case OMP_FOR:
2268       {
2269         tree init = OMP_FOR_INIT (expr);
2270         tree cond = OMP_FOR_COND (expr);
2271         tree incr = OMP_FOR_INCR (expr);
2272         tree c, clauses = OMP_FOR_CLAUSES (stmt);
2273
2274         get_expr_operands (stmt, &GIMPLE_STMT_OPERAND (init, 0), opf_def);
2275         get_expr_operands (stmt, &GIMPLE_STMT_OPERAND (init, 1), opf_use);
2276         get_expr_operands (stmt, &TREE_OPERAND (cond, 1), opf_use);
2277         get_expr_operands (stmt, &TREE_OPERAND (GIMPLE_STMT_OPERAND (incr, 1), 1),
2278                            opf_use);
2279
2280         c = find_omp_clause (clauses, OMP_CLAUSE_SCHEDULE);
2281         if (c)
2282           get_expr_operands (stmt, &OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c),
2283                              opf_use);
2284         return;
2285       }
2286
2287     case OMP_CONTINUE:
2288       {
2289         get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_def);
2290         get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_use);
2291         return;
2292       }
2293
2294     case OMP_PARALLEL:
2295       {
2296         tree c, clauses = OMP_PARALLEL_CLAUSES (stmt);
2297
2298         if (OMP_PARALLEL_DATA_ARG (stmt))
2299           {
2300             get_expr_operands (stmt, &OMP_PARALLEL_DATA_ARG (stmt), opf_use);
2301             add_to_addressable_set (OMP_PARALLEL_DATA_ARG (stmt),
2302                                     &s_ann->addresses_taken);
2303           }
2304
2305         c = find_omp_clause (clauses, OMP_CLAUSE_IF);
2306         if (c)
2307           get_expr_operands (stmt, &OMP_CLAUSE_IF_EXPR (c), opf_use);
2308         c = find_omp_clause (clauses, OMP_CLAUSE_NUM_THREADS);
2309         if (c)
2310           get_expr_operands (stmt, &OMP_CLAUSE_NUM_THREADS_EXPR (c), opf_use);
2311         return;
2312       }
2313
2314     case OMP_SECTIONS:
2315       {
2316         get_expr_operands (stmt, &OMP_SECTIONS_CONTROL (expr), opf_def);
2317         return;
2318       }
2319
2320     case OMP_ATOMIC_LOAD:
2321       {
2322                  tree *addr = &TREE_OPERAND (expr, 1);
2323                  get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_def);
2324
2325                  if (TREE_CODE (*addr) == ADDR_EXPR)
2326                    get_expr_operands (stmt, &TREE_OPERAND (*addr, 0), opf_def);
2327                  else
2328                    get_addr_dereference_operands (stmt, addr, opf_def,
2329                                                                                   NULL_TREE, 0, -1, true);
2330                  return;
2331       }
2332
2333     case OMP_ATOMIC_STORE:
2334       {
2335                  get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_use);
2336                  return;
2337       }
2338
2339     case BLOCK:
2340     case FUNCTION_DECL:
2341     case EXC_PTR_EXPR:
2342     case FILTER_EXPR:
2343     case LABEL_DECL:
2344     case CONST_DECL:
2345     case OMP_SINGLE:
2346     case OMP_MASTER:
2347     case OMP_ORDERED:
2348     case OMP_CRITICAL:
2349     case OMP_RETURN:
2350     case OMP_SECTION:
2351     case OMP_SECTIONS_SWITCH:
2352       /* Expressions that make no memory references.  */
2353       return;
2354
2355     default:
2356       if (codeclass == tcc_unary)
2357         goto do_unary;
2358       if (codeclass == tcc_binary || codeclass == tcc_comparison)
2359         goto do_binary;
2360       if (codeclass == tcc_constant || codeclass == tcc_type)
2361         return;
2362     }
2363
2364   /* If we get here, something has gone wrong.  */
2365 #ifdef ENABLE_CHECKING
2366   fprintf (stderr, "unhandled expression in get_expr_operands():\n");
2367   debug_tree (expr);
2368   fputs ("\n", stderr);
2369 #endif
2370   gcc_unreachable ();
2371 }
2372
2373
2374 /* Parse STMT looking for operands.  When finished, the various
2375    build_* operand vectors will have potential operands in them.  */
2376
2377 static void
2378 parse_ssa_operands (tree stmt)
2379 {
2380   enum tree_code code;
2381
2382   code = TREE_CODE (stmt);
2383   switch (code)
2384     {
2385     case GIMPLE_MODIFY_STMT:
2386       get_modify_stmt_operands (stmt, stmt);
2387       break;
2388
2389     case COND_EXPR:
2390       get_expr_operands (stmt, &COND_EXPR_COND (stmt), opf_use);
2391       break;
2392
2393     case SWITCH_EXPR:
2394       get_expr_operands (stmt, &SWITCH_COND (stmt), opf_use);
2395       break;
2396
2397     case ASM_EXPR:
2398       get_asm_expr_operands (stmt);
2399       break;
2400
2401     case RETURN_EXPR:
2402       get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), opf_use);
2403       break;
2404
2405     case GOTO_EXPR:
2406       get_expr_operands (stmt, &GOTO_DESTINATION (stmt), opf_use);
2407       break;
2408
2409     case LABEL_EXPR:
2410       get_expr_operands (stmt, &LABEL_EXPR_LABEL (stmt), opf_use);
2411       break;
2412
2413     case BIND_EXPR:
2414     case CASE_LABEL_EXPR:
2415     case TRY_CATCH_EXPR:
2416     case TRY_FINALLY_EXPR:
2417     case EH_FILTER_EXPR:
2418     case CATCH_EXPR:
2419     case RESX_EXPR:
2420       /* These nodes contain no variable references.  */
2421      break;
2422
2423     default:
2424       /* Notice that if get_expr_operands tries to use &STMT as the
2425          operand pointer (which may only happen for USE operands), we
2426          will fail in add_stmt_operand.  This default will handle
2427          statements like empty statements, or CALL_EXPRs that may
2428          appear on the RHS of a statement or as statements themselves.  */
2429       get_expr_operands (stmt, &stmt, opf_use);
2430       break;
2431     }
2432 }
2433
2434
2435 /* Create an operands cache for STMT.  */
2436
2437 static void
2438 build_ssa_operands (tree stmt)
2439 {
2440   stmt_ann_t ann = get_stmt_ann (stmt);
2441   
2442   /* Initially assume that the statement has no volatile operands and
2443      makes no memory references.  */
2444   ann->has_volatile_ops = false;
2445   ann->references_memory = false;
2446   /* Just clear the bitmap so we don't end up reallocating it over and over.  */
2447   if (ann->addresses_taken)
2448     bitmap_clear (ann->addresses_taken);
2449
2450   start_ssa_stmt_operands ();
2451   parse_ssa_operands (stmt);
2452   operand_build_sort_virtual (build_vuses);
2453   operand_build_sort_virtual (build_vdefs);
2454   finalize_ssa_stmt_operands (stmt);
2455
2456   if (ann->addresses_taken && bitmap_empty_p (ann->addresses_taken))
2457     ann->addresses_taken = NULL;
2458   /* For added safety, assume that statements with volatile operands
2459      also reference memory.  */
2460   if (ann->has_volatile_ops)
2461     ann->references_memory = true;
2462 }
2463
2464 /* Releases the operands of STMT back to their freelists, and clears
2465    the stmt operand lists.  */
2466
2467 void
2468 free_stmt_operands (tree stmt)
2469 {
2470   def_optype_p defs = DEF_OPS (stmt), last_def;
2471   use_optype_p uses = USE_OPS (stmt), last_use;
2472   voptype_p vuses = VUSE_OPS (stmt);
2473   voptype_p vdefs = VDEF_OPS (stmt), vdef, next_vdef;
2474   unsigned i;
2475
2476   if (defs)
2477     {
2478       for (last_def = defs; last_def->next; last_def = last_def->next)
2479         continue;
2480       last_def->next = gimple_ssa_operands (cfun)->free_defs;
2481       gimple_ssa_operands (cfun)->free_defs = defs;
2482       DEF_OPS (stmt) = NULL;
2483     }
2484
2485   if (uses)
2486     {
2487       for (last_use = uses; last_use->next; last_use = last_use->next)
2488         delink_imm_use (USE_OP_PTR (last_use));
2489       delink_imm_use (USE_OP_PTR (last_use));
2490       last_use->next = gimple_ssa_operands (cfun)->free_uses;
2491       gimple_ssa_operands (cfun)->free_uses = uses;
2492       USE_OPS (stmt) = NULL;
2493     }
2494
2495   if (vuses)
2496     {
2497       for (i = 0; i < VUSE_NUM (vuses); i++)
2498         delink_imm_use (VUSE_OP_PTR (vuses, i));
2499       add_vop_to_freelist (vuses);
2500       VUSE_OPS (stmt) = NULL;
2501     }
2502
2503   if (vdefs)
2504     {
2505       for (vdef = vdefs; vdef; vdef = next_vdef)
2506         {
2507           next_vdef = vdef->next;
2508           delink_imm_use (VDEF_OP_PTR (vdef, 0));
2509           add_vop_to_freelist (vdef);
2510         }
2511       VDEF_OPS (stmt) = NULL;
2512     }
2513 }
2514
2515 /* Free any operands vectors in OPS.  */
2516
2517 void 
2518 free_ssa_operands (stmt_operands_p ops)
2519 {
2520   ops->def_ops = NULL;
2521   ops->use_ops = NULL;
2522   ops->vdef_ops = NULL;
2523   ops->vuse_ops = NULL;
2524   BITMAP_FREE (ops->loads);
2525   BITMAP_FREE (ops->stores);
2526 }
2527
2528
2529 /* Get the operands of statement STMT.  */
2530
2531 void
2532 update_stmt_operands (tree stmt)
2533 {
2534   stmt_ann_t ann = get_stmt_ann (stmt);
2535
2536   /* If update_stmt_operands is called before SSA is initialized, do
2537      nothing.  */
2538   if (!ssa_operands_active ())
2539     return;
2540
2541   /* The optimizers cannot handle statements that are nothing but a
2542      _DECL.  This indicates a bug in the gimplifier.  */
2543   gcc_assert (!SSA_VAR_P (stmt));
2544
2545   timevar_push (TV_TREE_OPS);
2546
2547   gcc_assert (ann->modified);
2548   build_ssa_operands (stmt);
2549   ann->modified = 0;
2550
2551   timevar_pop (TV_TREE_OPS);
2552 }
2553
2554
2555 /* Copies virtual operands from SRC to DST.  */
2556
2557 void
2558 copy_virtual_operands (tree dest, tree src)
2559 {
2560   unsigned int i, n;
2561   voptype_p src_vuses, dest_vuses;
2562   voptype_p src_vdefs, dest_vdefs;
2563   struct voptype_d vuse;
2564   struct voptype_d vdef;
2565   stmt_ann_t dest_ann;
2566
2567   VDEF_OPS (dest) = NULL;
2568   VUSE_OPS (dest) = NULL;
2569
2570   dest_ann = get_stmt_ann (dest);
2571   BITMAP_FREE (dest_ann->operands.loads);
2572   BITMAP_FREE (dest_ann->operands.stores);
2573
2574   if (LOADED_SYMS (src))
2575     {
2576       dest_ann->operands.loads = BITMAP_ALLOC (&operands_bitmap_obstack);
2577       bitmap_copy (dest_ann->operands.loads, LOADED_SYMS (src));
2578     }
2579
2580   if (STORED_SYMS (src))
2581     {
2582       dest_ann->operands.stores = BITMAP_ALLOC (&operands_bitmap_obstack);
2583       bitmap_copy (dest_ann->operands.stores, STORED_SYMS (src));
2584     }
2585
2586   /* Copy all the VUSE operators and corresponding operands.  */
2587   dest_vuses = &vuse;
2588   for (src_vuses = VUSE_OPS (src); src_vuses; src_vuses = src_vuses->next)
2589     {
2590       n = VUSE_NUM (src_vuses);
2591       dest_vuses = add_vuse_op (dest, NULL_TREE, n, dest_vuses);
2592       for (i = 0; i < n; i++)
2593         SET_USE (VUSE_OP_PTR (dest_vuses, i), VUSE_OP (src_vuses, i));
2594
2595       if (VUSE_OPS (dest) == NULL)
2596         VUSE_OPS (dest) = vuse.next;
2597     }
2598
2599   /* Copy all the VDEF operators and corresponding operands.  */
2600   dest_vdefs = &vdef;
2601   for (src_vdefs = VDEF_OPS (src); src_vdefs; src_vdefs = src_vdefs->next)
2602     {
2603       n = VUSE_NUM (src_vdefs);
2604       dest_vdefs = add_vdef_op (dest, NULL_TREE, n, dest_vdefs);
2605       VDEF_RESULT (dest_vdefs) = VDEF_RESULT (src_vdefs);
2606       for (i = 0; i < n; i++)
2607         SET_USE (VUSE_OP_PTR (dest_vdefs, i), VUSE_OP (src_vdefs, i));
2608
2609       if (VDEF_OPS (dest) == NULL)
2610         VDEF_OPS (dest) = vdef.next;
2611     }
2612 }
2613
2614
2615 /* Specifically for use in DOM's expression analysis.  Given a store, we
2616    create an artificial stmt which looks like a load from the store, this can
2617    be used to eliminate redundant loads.  OLD_OPS are the operands from the 
2618    store stmt, and NEW_STMT is the new load which represents a load of the
2619    values stored.  */
2620
2621 void
2622 create_ssa_artificial_load_stmt (tree new_stmt, tree old_stmt)
2623 {
2624   tree op;
2625   ssa_op_iter iter;
2626   use_operand_p use_p;
2627   unsigned i;
2628
2629   get_stmt_ann (new_stmt);
2630
2631   /* Process NEW_STMT looking for operands.  */
2632   start_ssa_stmt_operands ();
2633   parse_ssa_operands (new_stmt);
2634
2635   for (i = 0; VEC_iterate (tree, build_vuses, i, op); i++)
2636     if (TREE_CODE (op) != SSA_NAME)
2637       var_ann (op)->in_vuse_list = false;
2638    
2639   for (i = 0; VEC_iterate (tree, build_vuses, i, op); i++)
2640     if (TREE_CODE (op) != SSA_NAME)
2641       var_ann (op)->in_vdef_list = false;
2642
2643   /* Remove any virtual operands that were found.  */
2644   VEC_truncate (tree, build_vdefs, 0);
2645   VEC_truncate (tree, build_vuses, 0);
2646
2647   /* For each VDEF on the original statement, we want to create a
2648      VUSE of the VDEF result operand on the new statement.  */
2649   FOR_EACH_SSA_TREE_OPERAND (op, old_stmt, iter, SSA_OP_VDEF)
2650     append_vuse (op);
2651
2652   finalize_ssa_stmt_operands (new_stmt);
2653
2654   /* All uses in this fake stmt must not be in the immediate use lists.  */
2655   FOR_EACH_SSA_USE_OPERAND (use_p, new_stmt, iter, SSA_OP_ALL_USES)
2656     delink_imm_use (use_p);
2657 }
2658
2659
2660 /* Swap operands EXP0 and EXP1 in statement STMT.  No attempt is done
2661    to test the validity of the swap operation.  */
2662
2663 void
2664 swap_tree_operands (tree stmt, tree *exp0, tree *exp1)
2665 {
2666   tree op0, op1;
2667   op0 = *exp0;
2668   op1 = *exp1;
2669
2670   /* If the operand cache is active, attempt to preserve the relative
2671      positions of these two operands in their respective immediate use
2672      lists.  */
2673   if (ssa_operands_active () && op0 != op1)
2674     {
2675       use_optype_p use0, use1, ptr;
2676       use0 = use1 = NULL;
2677
2678       /* Find the 2 operands in the cache, if they are there.  */
2679       for (ptr = USE_OPS (stmt); ptr; ptr = ptr->next)
2680         if (USE_OP_PTR (ptr)->use == exp0)
2681           {
2682             use0 = ptr;
2683             break;
2684           }
2685
2686       for (ptr = USE_OPS (stmt); ptr; ptr = ptr->next)
2687         if (USE_OP_PTR (ptr)->use == exp1)
2688           {
2689             use1 = ptr;
2690             break;
2691           }
2692
2693       /* If both uses don't have operand entries, there isn't much we can do
2694          at this point.  Presumably we don't need to worry about it.  */
2695       if (use0 && use1)
2696         {
2697           tree *tmp = USE_OP_PTR (use1)->use;
2698           USE_OP_PTR (use1)->use = USE_OP_PTR (use0)->use;
2699           USE_OP_PTR (use0)->use = tmp;
2700         }
2701     }
2702
2703   /* Now swap the data.  */
2704   *exp0 = op1;
2705   *exp1 = op0;
2706 }
2707
2708
2709 /* Add the base address of REF to the set *ADDRESSES_TAKEN.  If
2710    *ADDRESSES_TAKEN is NULL, a new set is created.  REF may be
2711    a single variable whose address has been taken or any other valid
2712    GIMPLE memory reference (structure reference, array, etc).  If the
2713    base address of REF is a decl that has sub-variables, also add all
2714    of its sub-variables.  */
2715
2716 void
2717 add_to_addressable_set (tree ref, bitmap *addresses_taken)
2718 {
2719   tree var;
2720   subvar_t svars;
2721
2722   gcc_assert (addresses_taken);
2723
2724   /* Note that it is *NOT OKAY* to use the target of a COMPONENT_REF
2725      as the only thing we take the address of.  If VAR is a structure,
2726      taking the address of a field means that the whole structure may
2727      be referenced using pointer arithmetic.  See PR 21407 and the
2728      ensuing mailing list discussion.  */
2729   var = get_base_address (ref);
2730   if (var && SSA_VAR_P (var))
2731     {
2732       if (*addresses_taken == NULL)
2733         *addresses_taken = BITMAP_GGC_ALLOC ();      
2734       
2735       if (var_can_have_subvars (var)
2736           && (svars = get_subvars_for_var (var)))
2737         {
2738           unsigned int i;
2739           tree subvar;
2740           for (i = 0; VEC_iterate (tree, svars, i, subvar); ++i)
2741             {
2742               bitmap_set_bit (*addresses_taken, DECL_UID (subvar));
2743               TREE_ADDRESSABLE (subvar) = 1;
2744             }
2745         }
2746       else
2747         {
2748           bitmap_set_bit (*addresses_taken, DECL_UID (var));
2749           TREE_ADDRESSABLE (var) = 1;
2750         }
2751     }
2752 }
2753
2754
2755 /* Scan the immediate_use list for VAR making sure its linked properly.
2756    Return TRUE if there is a problem and emit an error message to F.  */
2757
2758 bool
2759 verify_imm_links (FILE *f, tree var)
2760 {
2761   use_operand_p ptr, prev, list;
2762   int count;
2763
2764   gcc_assert (TREE_CODE (var) == SSA_NAME);
2765
2766   list = &(SSA_NAME_IMM_USE_NODE (var));
2767   gcc_assert (list->use == NULL);
2768
2769   if (list->prev == NULL)
2770     {
2771       gcc_assert (list->next == NULL);
2772       return false;
2773     }
2774
2775   prev = list;
2776   count = 0;
2777   for (ptr = list->next; ptr != list; )
2778     {
2779       if (prev != ptr->prev)
2780         goto error;
2781       
2782       if (ptr->use == NULL)
2783         goto error; /* 2 roots, or SAFE guard node.  */
2784       else if (*(ptr->use) != var)
2785         goto error;
2786
2787       prev = ptr;
2788       ptr = ptr->next;
2789
2790       /* Avoid infinite loops.  50,000,000 uses probably indicates a
2791          problem.  */
2792       if (count++ > 50000000)
2793         goto error;
2794     }
2795
2796   /* Verify list in the other direction.  */
2797   prev = list;
2798   for (ptr = list->prev; ptr != list; )
2799     {
2800       if (prev != ptr->next)
2801         goto error;
2802       prev = ptr;
2803       ptr = ptr->prev;
2804       if (count-- < 0)
2805         goto error;
2806     }
2807
2808   if (count != 0)
2809     goto error;
2810
2811   return false;
2812
2813  error:
2814   if (ptr->stmt && stmt_modified_p (ptr->stmt))
2815     {
2816       fprintf (f, " STMT MODIFIED. - <%p> ", (void *)ptr->stmt);
2817       print_generic_stmt (f, ptr->stmt, TDF_SLIM);
2818     }
2819   fprintf (f, " IMM ERROR : (use_p : tree - %p:%p)", (void *)ptr, 
2820            (void *)ptr->use);
2821   print_generic_expr (f, USE_FROM_PTR (ptr), TDF_SLIM);
2822   fprintf(f, "\n");
2823   return true;
2824 }
2825
2826
2827 /* Dump all the immediate uses to FILE.  */
2828
2829 void
2830 dump_immediate_uses_for (FILE *file, tree var)
2831 {
2832   imm_use_iterator iter;
2833   use_operand_p use_p;
2834
2835   gcc_assert (var && TREE_CODE (var) == SSA_NAME);
2836
2837   print_generic_expr (file, var, TDF_SLIM);
2838   fprintf (file, " : -->");
2839   if (has_zero_uses (var))
2840     fprintf (file, " no uses.\n");
2841   else
2842     if (has_single_use (var))
2843       fprintf (file, " single use.\n");
2844     else
2845       fprintf (file, "%d uses.\n", num_imm_uses (var));
2846
2847   FOR_EACH_IMM_USE_FAST (use_p, iter, var)
2848     {
2849       if (use_p->stmt == NULL && use_p->use == NULL)
2850         fprintf (file, "***end of stmt iterator marker***\n");
2851       else
2852         if (!is_gimple_reg (USE_FROM_PTR (use_p)))
2853           print_generic_stmt (file, USE_STMT (use_p), TDF_VOPS|TDF_MEMSYMS);
2854         else
2855           print_generic_stmt (file, USE_STMT (use_p), TDF_SLIM);
2856     }
2857   fprintf(file, "\n");
2858 }
2859
2860
2861 /* Dump all the immediate uses to FILE.  */
2862
2863 void
2864 dump_immediate_uses (FILE *file)
2865 {
2866   tree var;
2867   unsigned int x;
2868
2869   fprintf (file, "Immediate_uses: \n\n");
2870   for (x = 1; x < num_ssa_names; x++)
2871     {
2872       var = ssa_name(x);
2873       if (!var)
2874         continue;
2875       dump_immediate_uses_for (file, var);
2876     }
2877 }
2878
2879
2880 /* Dump def-use edges on stderr.  */
2881
2882 void
2883 debug_immediate_uses (void)
2884 {
2885   dump_immediate_uses (stderr);
2886 }
2887
2888
2889 /* Dump def-use edges on stderr.  */
2890
2891 void
2892 debug_immediate_uses_for (tree var)
2893 {
2894   dump_immediate_uses_for (stderr, var);
2895 }
2896
2897
2898 /* Create a new change buffer for the statement pointed by STMT_P and
2899    push the buffer into SCB_STACK.  Each change buffer
2900    records state information needed to determine what changed in the
2901    statement.  Mainly, this keeps track of symbols that may need to be
2902    put into SSA form, SSA name replacements and other information
2903    needed to keep the SSA form up to date.  */
2904
2905 void
2906 push_stmt_changes (tree *stmt_p)
2907 {
2908   tree stmt;
2909   scb_t buf;
2910   
2911   stmt = *stmt_p;
2912
2913   /* It makes no sense to keep track of PHI nodes.  */
2914   if (TREE_CODE (stmt) == PHI_NODE)
2915     return;
2916
2917   buf = XNEW (struct scb_d);
2918   memset (buf, 0, sizeof *buf);
2919
2920   buf->stmt_p = stmt_p;
2921
2922   if (stmt_references_memory_p (stmt))
2923     {
2924       tree op;
2925       ssa_op_iter i;
2926
2927       FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_VUSE)
2928         {
2929           tree sym = TREE_CODE (op) == SSA_NAME ? SSA_NAME_VAR (op) : op;
2930           if (buf->loads == NULL)
2931             buf->loads = BITMAP_ALLOC (NULL);
2932           bitmap_set_bit (buf->loads, DECL_UID (sym));
2933         }
2934
2935       FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_VDEF)
2936         {
2937           tree sym = TREE_CODE (op) == SSA_NAME ? SSA_NAME_VAR (op) : op;
2938           if (buf->stores == NULL)
2939             buf->stores = BITMAP_ALLOC (NULL);
2940           bitmap_set_bit (buf->stores, DECL_UID (sym));
2941         }
2942     }
2943
2944   VEC_safe_push (scb_t, heap, scb_stack, buf);
2945 }
2946
2947
2948 /* Given two sets S1 and S2, mark the symbols that differ in S1 and S2
2949    for renaming.  The set to mark for renaming is (S1 & ~S2) | (S2 & ~S1).  */
2950
2951 static void
2952 mark_difference_for_renaming (bitmap s1, bitmap s2)
2953 {
2954   if (s1 == NULL && s2 == NULL)
2955     return;
2956
2957   if (s1 && s2 == NULL)
2958     mark_set_for_renaming (s1);
2959   else if (s1 == NULL && s2)
2960     mark_set_for_renaming (s2);
2961   else if (!bitmap_equal_p (s1, s2))
2962     {
2963       bitmap t1 = BITMAP_ALLOC (NULL);
2964       bitmap t2 = BITMAP_ALLOC (NULL);
2965
2966       bitmap_and_compl (t1, s1, s2);
2967       bitmap_and_compl (t2, s2, s1);
2968       bitmap_ior_into (t1, t2);
2969       mark_set_for_renaming (t1);
2970
2971       BITMAP_FREE (t1);
2972       BITMAP_FREE (t2);
2973     }
2974 }
2975
2976
2977 /* Pop the top SCB from SCB_STACK and act on the differences between
2978    what was recorded by push_stmt_changes and the current state of
2979    the statement.  */
2980
2981 void
2982 pop_stmt_changes (tree *stmt_p)
2983 {
2984   tree op, stmt;
2985   ssa_op_iter iter;
2986   bitmap loads, stores;
2987   scb_t buf;
2988
2989   stmt = *stmt_p;
2990
2991   /* It makes no sense to keep track of PHI nodes.  */
2992   if (TREE_CODE (stmt) == PHI_NODE)
2993     return;
2994
2995   buf = VEC_pop (scb_t, scb_stack);
2996   gcc_assert (stmt_p == buf->stmt_p);
2997
2998   /* Force an operand re-scan on the statement and mark any newly
2999      exposed variables.  */
3000   update_stmt (stmt);
3001
3002   /* Determine whether any memory symbols need to be renamed.  If the
3003      sets of loads and stores are different after the statement is
3004      modified, then the affected symbols need to be renamed.
3005      
3006      Note that it may be possible for the statement to not reference
3007      memory anymore, but we still need to act on the differences in
3008      the sets of symbols.  */
3009   loads = stores = NULL;
3010   if (stmt_references_memory_p (stmt))
3011     {
3012       tree op;
3013       ssa_op_iter i;
3014
3015       FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_VUSE)
3016         {
3017           tree sym = TREE_CODE (op) == SSA_NAME ? SSA_NAME_VAR (op) : op;
3018           if (loads == NULL)
3019             loads = BITMAP_ALLOC (NULL);
3020           bitmap_set_bit (loads, DECL_UID (sym));
3021         }
3022
3023       FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_VDEF)
3024         {
3025           tree sym = TREE_CODE (op) == SSA_NAME ? SSA_NAME_VAR (op) : op;
3026           if (stores == NULL)
3027             stores = BITMAP_ALLOC (NULL);
3028           bitmap_set_bit (stores, DECL_UID (sym));
3029         }
3030     }
3031
3032   /* If LOADS is different from BUF->LOADS, the affected
3033      symbols need to be marked for renaming.  */
3034   mark_difference_for_renaming (loads, buf->loads);
3035
3036   /* Similarly for STORES and BUF->STORES.  */
3037   mark_difference_for_renaming (stores, buf->stores);
3038
3039   /* Mark all the naked GIMPLE register operands for renaming.  */
3040   FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF|SSA_OP_USE)
3041     if (DECL_P (op))
3042       mark_sym_for_renaming (op);
3043
3044   /* FIXME, need to add more finalizers here.  Cleanup EH info,
3045      recompute invariants for address expressions, add
3046      SSA replacement mappings, etc.  For instance, given
3047      testsuite/gcc.c-torture/compile/pr16808.c, we fold a statement of
3048      the form:
3049
3050           # SMT.4_20 = VDEF <SMT.4_16>
3051           D.1576_11 = 1.0e+0;
3052
3053      So, the VDEF will disappear, but instead of marking SMT.4 for
3054      renaming it would be far more efficient to establish a
3055      replacement mapping that would replace every reference of
3056      SMT.4_20 with SMT.4_16.  */
3057
3058   /* Free memory used by the buffer.  */
3059   BITMAP_FREE (buf->loads);
3060   BITMAP_FREE (buf->stores);
3061   BITMAP_FREE (loads);
3062   BITMAP_FREE (stores);
3063   buf->stmt_p = NULL;
3064   free (buf);
3065 }
3066
3067
3068 /* Discard the topmost change buffer from SCB_STACK.  This is useful
3069    when the caller realized that it did not actually modified the
3070    statement.  It avoids the expensive operand re-scan.  */
3071
3072 void
3073 discard_stmt_changes (tree *stmt_p)
3074 {
3075   scb_t buf;
3076   tree stmt;
3077   
3078   /* It makes no sense to keep track of PHI nodes.  */
3079   stmt = *stmt_p;
3080   if (TREE_CODE (stmt) == PHI_NODE)
3081     return;
3082
3083   buf = VEC_pop (scb_t, scb_stack);
3084   gcc_assert (stmt_p == buf->stmt_p);
3085
3086   /* Free memory used by the buffer.  */
3087   BITMAP_FREE (buf->loads);
3088   BITMAP_FREE (buf->stores);
3089   buf->stmt_p = NULL;
3090   free (buf);
3091 }
3092
3093
3094 /* Returns true if statement STMT may access memory.  */
3095
3096 bool
3097 stmt_references_memory_p (tree stmt)
3098 {
3099   if (!gimple_ssa_operands (cfun)->ops_active || TREE_CODE (stmt) == PHI_NODE)
3100     return false;
3101
3102   return stmt_ann (stmt)->references_memory;
3103 }