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