OSDN Git Service

PR rtl-optimization/50663
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-operands.c
1 /* SSA operands management for trees.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "function.h"
28 #include "tree-pretty-print.h"
29 #include "gimple-pretty-print.h"
30 #include "tree-flow.h"
31 #include "tree-inline.h"
32 #include "tree-pass.h"
33 #include "ggc.h"
34 #include "timevar.h"
35 #include "langhooks.h"
36 #include "ipa-reference.h"
37
38 /* This file contains the code required to manage the operands cache of the
39    SSA optimizer.  For every stmt, we maintain an operand cache in the stmt
40    annotation.  This cache contains operands that will be of interest to
41    optimizers and other passes wishing to manipulate the IL.
42
43    The operand type are broken up into REAL and VIRTUAL operands.  The real
44    operands are represented as pointers into the stmt's operand tree.  Thus
45    any manipulation of the real operands will be reflected in the actual tree.
46    Virtual operands are represented solely in the cache, although the base
47    variable for the SSA_NAME may, or may not occur in the stmt's tree.
48    Manipulation of the virtual operands will not be reflected in the stmt tree.
49
50    The routines in this file are concerned with creating this operand cache
51    from a stmt tree.
52
53    The operand tree is the parsed by the various get_* routines which look
54    through the stmt tree for the occurrence of operands which may be of
55    interest, and calls are made to the append_* routines whenever one is
56    found.  There are 4 of these routines, each representing one of the
57    4 types of operands. Defs, Uses, Virtual Uses, and Virtual May Defs.
58
59    The append_* routines check for duplication, and simply keep a list of
60    unique objects for each operand type in the build_* extendable vectors.
61
62    Once the stmt tree is completely parsed, the finalize_ssa_operands()
63    routine is called, which proceeds to perform the finalization routine
64    on each of the 4 operand vectors which have been built up.
65
66    If the stmt had a previous operand cache, the finalization routines
67    attempt to match up the new operands with the old ones.  If it's a perfect
68    match, the old vector is simply reused.  If it isn't a perfect match, then
69    a new vector is created and the new operands are placed there.  For
70    virtual operands, if the previous cache had SSA_NAME version of a
71    variable, and that same variable occurs in the same operands cache, then
72    the new cache vector will also get the same SSA_NAME.
73
74    i.e., if a stmt had a VUSE of 'a_5', and 'a' occurs in the new
75    operand vector for VUSE, then the new vector will also be modified
76    such that it contains 'a_5' rather than 'a'.  */
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 MODIFY_EXPR from
126    clobbering sites like function calls or ASM_EXPRs.  */
127 #define opf_implicit    (1 << 2)
128
129 /* Operand is in a place where address-taken does not imply addressable.  */
130 #define opf_non_addressable (1 << 3)
131
132 /* Operand is in a place where opf_non_addressable does not apply.  */
133 #define opf_not_non_addressable (1 << 4)
134
135 /* Array for building all the def operands.  */
136 static VEC(tree,heap) *build_defs;
137
138 /* Array for building all the use operands.  */
139 static VEC(tree,heap) *build_uses;
140
141 /* The built VDEF operand.  */
142 static tree build_vdef;
143
144 /* The built VUSE operand.  */
145 static tree build_vuse;
146
147 /* Bitmap obstack for our datastructures that needs to survive across
148    compilations of multiple functions.  */
149 static bitmap_obstack operands_bitmap_obstack;
150
151 static void get_expr_operands (gimple, tree *, int);
152
153 /* Number of functions with initialized ssa_operands.  */
154 static int n_initialized = 0;
155
156 /* Return the DECL_UID of the base variable of T.  */
157
158 static inline unsigned
159 get_name_decl (const_tree t)
160 {
161   if (TREE_CODE (t) != SSA_NAME)
162     return DECL_UID (t);
163   else
164     return DECL_UID (SSA_NAME_VAR (t));
165 }
166
167
168 /*  Return true if the SSA operands cache is active.  */
169
170 bool
171 ssa_operands_active (void)
172 {
173   /* This function may be invoked from contexts where CFUN is NULL
174      (IPA passes), return false for now.  FIXME: operands may be
175      active in each individual function, maybe this function should
176      take CFUN as a parameter.  */
177   if (cfun == NULL)
178     return false;
179
180   return cfun->gimple_df && gimple_ssa_operands (cfun)->ops_active;
181 }
182
183
184 /* Create the VOP variable, an artificial global variable to act as a
185    representative of all of the virtual operands FUD chain.  */
186
187 static void
188 create_vop_var (void)
189 {
190   tree global_var;
191
192   gcc_assert (cfun->gimple_df->vop == NULL_TREE);
193
194   global_var = build_decl (BUILTINS_LOCATION, VAR_DECL,
195                            get_identifier (".MEM"),
196                            void_type_node);
197   DECL_ARTIFICIAL (global_var) = 1;
198   TREE_READONLY (global_var) = 0;
199   DECL_EXTERNAL (global_var) = 1;
200   TREE_STATIC (global_var) = 1;
201   TREE_USED (global_var) = 1;
202   DECL_CONTEXT (global_var) = NULL_TREE;
203   TREE_THIS_VOLATILE (global_var) = 0;
204   TREE_ADDRESSABLE (global_var) = 0;
205
206   create_var_ann (global_var);
207   add_referenced_var (global_var);
208   cfun->gimple_df->vop = global_var;
209 }
210
211 /* These are the sizes of the operand memory buffer in bytes which gets
212    allocated each time more operands space is required.  The final value is
213    the amount that is allocated every time after that.
214    In 1k we can fit 25 use operands (or 63 def operands) on a host with
215    8 byte pointers, that would be 10 statements each with 1 def and 2
216    uses.  */
217
218 #define OP_SIZE_INIT    0
219 #define OP_SIZE_1       (1024 - sizeof (void *))
220 #define OP_SIZE_2       (1024 * 4 - sizeof (void *))
221 #define OP_SIZE_3       (1024 * 16 - sizeof (void *))
222
223 /* Initialize the operand cache routines.  */
224
225 void
226 init_ssa_operands (void)
227 {
228   if (!n_initialized++)
229     {
230       build_defs = VEC_alloc (tree, heap, 5);
231       build_uses = VEC_alloc (tree, heap, 10);
232       build_vuse = NULL_TREE;
233       build_vdef = NULL_TREE;
234       bitmap_obstack_initialize (&operands_bitmap_obstack);
235     }
236
237   gcc_assert (gimple_ssa_operands (cfun)->operand_memory == NULL);
238   gimple_ssa_operands (cfun)->operand_memory_index
239      = gimple_ssa_operands (cfun)->ssa_operand_mem_size;
240   gimple_ssa_operands (cfun)->ops_active = true;
241   memset (&clobber_stats, 0, sizeof (clobber_stats));
242   gimple_ssa_operands (cfun)->ssa_operand_mem_size = OP_SIZE_INIT;
243   create_vop_var ();
244 }
245
246
247 /* Dispose of anything required by the operand routines.  */
248
249 void
250 fini_ssa_operands (void)
251 {
252   struct ssa_operand_memory_d *ptr;
253
254   if (!--n_initialized)
255     {
256       VEC_free (tree, heap, build_defs);
257       VEC_free (tree, heap, build_uses);
258       build_vdef = NULL_TREE;
259       build_vuse = NULL_TREE;
260     }
261
262   gimple_ssa_operands (cfun)->free_defs = NULL;
263   gimple_ssa_operands (cfun)->free_uses = NULL;
264
265   while ((ptr = gimple_ssa_operands (cfun)->operand_memory) != NULL)
266     {
267       gimple_ssa_operands (cfun)->operand_memory
268         = gimple_ssa_operands (cfun)->operand_memory->next;
269       ggc_free (ptr);
270     }
271
272   gimple_ssa_operands (cfun)->ops_active = false;
273
274   if (!n_initialized)
275     bitmap_obstack_release (&operands_bitmap_obstack);
276
277   cfun->gimple_df->vop = NULL_TREE;
278
279   if (dump_file && (dump_flags & TDF_STATS))
280     {
281       fprintf (dump_file, "Original clobbered vars:           %d\n",
282                clobber_stats.clobbered_vars);
283       fprintf (dump_file, "Static write clobbers avoided:     %d\n",
284                clobber_stats.static_write_clobbers_avoided);
285       fprintf (dump_file, "Static read clobbers avoided:      %d\n",
286                clobber_stats.static_read_clobbers_avoided);
287       fprintf (dump_file, "Unescapable clobbers avoided:      %d\n",
288                clobber_stats.unescapable_clobbers_avoided);
289       fprintf (dump_file, "Original read-only clobbers:       %d\n",
290                clobber_stats.readonly_clobbers);
291       fprintf (dump_file, "Static read-only clobbers avoided: %d\n",
292                clobber_stats.static_readonly_clobbers_avoided);
293     }
294 }
295
296
297 /* Return memory for an operand of size SIZE.  */
298
299 static inline void *
300 ssa_operand_alloc (unsigned size)
301 {
302   char *ptr;
303
304   gcc_assert (size == sizeof (struct use_optype_d)
305               || size == sizeof (struct def_optype_d));
306
307   if (gimple_ssa_operands (cfun)->operand_memory_index + size
308       >= gimple_ssa_operands (cfun)->ssa_operand_mem_size)
309     {
310       struct ssa_operand_memory_d *ptr;
311
312       switch (gimple_ssa_operands (cfun)->ssa_operand_mem_size)
313         {
314         case OP_SIZE_INIT:
315           gimple_ssa_operands (cfun)->ssa_operand_mem_size = OP_SIZE_1;
316           break;
317         case OP_SIZE_1:
318           gimple_ssa_operands (cfun)->ssa_operand_mem_size = OP_SIZE_2;
319           break;
320         case OP_SIZE_2:
321         case OP_SIZE_3:
322           gimple_ssa_operands (cfun)->ssa_operand_mem_size = OP_SIZE_3;
323           break;
324         default:
325           gcc_unreachable ();
326         }
327
328
329       ptr = ggc_alloc_ssa_operand_memory_d (sizeof (void *)
330                         + gimple_ssa_operands (cfun)->ssa_operand_mem_size);
331
332       ptr->next = gimple_ssa_operands (cfun)->operand_memory;
333       gimple_ssa_operands (cfun)->operand_memory = ptr;
334       gimple_ssa_operands (cfun)->operand_memory_index = 0;
335     }
336
337   ptr = &(gimple_ssa_operands (cfun)->operand_memory
338           ->mem[gimple_ssa_operands (cfun)->operand_memory_index]);
339   gimple_ssa_operands (cfun)->operand_memory_index += size;
340   return ptr;
341 }
342
343
344 /* Allocate a DEF operand.  */
345
346 static inline struct def_optype_d *
347 alloc_def (void)
348 {
349   struct def_optype_d *ret;
350   if (gimple_ssa_operands (cfun)->free_defs)
351     {
352       ret = gimple_ssa_operands (cfun)->free_defs;
353       gimple_ssa_operands (cfun)->free_defs
354         = gimple_ssa_operands (cfun)->free_defs->next;
355     }
356   else
357     ret = (struct def_optype_d *)
358           ssa_operand_alloc (sizeof (struct def_optype_d));
359   return ret;
360 }
361
362
363 /* Allocate a USE operand.  */
364
365 static inline struct use_optype_d *
366 alloc_use (void)
367 {
368   struct use_optype_d *ret;
369   if (gimple_ssa_operands (cfun)->free_uses)
370     {
371       ret = gimple_ssa_operands (cfun)->free_uses;
372       gimple_ssa_operands (cfun)->free_uses
373         = gimple_ssa_operands (cfun)->free_uses->next;
374     }
375   else
376     ret = (struct use_optype_d *)
377           ssa_operand_alloc (sizeof (struct use_optype_d));
378   return ret;
379 }
380
381
382 /* Adds OP to the list of defs after LAST.  */
383
384 static inline def_optype_p
385 add_def_op (tree *op, def_optype_p last)
386 {
387   def_optype_p new_def;
388
389   new_def = alloc_def ();
390   DEF_OP_PTR (new_def) = op;
391   last->next = new_def;
392   new_def->next = NULL;
393   return new_def;
394 }
395
396
397 /* Adds OP to the list of uses of statement STMT after LAST.  */
398
399 static inline use_optype_p
400 add_use_op (gimple stmt, tree *op, use_optype_p last)
401 {
402   use_optype_p new_use;
403
404   new_use = alloc_use ();
405   USE_OP_PTR (new_use)->use = op;
406   link_imm_use_stmt (USE_OP_PTR (new_use), *op, stmt);
407   last->next = new_use;
408   new_use->next = NULL;
409   return new_use;
410 }
411
412
413
414 /* Takes elements from build_defs and turns them into def operands of STMT.
415    TODO -- Make build_defs VEC of tree *.  */
416
417 static inline void
418 finalize_ssa_defs (gimple stmt)
419 {
420   unsigned new_i;
421   struct def_optype_d new_list;
422   def_optype_p old_ops, last;
423   unsigned int num = VEC_length (tree, build_defs);
424
425   /* There should only be a single real definition per assignment.  */
426   gcc_assert ((stmt && gimple_code (stmt) != GIMPLE_ASSIGN) || num <= 1);
427
428   /* Pre-pend the vdef we may have built.  */
429   if (build_vdef != NULL_TREE)
430     {
431       tree oldvdef = gimple_vdef (stmt);
432       if (oldvdef
433           && TREE_CODE (oldvdef) == SSA_NAME)
434         oldvdef = SSA_NAME_VAR (oldvdef);
435       if (oldvdef != build_vdef)
436         gimple_set_vdef (stmt, build_vdef);
437       VEC_safe_insert (tree, heap, build_defs, 0, (tree)gimple_vdef_ptr (stmt));
438       ++num;
439     }
440
441   new_list.next = NULL;
442   last = &new_list;
443
444   old_ops = gimple_def_ops (stmt);
445
446   new_i = 0;
447
448   /* Clear and unlink a no longer necessary VDEF.  */
449   if (build_vdef == NULL_TREE
450       && gimple_vdef (stmt) != NULL_TREE)
451     {
452       if (TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
453         {
454           unlink_stmt_vdef (stmt);
455           release_ssa_name (gimple_vdef (stmt));
456         }
457       gimple_set_vdef (stmt, NULL_TREE);
458     }
459
460   /* If we have a non-SSA_NAME VDEF, mark it for renaming.  */
461   if (gimple_vdef (stmt)
462       && TREE_CODE (gimple_vdef (stmt)) != SSA_NAME)
463     mark_sym_for_renaming (gimple_vdef (stmt));
464
465   /* Check for the common case of 1 def that hasn't changed.  */
466   if (old_ops && old_ops->next == NULL && num == 1
467       && (tree *) VEC_index (tree, build_defs, 0) == DEF_OP_PTR (old_ops))
468     return;
469
470   /* If there is anything in the old list, free it.  */
471   if (old_ops)
472     {
473       old_ops->next = gimple_ssa_operands (cfun)->free_defs;
474       gimple_ssa_operands (cfun)->free_defs = old_ops;
475     }
476
477   /* If there is anything remaining in the build_defs list, simply emit it.  */
478   for ( ; new_i < num; new_i++)
479     last = add_def_op ((tree *) VEC_index (tree, build_defs, new_i), last);
480
481   /* Now set the stmt's operands.  */
482   gimple_set_def_ops (stmt, new_list.next);
483 }
484
485
486 /* Takes elements from build_uses and turns them into use operands of STMT.
487    TODO -- Make build_uses VEC of tree *.  */
488
489 static inline void
490 finalize_ssa_uses (gimple stmt)
491 {
492   unsigned new_i;
493   struct use_optype_d new_list;
494   use_optype_p old_ops, ptr, last;
495
496   /* Pre-pend the VUSE we may have built.  */
497   if (build_vuse != NULL_TREE)
498     {
499       tree oldvuse = gimple_vuse (stmt);
500       if (oldvuse
501           && TREE_CODE (oldvuse) == SSA_NAME)
502         oldvuse = SSA_NAME_VAR (oldvuse);
503       if (oldvuse != (build_vuse != NULL_TREE
504                       ? build_vuse : build_vdef))
505         gimple_set_vuse (stmt, NULL_TREE);
506       VEC_safe_insert (tree, heap, build_uses, 0, (tree)gimple_vuse_ptr (stmt));
507     }
508
509   new_list.next = NULL;
510   last = &new_list;
511
512   old_ops = gimple_use_ops (stmt);
513
514   /* Clear a no longer necessary VUSE.  */
515   if (build_vuse == NULL_TREE
516       && gimple_vuse (stmt) != NULL_TREE)
517     gimple_set_vuse (stmt, NULL_TREE);
518
519   /* If there is anything in the old list, free it.  */
520   if (old_ops)
521     {
522       for (ptr = old_ops; ptr; ptr = ptr->next)
523         delink_imm_use (USE_OP_PTR (ptr));
524       old_ops->next = gimple_ssa_operands (cfun)->free_uses;
525       gimple_ssa_operands (cfun)->free_uses = old_ops;
526     }
527
528   /* If we added a VUSE, make sure to set the operand if it is not already
529      present and mark it for renaming.  */
530   if (build_vuse != NULL_TREE
531       && gimple_vuse (stmt) == NULL_TREE)
532     {
533       gimple_set_vuse (stmt, gimple_vop (cfun));
534       mark_sym_for_renaming (gimple_vop (cfun));
535     }
536
537   /* Now create nodes for all the new nodes.  */
538   for (new_i = 0; new_i < VEC_length (tree, build_uses); new_i++)
539     last = add_use_op (stmt,
540                        (tree *) VEC_index (tree, build_uses, new_i),
541                        last);
542
543   /* Now set the stmt's operands.  */
544   gimple_set_use_ops (stmt, new_list.next);
545 }
546
547
548 /* Clear the in_list bits and empty the build array for VDEFs and
549    VUSEs.  */
550
551 static inline void
552 cleanup_build_arrays (void)
553 {
554   build_vdef = NULL_TREE;
555   build_vuse = NULL_TREE;
556   VEC_truncate (tree, build_defs, 0);
557   VEC_truncate (tree, build_uses, 0);
558 }
559
560
561 /* Finalize all the build vectors, fill the new ones into INFO.  */
562
563 static inline void
564 finalize_ssa_stmt_operands (gimple stmt)
565 {
566   finalize_ssa_defs (stmt);
567   finalize_ssa_uses (stmt);
568   cleanup_build_arrays ();
569 }
570
571
572 /* Start the process of building up operands vectors in INFO.  */
573
574 static inline void
575 start_ssa_stmt_operands (void)
576 {
577   gcc_assert (VEC_length (tree, build_defs) == 0);
578   gcc_assert (VEC_length (tree, build_uses) == 0);
579   gcc_assert (build_vuse == NULL_TREE);
580   gcc_assert (build_vdef == NULL_TREE);
581 }
582
583
584 /* Add DEF_P to the list of pointers to operands.  */
585
586 static inline void
587 append_def (tree *def_p)
588 {
589   VEC_safe_push (tree, heap, build_defs, (tree) def_p);
590 }
591
592
593 /* Add USE_P to the list of pointers to operands.  */
594
595 static inline void
596 append_use (tree *use_p)
597 {
598   VEC_safe_push (tree, heap, build_uses, (tree) use_p);
599 }
600
601
602 /* Add VAR to the set of variables that require a VDEF operator.  */
603
604 static inline void
605 append_vdef (tree var)
606 {
607   if (!optimize)
608     return;
609
610   gcc_assert ((build_vdef == NULL_TREE
611                || build_vdef == var)
612               && (build_vuse == NULL_TREE
613                   || build_vuse == var));
614
615   build_vdef = var;
616   build_vuse = var;
617 }
618
619
620 /* Add VAR to the set of variables that require a VUSE operator.  */
621
622 static inline void
623 append_vuse (tree var)
624 {
625   if (!optimize)
626     return;
627
628   gcc_assert (build_vuse == NULL_TREE
629               || build_vuse == var);
630
631   build_vuse = var;
632 }
633
634 /* Add virtual operands for STMT.  FLAGS is as in get_expr_operands.  */
635
636 static void
637 add_virtual_operand (gimple stmt ATTRIBUTE_UNUSED, int flags)
638 {
639   /* Add virtual operands to the stmt, unless the caller has specifically
640      requested not to do that (used when adding operands inside an
641      ADDR_EXPR expression).  */
642   if (flags & opf_no_vops)
643     return;
644
645   gcc_assert (!is_gimple_debug (stmt));
646
647   if (flags & opf_def)
648     append_vdef (gimple_vop (cfun));
649   else
650     append_vuse (gimple_vop (cfun));
651 }
652
653
654 /* Add *VAR_P to the appropriate operand array for statement STMT.
655    FLAGS is as in get_expr_operands.  If *VAR_P is a GIMPLE register,
656    it will be added to the statement's real operands, otherwise it is
657    added to virtual operands.  */
658
659 static void
660 add_stmt_operand (tree *var_p, gimple stmt, int flags)
661 {
662   tree var, sym;
663
664   gcc_assert (SSA_VAR_P (*var_p));
665
666   var = *var_p;
667   sym = (TREE_CODE (var) == SSA_NAME ? SSA_NAME_VAR (var) : var);
668
669   /* Mark statements with volatile operands.  */
670   if (TREE_THIS_VOLATILE (sym))
671     gimple_set_has_volatile_ops (stmt, true);
672
673   if (is_gimple_reg (sym))
674     {
675       /* The variable is a GIMPLE register.  Add it to real operands.  */
676       if (flags & opf_def)
677         append_def (var_p);
678       else
679         append_use (var_p);
680     }
681   else
682     add_virtual_operand (stmt, flags);
683 }
684
685 /* Mark the base address of REF as having its address taken.
686    REF may be a single variable whose address has been taken or any
687    other valid GIMPLE memory reference (structure reference, array,
688    etc).  */
689
690 static void
691 mark_address_taken (tree ref)
692 {
693   tree var;
694
695   /* Note that it is *NOT OKAY* to use the target of a COMPONENT_REF
696      as the only thing we take the address of.  If VAR is a structure,
697      taking the address of a field means that the whole structure may
698      be referenced using pointer arithmetic.  See PR 21407 and the
699      ensuing mailing list discussion.  */
700   var = get_base_address (ref);
701   if (var)
702     {
703       if (DECL_P (var))
704         TREE_ADDRESSABLE (var) = 1;
705       else if (TREE_CODE (var) == MEM_REF
706                && TREE_CODE (TREE_OPERAND (var, 0)) == ADDR_EXPR
707                && DECL_P (TREE_OPERAND (TREE_OPERAND (var, 0), 0)))
708         TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (var, 0), 0)) = 1;
709     }
710 }
711
712
713 /* A subroutine of get_expr_operands to handle MEM_REF.
714
715    STMT is the statement being processed, EXPR is the MEM_REF
716       that got us here.
717
718    FLAGS is as in get_expr_operands.
719
720    RECURSE_ON_BASE should be set to true if we want to continue
721       calling get_expr_operands on the base pointer, and false if
722       something else will do it for us.  */
723
724 static void
725 get_indirect_ref_operands (gimple stmt, tree expr, int flags,
726                            bool recurse_on_base)
727 {
728   tree *pptr = &TREE_OPERAND (expr, 0);
729
730   if (TREE_THIS_VOLATILE (expr))
731     gimple_set_has_volatile_ops (stmt, true);
732
733   /* Add the VOP.  */
734   add_virtual_operand (stmt, flags);
735
736   /* If requested, add a USE operand for the base pointer.  */
737   if (recurse_on_base)
738     get_expr_operands (stmt, pptr,
739                        opf_non_addressable | opf_use
740                        | (flags & (opf_no_vops|opf_not_non_addressable)));
741 }
742
743
744 /* A subroutine of get_expr_operands to handle TARGET_MEM_REF.  */
745
746 static void
747 get_tmr_operands (gimple stmt, tree expr, int flags)
748 {
749   if (TREE_THIS_VOLATILE (expr))
750     gimple_set_has_volatile_ops (stmt, true);
751
752   /* First record the real operands.  */
753   get_expr_operands (stmt, &TMR_BASE (expr), opf_use | (flags & opf_no_vops));
754   get_expr_operands (stmt, &TMR_INDEX (expr), opf_use | (flags & opf_no_vops));
755   get_expr_operands (stmt, &TMR_INDEX2 (expr), opf_use | (flags & opf_no_vops));
756
757   add_virtual_operand (stmt, flags);
758 }
759
760
761 /* If STMT is a call that may clobber globals and other symbols that
762    escape, add them to the VDEF/VUSE lists for it.  */
763
764 static void
765 maybe_add_call_vops (gimple stmt)
766 {
767   int call_flags = gimple_call_flags (stmt);
768
769   /* If aliases have been computed already, add VDEF or VUSE
770      operands for all the symbols that have been found to be
771      call-clobbered.  */
772   if (!(call_flags & ECF_NOVOPS))
773     {
774       /* A 'pure' or a 'const' function never call-clobbers anything.
775          A 'noreturn' function might, but since we don't return anyway
776          there is no point in recording that.  */
777       if (!(call_flags & (ECF_PURE | ECF_CONST | ECF_NORETURN)))
778         add_virtual_operand (stmt, opf_def);
779       else if (!(call_flags & ECF_CONST))
780         add_virtual_operand (stmt, opf_use);
781     }
782 }
783
784
785 /* Scan operands in the ASM_EXPR stmt referred to in INFO.  */
786
787 static void
788 get_asm_expr_operands (gimple stmt)
789 {
790   size_t i, noutputs;
791   const char **oconstraints;
792   const char *constraint;
793   bool allows_mem, allows_reg, is_inout;
794
795   noutputs = gimple_asm_noutputs (stmt);
796   oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
797
798   /* Gather all output operands.  */
799   for (i = 0; i < gimple_asm_noutputs (stmt); i++)
800     {
801       tree link = gimple_asm_output_op (stmt, i);
802       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
803       oconstraints[i] = constraint;
804       parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
805                                &allows_reg, &is_inout);
806
807       /* This should have been split in gimplify_asm_expr.  */
808       gcc_assert (!allows_reg || !is_inout);
809
810       /* Memory operands are addressable.  Note that STMT needs the
811          address of this operand.  */
812       if (!allows_reg && allows_mem)
813         mark_address_taken (TREE_VALUE (link));
814
815       get_expr_operands (stmt, &TREE_VALUE (link), opf_def | opf_not_non_addressable);
816     }
817
818   /* Gather all input operands.  */
819   for (i = 0; i < gimple_asm_ninputs (stmt); i++)
820     {
821       tree link = gimple_asm_input_op (stmt, i);
822       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
823       parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
824                               &allows_mem, &allows_reg);
825
826       /* Memory operands are addressable.  Note that STMT needs the
827          address of this operand.  */
828       if (!allows_reg && allows_mem)
829         mark_address_taken (TREE_VALUE (link));
830
831       get_expr_operands (stmt, &TREE_VALUE (link), opf_not_non_addressable);
832     }
833
834   /* Clobber all memory and addressable symbols for asm ("" : : : "memory");  */
835   if (gimple_asm_clobbers_memory_p (stmt))
836     add_virtual_operand (stmt, opf_def);
837 }
838
839
840 /* Recursively scan the expression pointed to by EXPR_P in statement
841    STMT.  FLAGS is one of the OPF_* constants modifying how to
842    interpret the operands found.  */
843
844 static void
845 get_expr_operands (gimple stmt, tree *expr_p, int flags)
846 {
847   enum tree_code code;
848   enum tree_code_class codeclass;
849   tree expr = *expr_p;
850   int uflags = opf_use;
851
852   if (expr == NULL)
853     return;
854
855   if (is_gimple_debug (stmt))
856     uflags |= (flags & opf_no_vops);
857
858   code = TREE_CODE (expr);
859   codeclass = TREE_CODE_CLASS (code);
860
861   switch (code)
862     {
863     case ADDR_EXPR:
864       /* Taking the address of a variable does not represent a
865          reference to it, but the fact that the statement takes its
866          address will be of interest to some passes (e.g. alias
867          resolution).  */
868       if ((!(flags & opf_non_addressable)
869            || (flags & opf_not_non_addressable))
870           && !is_gimple_debug (stmt))
871         mark_address_taken (TREE_OPERAND (expr, 0));
872
873       /* If the address is invariant, there may be no interesting
874          variable references inside.  */
875       if (is_gimple_min_invariant (expr))
876         return;
877
878       /* Otherwise, there may be variables referenced inside but there
879          should be no VUSEs created, since the referenced objects are
880          not really accessed.  The only operands that we should find
881          here are ARRAY_REF indices which will always be real operands
882          (GIMPLE does not allow non-registers as array indices).  */
883       flags |= opf_no_vops;
884       get_expr_operands (stmt, &TREE_OPERAND (expr, 0),
885                          flags | opf_not_non_addressable);
886       return;
887
888     case SSA_NAME:
889      add_stmt_operand (expr_p, stmt, flags);
890      return;
891
892     case VAR_DECL:
893     case PARM_DECL:
894     case RESULT_DECL:
895       add_stmt_operand (expr_p, stmt, flags);
896       return;
897
898     case DEBUG_EXPR_DECL:
899       gcc_assert (gimple_debug_bind_p (stmt));
900       return;
901
902     case MEM_REF:
903       get_indirect_ref_operands (stmt, expr, flags, true);
904       return;
905
906     case TARGET_MEM_REF:
907       get_tmr_operands (stmt, expr, flags);
908       return;
909
910     case ARRAY_REF:
911     case ARRAY_RANGE_REF:
912     case COMPONENT_REF:
913     case REALPART_EXPR:
914     case IMAGPART_EXPR:
915       {
916         if (TREE_THIS_VOLATILE (expr))
917           gimple_set_has_volatile_ops (stmt, true);
918
919         get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
920
921         if (code == COMPONENT_REF)
922           {
923             if (TREE_THIS_VOLATILE (TREE_OPERAND (expr, 1)))
924               gimple_set_has_volatile_ops (stmt, true);
925             get_expr_operands (stmt, &TREE_OPERAND (expr, 2), uflags);
926           }
927         else if (code == ARRAY_REF || code == ARRAY_RANGE_REF)
928           {
929             get_expr_operands (stmt, &TREE_OPERAND (expr, 1), uflags);
930             get_expr_operands (stmt, &TREE_OPERAND (expr, 2), uflags);
931             get_expr_operands (stmt, &TREE_OPERAND (expr, 3), uflags);
932           }
933
934         return;
935       }
936
937     case WITH_SIZE_EXPR:
938       /* WITH_SIZE_EXPR is a pass-through reference to its first argument,
939          and an rvalue reference to its second argument.  */
940       get_expr_operands (stmt, &TREE_OPERAND (expr, 1), uflags);
941       get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
942       return;
943
944     case COND_EXPR:
945     case VEC_COND_EXPR:
946     case VEC_PERM_EXPR:
947       get_expr_operands (stmt, &TREE_OPERAND (expr, 0), uflags);
948       get_expr_operands (stmt, &TREE_OPERAND (expr, 1), uflags);
949       get_expr_operands (stmt, &TREE_OPERAND (expr, 2), uflags);
950       return;
951
952     case CONSTRUCTOR:
953       {
954         /* General aggregate CONSTRUCTORs have been decomposed, but they
955            are still in use as the COMPLEX_EXPR equivalent for vectors.  */
956         constructor_elt *ce;
957         unsigned HOST_WIDE_INT idx;
958
959         /* A volatile constructor is actually TREE_CLOBBER_P, transfer
960            the volatility to the statement, don't use TREE_CLOBBER_P for
961            mirroring the other uses of THIS_VOLATILE in this file.  */
962         if (TREE_THIS_VOLATILE (expr))
963           gimple_set_has_volatile_ops (stmt, true);
964
965         for (idx = 0;
966              VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (expr), idx, ce);
967              idx++)
968           get_expr_operands (stmt, &ce->value, uflags);
969
970         return;
971       }
972
973     case BIT_FIELD_REF:
974       if (TREE_THIS_VOLATILE (expr))
975         gimple_set_has_volatile_ops (stmt, true);
976       /* FALLTHRU */
977
978     case VIEW_CONVERT_EXPR:
979     do_unary:
980       get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
981       return;
982
983     case COMPOUND_EXPR:
984     case OBJ_TYPE_REF:
985     case ASSERT_EXPR:
986     do_binary:
987       {
988         get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
989         get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
990         return;
991       }
992
993     case DOT_PROD_EXPR:
994     case REALIGN_LOAD_EXPR:
995     case WIDEN_MULT_PLUS_EXPR:
996     case WIDEN_MULT_MINUS_EXPR:
997     case FMA_EXPR:
998       {
999         get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1000         get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1001         get_expr_operands (stmt, &TREE_OPERAND (expr, 2), flags);
1002         return;
1003       }
1004
1005     case FUNCTION_DECL:
1006     case LABEL_DECL:
1007     case CONST_DECL:
1008     case CASE_LABEL_EXPR:
1009       /* Expressions that make no memory references.  */
1010       return;
1011
1012     default:
1013       if (codeclass == tcc_unary)
1014         goto do_unary;
1015       if (codeclass == tcc_binary || codeclass == tcc_comparison)
1016         goto do_binary;
1017       if (codeclass == tcc_constant || codeclass == tcc_type)
1018         return;
1019     }
1020
1021   /* If we get here, something has gone wrong.  */
1022 #ifdef ENABLE_CHECKING
1023   fprintf (stderr, "unhandled expression in get_expr_operands():\n");
1024   debug_tree (expr);
1025   fputs ("\n", stderr);
1026 #endif
1027   gcc_unreachable ();
1028 }
1029
1030
1031 /* Parse STMT looking for operands.  When finished, the various
1032    build_* operand vectors will have potential operands in them.  */
1033
1034 static void
1035 parse_ssa_operands (gimple stmt)
1036 {
1037   enum gimple_code code = gimple_code (stmt);
1038
1039   if (code == GIMPLE_ASM)
1040     get_asm_expr_operands (stmt);
1041   else if (is_gimple_debug (stmt))
1042     {
1043       if (gimple_debug_bind_p (stmt)
1044           && gimple_debug_bind_has_value_p (stmt))
1045         get_expr_operands (stmt, gimple_debug_bind_get_value_ptr (stmt),
1046                            opf_use | opf_no_vops);
1047     }
1048   else
1049     {
1050       size_t i, start = 0;
1051
1052       if (code == GIMPLE_ASSIGN || code == GIMPLE_CALL)
1053         {
1054           get_expr_operands (stmt, gimple_op_ptr (stmt, 0), opf_def);
1055           start = 1;
1056         }
1057
1058       for (i = start; i < gimple_num_ops (stmt); i++)
1059         get_expr_operands (stmt, gimple_op_ptr (stmt, i), opf_use);
1060
1061       /* Add call-clobbered operands, if needed.  */
1062       if (code == GIMPLE_CALL)
1063         maybe_add_call_vops (stmt);
1064
1065       if (code == GIMPLE_RETURN)
1066         append_vuse (gimple_vop (cfun));
1067     }
1068 }
1069
1070
1071 /* Create an operands cache for STMT.  */
1072
1073 static void
1074 build_ssa_operands (gimple stmt)
1075 {
1076   /* Initially assume that the statement has no volatile operands.  */
1077   gimple_set_has_volatile_ops (stmt, false);
1078
1079   start_ssa_stmt_operands ();
1080   parse_ssa_operands (stmt);
1081   finalize_ssa_stmt_operands (stmt);
1082 }
1083
1084
1085 /* Releases the operands of STMT back to their freelists, and clears
1086    the stmt operand lists.  */
1087
1088 void
1089 free_stmt_operands (gimple stmt)
1090 {
1091   def_optype_p defs = gimple_def_ops (stmt), last_def;
1092   use_optype_p uses = gimple_use_ops (stmt), last_use;
1093
1094   if (defs)
1095     {
1096       for (last_def = defs; last_def->next; last_def = last_def->next)
1097         continue;
1098       last_def->next = gimple_ssa_operands (cfun)->free_defs;
1099       gimple_ssa_operands (cfun)->free_defs = defs;
1100       gimple_set_def_ops (stmt, NULL);
1101     }
1102
1103   if (uses)
1104     {
1105       for (last_use = uses; last_use->next; last_use = last_use->next)
1106         delink_imm_use (USE_OP_PTR (last_use));
1107       delink_imm_use (USE_OP_PTR (last_use));
1108       last_use->next = gimple_ssa_operands (cfun)->free_uses;
1109       gimple_ssa_operands (cfun)->free_uses = uses;
1110       gimple_set_use_ops (stmt, NULL);
1111     }
1112
1113   if (gimple_has_mem_ops (stmt))
1114     {
1115       gimple_set_vuse (stmt, NULL_TREE);
1116       gimple_set_vdef (stmt, NULL_TREE);
1117     }
1118 }
1119
1120
1121 /* Get the operands of statement STMT.  */
1122
1123 void
1124 update_stmt_operands (gimple stmt)
1125 {
1126   /* If update_stmt_operands is called before SSA is initialized, do
1127      nothing.  */
1128   if (!ssa_operands_active ())
1129     return;
1130
1131   timevar_push (TV_TREE_OPS);
1132
1133   /* If the stmt is a noreturn call queue it to be processed by
1134      split_bbs_on_noreturn_calls during cfg cleanup.  */
1135   if (is_gimple_call (stmt)
1136       && gimple_call_noreturn_p (stmt))
1137     VEC_safe_push (gimple, gc, MODIFIED_NORETURN_CALLS (cfun), stmt);
1138
1139   gcc_assert (gimple_modified_p (stmt));
1140   build_ssa_operands (stmt);
1141   gimple_set_modified (stmt, false);
1142
1143   timevar_pop (TV_TREE_OPS);
1144 }
1145
1146
1147 /* Swap operands EXP0 and EXP1 in statement STMT.  No attempt is done
1148    to test the validity of the swap operation.  */
1149
1150 void
1151 swap_tree_operands (gimple stmt, tree *exp0, tree *exp1)
1152 {
1153   tree op0, op1;
1154   op0 = *exp0;
1155   op1 = *exp1;
1156
1157   /* If the operand cache is active, attempt to preserve the relative
1158      positions of these two operands in their respective immediate use
1159      lists by adjusting their use pointer to point to the new
1160      operand position.  */
1161   if (ssa_operands_active () && op0 != op1)
1162     {
1163       use_optype_p use0, use1, ptr;
1164       use0 = use1 = NULL;
1165
1166       /* Find the 2 operands in the cache, if they are there.  */
1167       for (ptr = gimple_use_ops (stmt); ptr; ptr = ptr->next)
1168         if (USE_OP_PTR (ptr)->use == exp0)
1169           {
1170             use0 = ptr;
1171             break;
1172           }
1173
1174       for (ptr = gimple_use_ops (stmt); ptr; ptr = ptr->next)
1175         if (USE_OP_PTR (ptr)->use == exp1)
1176           {
1177             use1 = ptr;
1178             break;
1179           }
1180
1181       /* And adjust their location to point to the new position of the
1182          operand.  */
1183       if (use0)
1184         USE_OP_PTR (use0)->use = exp1;
1185       if (use1)
1186         USE_OP_PTR (use1)->use = exp0;
1187     }
1188
1189   /* Now swap the data.  */
1190   *exp0 = op1;
1191   *exp1 = op0;
1192 }
1193
1194
1195 /* Scan the immediate_use list for VAR making sure its linked properly.
1196    Return TRUE if there is a problem and emit an error message to F.  */
1197
1198 DEBUG_FUNCTION bool
1199 verify_imm_links (FILE *f, tree var)
1200 {
1201   use_operand_p ptr, prev, list;
1202   int count;
1203
1204   gcc_assert (TREE_CODE (var) == SSA_NAME);
1205
1206   list = &(SSA_NAME_IMM_USE_NODE (var));
1207   gcc_assert (list->use == NULL);
1208
1209   if (list->prev == NULL)
1210     {
1211       gcc_assert (list->next == NULL);
1212       return false;
1213     }
1214
1215   prev = list;
1216   count = 0;
1217   for (ptr = list->next; ptr != list; )
1218     {
1219       if (prev != ptr->prev)
1220         goto error;
1221
1222       if (ptr->use == NULL)
1223         goto error; /* 2 roots, or SAFE guard node.  */
1224       else if (*(ptr->use) != var)
1225         goto error;
1226
1227       prev = ptr;
1228       ptr = ptr->next;
1229
1230       /* Avoid infinite loops.  50,000,000 uses probably indicates a
1231          problem.  */
1232       if (count++ > 50000000)
1233         goto error;
1234     }
1235
1236   /* Verify list in the other direction.  */
1237   prev = list;
1238   for (ptr = list->prev; ptr != list; )
1239     {
1240       if (prev != ptr->next)
1241         goto error;
1242       prev = ptr;
1243       ptr = ptr->prev;
1244       if (count-- < 0)
1245         goto error;
1246     }
1247
1248   if (count != 0)
1249     goto error;
1250
1251   return false;
1252
1253  error:
1254   if (ptr->loc.stmt && gimple_modified_p (ptr->loc.stmt))
1255     {
1256       fprintf (f, " STMT MODIFIED. - <%p> ", (void *)ptr->loc.stmt);
1257       print_gimple_stmt (f, ptr->loc.stmt, 0, TDF_SLIM);
1258     }
1259   fprintf (f, " IMM ERROR : (use_p : tree - %p:%p)", (void *)ptr,
1260            (void *)ptr->use);
1261   print_generic_expr (f, USE_FROM_PTR (ptr), TDF_SLIM);
1262   fprintf(f, "\n");
1263   return true;
1264 }
1265
1266
1267 /* Dump all the immediate uses to FILE.  */
1268
1269 void
1270 dump_immediate_uses_for (FILE *file, tree var)
1271 {
1272   imm_use_iterator iter;
1273   use_operand_p use_p;
1274
1275   gcc_assert (var && TREE_CODE (var) == SSA_NAME);
1276
1277   print_generic_expr (file, var, TDF_SLIM);
1278   fprintf (file, " : -->");
1279   if (has_zero_uses (var))
1280     fprintf (file, " no uses.\n");
1281   else
1282     if (has_single_use (var))
1283       fprintf (file, " single use.\n");
1284     else
1285       fprintf (file, "%d uses.\n", num_imm_uses (var));
1286
1287   FOR_EACH_IMM_USE_FAST (use_p, iter, var)
1288     {
1289       if (use_p->loc.stmt == NULL && use_p->use == NULL)
1290         fprintf (file, "***end of stmt iterator marker***\n");
1291       else
1292         if (!is_gimple_reg (USE_FROM_PTR (use_p)))
1293           print_gimple_stmt (file, USE_STMT (use_p), 0, TDF_VOPS|TDF_MEMSYMS);
1294         else
1295           print_gimple_stmt (file, USE_STMT (use_p), 0, TDF_SLIM);
1296     }
1297   fprintf(file, "\n");
1298 }
1299
1300
1301 /* Dump all the immediate uses to FILE.  */
1302
1303 void
1304 dump_immediate_uses (FILE *file)
1305 {
1306   tree var;
1307   unsigned int x;
1308
1309   fprintf (file, "Immediate_uses: \n\n");
1310   for (x = 1; x < num_ssa_names; x++)
1311     {
1312       var = ssa_name(x);
1313       if (!var)
1314         continue;
1315       dump_immediate_uses_for (file, var);
1316     }
1317 }
1318
1319
1320 /* Dump def-use edges on stderr.  */
1321
1322 DEBUG_FUNCTION void
1323 debug_immediate_uses (void)
1324 {
1325   dump_immediate_uses (stderr);
1326 }
1327
1328
1329 /* Dump def-use edges on stderr.  */
1330
1331 DEBUG_FUNCTION void
1332 debug_immediate_uses_for (tree var)
1333 {
1334   dump_immediate_uses_for (stderr, var);
1335 }
1336
1337
1338 /* Unlink STMTs virtual definition from the IL by propagating its use.  */
1339
1340 void
1341 unlink_stmt_vdef (gimple stmt)
1342 {
1343   use_operand_p use_p;
1344   imm_use_iterator iter;
1345   gimple use_stmt;
1346   tree vdef = gimple_vdef (stmt);
1347
1348   if (!vdef
1349       || TREE_CODE (vdef) != SSA_NAME)
1350     return;
1351
1352   FOR_EACH_IMM_USE_STMT (use_stmt, iter, gimple_vdef (stmt))
1353     {
1354       FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1355         SET_USE (use_p, gimple_vuse (stmt));
1356     }
1357
1358   if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_vdef (stmt)))
1359     SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_vuse (stmt)) = 1;
1360 }
1361