OSDN Git Service

2007-10-18 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa.c
1 /* Miscellaneous SSA utility functions.
2    Copyright (C) 2001, 2002, 2003, 2004, 2005, 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 "rtl.h"
27 #include "tm_p.h"
28 #include "ggc.h"
29 #include "langhooks.h"
30 #include "hard-reg-set.h"
31 #include "basic-block.h"
32 #include "output.h"
33 #include "expr.h"
34 #include "function.h"
35 #include "diagnostic.h"
36 #include "bitmap.h"
37 #include "pointer-set.h"
38 #include "tree-flow.h"
39 #include "tree-gimple.h"
40 #include "tree-inline.h"
41 #include "varray.h"
42 #include "timevar.h"
43 #include "hashtab.h"
44 #include "tree-dump.h"
45 #include "tree-pass.h"
46 #include "toplev.h"
47
48 /* Remove the corresponding arguments from the PHI nodes in E's
49    destination block and redirect it to DEST.  Return redirected edge.
50    The list of removed arguments is stored in PENDING_STMT (e).  */
51
52 edge
53 ssa_redirect_edge (edge e, basic_block dest)
54 {
55   tree phi;
56   tree list = NULL, *last = &list;
57   tree src, dst, node;
58
59   /* Remove the appropriate PHI arguments in E's destination block.  */
60   for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
61     {
62       if (PHI_ARG_DEF (phi, e->dest_idx) == NULL_TREE)
63         continue;
64
65       src = PHI_ARG_DEF (phi, e->dest_idx);
66       dst = PHI_RESULT (phi);
67       node = build_tree_list (dst, src);
68       *last = node;
69       last = &TREE_CHAIN (node);
70     }
71
72   e = redirect_edge_succ_nodup (e, dest);
73   PENDING_STMT (e) = list;
74
75   return e;
76 }
77
78 /* Add PHI arguments queued in PENDING_STMT list on edge E to edge
79    E->dest.  */
80
81 void
82 flush_pending_stmts (edge e)
83 {
84   tree phi, arg;
85
86   if (!PENDING_STMT (e))
87     return;
88
89   for (phi = phi_nodes (e->dest), arg = PENDING_STMT (e);
90        phi;
91        phi = PHI_CHAIN (phi), arg = TREE_CHAIN (arg))
92     {
93       tree def = TREE_VALUE (arg);
94       add_phi_arg (phi, def, e);
95     }
96
97   PENDING_STMT (e) = NULL;
98 }
99
100 /* Return true if SSA_NAME is malformed and mark it visited.
101
102    IS_VIRTUAL is true if this SSA_NAME was found inside a virtual
103       operand.  */
104
105 static bool
106 verify_ssa_name (tree ssa_name, bool is_virtual)
107 {
108   if (TREE_CODE (ssa_name) != SSA_NAME)
109     {
110       error ("expected an SSA_NAME object");
111       return true;
112     }
113
114   if (TREE_TYPE (ssa_name) != TREE_TYPE (SSA_NAME_VAR (ssa_name)))
115     {
116       error ("type mismatch between an SSA_NAME and its symbol");
117       return true;
118     }
119
120   if (SSA_NAME_IN_FREE_LIST (ssa_name))
121     {
122       error ("found an SSA_NAME that had been released into the free pool");
123       return true;
124     }
125
126   if (is_virtual && is_gimple_reg (ssa_name))
127     {
128       error ("found a virtual definition for a GIMPLE register");
129       return true;
130     }
131
132   if (!is_virtual && !is_gimple_reg (ssa_name))
133     {
134       error ("found a real definition for a non-register");
135       return true;
136     }
137
138   if (is_virtual && var_ann (SSA_NAME_VAR (ssa_name)) 
139       && get_subvars_for_var (SSA_NAME_VAR (ssa_name)) != NULL)
140     {
141       error ("found real variable when subvariables should have appeared");
142       return true;
143     }
144
145   if (SSA_NAME_IS_DEFAULT_DEF (ssa_name)
146       && !IS_EMPTY_STMT (SSA_NAME_DEF_STMT (ssa_name)))
147     {
148       error ("found a default name with a non-empty defining statement");
149       return true;
150     }
151
152   return false;
153 }
154
155
156 /* Return true if the definition of SSA_NAME at block BB is malformed.
157
158    STMT is the statement where SSA_NAME is created.
159
160    DEFINITION_BLOCK is an array of basic blocks indexed by SSA_NAME
161       version numbers.  If DEFINITION_BLOCK[SSA_NAME_VERSION] is set,
162       it means that the block in that array slot contains the
163       definition of SSA_NAME.
164
165    IS_VIRTUAL is true if SSA_NAME is created by a VDEF.  */
166
167 static bool
168 verify_def (basic_block bb, basic_block *definition_block, tree ssa_name,
169             tree stmt, bool is_virtual)
170 {
171   if (verify_ssa_name (ssa_name, is_virtual))
172     goto err;
173
174   if (definition_block[SSA_NAME_VERSION (ssa_name)])
175     {
176       error ("SSA_NAME created in two different blocks %i and %i",
177              definition_block[SSA_NAME_VERSION (ssa_name)]->index, bb->index);
178       goto err;
179     }
180
181   definition_block[SSA_NAME_VERSION (ssa_name)] = bb;
182
183   if (SSA_NAME_DEF_STMT (ssa_name) != stmt)
184     {
185       error ("SSA_NAME_DEF_STMT is wrong");
186       fprintf (stderr, "Expected definition statement:\n");
187       print_generic_stmt (stderr, SSA_NAME_DEF_STMT (ssa_name), TDF_VOPS);
188       fprintf (stderr, "\nActual definition statement:\n");
189       print_generic_stmt (stderr, stmt, TDF_VOPS);
190       goto err;
191     }
192
193   return false;
194
195 err:
196   fprintf (stderr, "while verifying SSA_NAME ");
197   print_generic_expr (stderr, ssa_name, 0);
198   fprintf (stderr, " in statement\n");
199   print_generic_stmt (stderr, stmt, TDF_VOPS);
200
201   return true;
202 }
203
204
205 /* Return true if the use of SSA_NAME at statement STMT in block BB is
206    malformed.
207
208    DEF_BB is the block where SSA_NAME was found to be created.
209
210    IDOM contains immediate dominator information for the flowgraph.
211
212    CHECK_ABNORMAL is true if the caller wants to check whether this use
213       is flowing through an abnormal edge (only used when checking PHI
214       arguments).
215
216    If NAMES_DEFINED_IN_BB is not NULL, it contains a bitmap of ssa names
217      that are defined before STMT in basic block BB.  */
218
219 static bool
220 verify_use (basic_block bb, basic_block def_bb, use_operand_p use_p,
221             tree stmt, bool check_abnormal, bitmap names_defined_in_bb)
222 {
223   bool err = false;
224   tree ssa_name = USE_FROM_PTR (use_p);
225
226   if (!TREE_VISITED (ssa_name))
227     if (verify_imm_links (stderr, ssa_name))
228       err = true;
229
230   TREE_VISITED (ssa_name) = 1;
231
232   if (IS_EMPTY_STMT (SSA_NAME_DEF_STMT (ssa_name))
233       && SSA_NAME_IS_DEFAULT_DEF (ssa_name))
234     ; /* Default definitions have empty statements.  Nothing to do.  */
235   else if (!def_bb)
236     {
237       error ("missing definition");
238       err = true;
239     }
240   else if (bb != def_bb
241            && !dominated_by_p (CDI_DOMINATORS, bb, def_bb))
242     {
243       error ("definition in block %i does not dominate use in block %i",
244              def_bb->index, bb->index);
245       err = true;
246     }
247   else if (bb == def_bb
248            && names_defined_in_bb != NULL
249            && !bitmap_bit_p (names_defined_in_bb, SSA_NAME_VERSION (ssa_name)))
250     {
251       error ("definition in block %i follows the use", def_bb->index);
252       err = true;
253     }
254
255   if (check_abnormal
256       && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
257     {
258       error ("SSA_NAME_OCCURS_IN_ABNORMAL_PHI should be set");
259       err = true;
260     }
261
262   /* Make sure the use is in an appropriate list by checking the previous 
263      element to make sure it's the same.  */
264   if (use_p->prev == NULL)
265     {
266       error ("no immediate_use list");
267       err = true;
268     }
269   else
270     {
271       tree listvar ;
272       if (use_p->prev->use == NULL)
273         listvar = use_p->prev->stmt;
274       else
275         listvar = USE_FROM_PTR (use_p->prev);
276       if (listvar != ssa_name)
277         {
278           error ("wrong immediate use list");
279           err = true;
280         }
281     }
282
283   if (err)
284     {
285       fprintf (stderr, "for SSA_NAME: ");
286       print_generic_expr (stderr, ssa_name, TDF_VOPS);
287       fprintf (stderr, " in statement:\n");
288       print_generic_stmt (stderr, stmt, TDF_VOPS);
289     }
290
291   return err;
292 }
293
294
295 /* Return true if any of the arguments for PHI node PHI at block BB is
296    malformed.
297
298    DEFINITION_BLOCK is an array of basic blocks indexed by SSA_NAME
299       version numbers.  If DEFINITION_BLOCK[SSA_NAME_VERSION] is set,
300       it means that the block in that array slot contains the
301       definition of SSA_NAME.  */
302
303 static bool
304 verify_phi_args (tree phi, basic_block bb, basic_block *definition_block)
305 {
306   edge e;
307   bool err = false;
308   unsigned i, phi_num_args = PHI_NUM_ARGS (phi);
309
310   if (EDGE_COUNT (bb->preds) != phi_num_args)
311     {
312       error ("incoming edge count does not match number of PHI arguments");
313       err = true;
314       goto error;
315     }
316
317   for (i = 0; i < phi_num_args; i++)
318     {
319       use_operand_p op_p = PHI_ARG_DEF_PTR (phi, i);
320       tree op = USE_FROM_PTR (op_p);
321
322       e = EDGE_PRED (bb, i);
323
324       if (op == NULL_TREE)
325         {
326           error ("PHI argument is missing for edge %d->%d",
327                  e->src->index,
328                  e->dest->index);
329           err = true;
330           goto error;
331         }
332
333       if (TREE_CODE (op) != SSA_NAME && !is_gimple_min_invariant (op))
334         {
335           error ("PHI argument is not SSA_NAME, or invariant");
336           err = true;
337         }
338
339       if (TREE_CODE (op) == SSA_NAME)
340         {
341           err = verify_ssa_name (op, !is_gimple_reg (PHI_RESULT (phi)));
342           err |= verify_use (e->src, definition_block[SSA_NAME_VERSION (op)],
343                              op_p, phi, e->flags & EDGE_ABNORMAL, NULL);
344         }
345
346       if (e->dest != bb)
347         {
348           error ("wrong edge %d->%d for PHI argument",
349                  e->src->index, e->dest->index);
350           err = true;
351         }
352
353       if (err)
354         {
355           fprintf (stderr, "PHI argument\n");
356           print_generic_stmt (stderr, op, TDF_VOPS);
357           goto error;
358         }
359     }
360
361 error:
362   if (err)
363     {
364       fprintf (stderr, "for PHI node\n");
365       print_generic_stmt (stderr, phi, TDF_VOPS|TDF_MEMSYMS);
366     }
367
368
369   return err;
370 }
371
372
373 static void
374 verify_flow_insensitive_alias_info (void)
375 {
376   tree var;
377   referenced_var_iterator rvi;
378
379   FOR_EACH_REFERENCED_VAR (var, rvi)
380     {
381       unsigned int j;
382       bitmap aliases;
383       tree alias;
384       bitmap_iterator bi;
385
386       if (!MTAG_P (var) || !MTAG_ALIASES (var))
387         continue;
388       
389       aliases = MTAG_ALIASES (var);
390
391       EXECUTE_IF_SET_IN_BITMAP (aliases, 0, j, bi)
392         {
393           alias = referenced_var (j);
394
395           if (TREE_CODE (alias) != MEMORY_PARTITION_TAG
396               && !may_be_aliased (alias))
397             {
398               error ("non-addressable variable inside an alias set");
399               debug_variable (alias);
400               goto err;
401             }
402         }
403     }
404
405   return;
406
407 err:
408   debug_variable (var);
409   internal_error ("verify_flow_insensitive_alias_info failed");
410 }
411
412
413 static void
414 verify_flow_sensitive_alias_info (void)
415 {
416   size_t i;
417   tree ptr;
418
419   for (i = 1; i < num_ssa_names; i++)
420     {
421       tree var;
422       var_ann_t ann;
423       struct ptr_info_def *pi;
424  
425
426       ptr = ssa_name (i);
427       if (!ptr)
428         continue;
429
430       /* We only care for pointers that are actually referenced in the
431          program.  */
432       if (!POINTER_TYPE_P (TREE_TYPE (ptr)) || !TREE_VISITED (ptr))
433         continue;
434
435       /* RESULT_DECL is special.  If it's a GIMPLE register, then it
436          is only written-to only once in the return statement.
437          Otherwise, aggregate RESULT_DECLs may be written-to more than
438          once in virtual operands.  */
439       var = SSA_NAME_VAR (ptr);
440       if (TREE_CODE (var) == RESULT_DECL
441           && is_gimple_reg (ptr))
442         continue;
443
444       pi = SSA_NAME_PTR_INFO (ptr);
445       if (pi == NULL)
446         continue;
447
448       ann = var_ann (var);
449       if (pi->is_dereferenced && !pi->name_mem_tag && !ann->symbol_mem_tag)
450         {
451           error ("dereferenced pointers should have a name or a symbol tag");
452           goto err;
453         }
454
455       if (pi->name_mem_tag
456           && (pi->pt_vars == NULL || bitmap_empty_p (pi->pt_vars)))
457         {
458           error ("pointers with a memory tag, should have points-to sets");
459           goto err;
460         }
461
462       if (pi->value_escapes_p && pi->name_mem_tag)
463         {
464           tree t = memory_partition (pi->name_mem_tag);
465           if (t == NULL_TREE)
466             t = pi->name_mem_tag;
467           
468           if (!is_call_clobbered (t))
469             {
470               error ("pointer escapes but its name tag is not call-clobbered");
471               goto err;
472             }
473         }
474     }
475
476   return;
477
478 err:
479   debug_variable (ptr);
480   internal_error ("verify_flow_sensitive_alias_info failed");
481 }
482
483
484 /* Verify the consistency of call clobbering information.  */
485
486 static void
487 verify_call_clobbering (void)
488 {
489   unsigned int i;
490   bitmap_iterator bi;
491   tree var;
492   referenced_var_iterator rvi;
493
494   /* At all times, the result of the call_clobbered flag should
495      match the result of the call_clobbered_vars bitmap.  Verify both
496      that everything in call_clobbered_vars is marked
497      call_clobbered, and that everything marked
498      call_clobbered is in call_clobbered_vars.  */
499   EXECUTE_IF_SET_IN_BITMAP (gimple_call_clobbered_vars (cfun), 0, i, bi)
500     {
501       var = referenced_var (i);
502
503       if (memory_partition (var))
504         var = memory_partition (var);
505
506       if (!MTAG_P (var) && !var_ann (var)->call_clobbered)
507         {
508           error ("variable in call_clobbered_vars but not marked "
509                  "call_clobbered");
510           debug_variable (var);
511           goto err;
512         }
513     }
514
515   FOR_EACH_REFERENCED_VAR (var, rvi)
516     {
517       if (is_gimple_reg (var))
518         continue;
519
520       if (memory_partition (var))
521         var = memory_partition (var);
522
523       if (!MTAG_P (var)
524           && var_ann (var)->call_clobbered
525           && !bitmap_bit_p (gimple_call_clobbered_vars (cfun), DECL_UID (var)))
526         {
527           error ("variable marked call_clobbered but not in "
528                  "call_clobbered_vars bitmap.");
529           debug_variable (var);
530           goto err;
531         }
532     }
533
534   return;
535
536  err:
537     internal_error ("verify_call_clobbering failed");
538 }
539
540
541 /* Verify invariants in memory partitions.  */
542
543 static void
544 verify_memory_partitions (void)
545 {
546   unsigned i;
547   tree mpt;
548   VEC(tree,heap) *mpt_table = gimple_ssa_operands (cfun)->mpt_table;
549   struct pointer_set_t *partitioned_syms = pointer_set_create ();
550
551   for (i = 0; VEC_iterate (tree, mpt_table, i, mpt); i++)
552     {
553       unsigned j;
554       bitmap_iterator bj;
555
556       if (MPT_SYMBOLS (mpt) == NULL)
557         {
558           error ("Memory partitions should have at least one symbol");
559           debug_variable (mpt);
560           goto err;
561         }
562
563       EXECUTE_IF_SET_IN_BITMAP (MPT_SYMBOLS (mpt), 0, j, bj)
564         {
565           tree var = referenced_var (j);
566           if (pointer_set_insert (partitioned_syms, var))
567             {
568               error ("Partitioned symbols should belong to exactly one "
569                      "partition");
570               debug_variable (var);
571               goto err;
572             }
573         }
574     }
575
576   pointer_set_destroy (partitioned_syms);
577
578   return;
579
580 err:
581   internal_error ("verify_memory_partitions failed");
582 }
583
584
585 /* Verify the consistency of aliasing information.  */
586
587 static void
588 verify_alias_info (void)
589 {
590   verify_flow_sensitive_alias_info ();
591   verify_call_clobbering ();
592   verify_flow_insensitive_alias_info ();
593   verify_memory_partitions ();
594 }
595
596
597 /* Verify common invariants in the SSA web.
598    TODO: verify the variable annotations.  */
599
600 void
601 verify_ssa (bool check_modified_stmt)
602 {
603   size_t i;
604   basic_block bb;
605   basic_block *definition_block = XCNEWVEC (basic_block, num_ssa_names);
606   ssa_op_iter iter;
607   tree op;
608   enum dom_state orig_dom_state = dom_info_state (CDI_DOMINATORS);
609   bitmap names_defined_in_bb = BITMAP_ALLOC (NULL);
610
611   gcc_assert (!need_ssa_update_p ());
612
613   verify_stmts ();
614
615   timevar_push (TV_TREE_SSA_VERIFY);
616
617   /* Keep track of SSA names present in the IL.  */
618   for (i = 1; i < num_ssa_names; i++)
619     {
620       tree name = ssa_name (i);
621       if (name)
622         {
623           tree stmt;
624           TREE_VISITED (name) = 0;
625
626           stmt = SSA_NAME_DEF_STMT (name);
627           if (!IS_EMPTY_STMT (stmt))
628             {
629               basic_block bb = bb_for_stmt (stmt);
630               verify_def (bb, definition_block,
631                           name, stmt, !is_gimple_reg (name));
632
633             }
634         }
635     }
636
637   calculate_dominance_info (CDI_DOMINATORS);
638
639   /* Now verify all the uses and make sure they agree with the definitions
640      found in the previous pass.  */
641   FOR_EACH_BB (bb)
642     {
643       edge e;
644       tree phi;
645       edge_iterator ei;
646       block_stmt_iterator bsi;
647
648       /* Make sure that all edges have a clear 'aux' field.  */
649       FOR_EACH_EDGE (e, ei, bb->preds)
650         {
651           if (e->aux)
652             {
653               error ("AUX pointer initialized for edge %d->%d", e->src->index,
654                       e->dest->index);
655               goto err;
656             }
657         }
658
659       /* Verify the arguments for every PHI node in the block.  */
660       for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
661         {
662           if (verify_phi_args (phi, bb, definition_block))
663             goto err;
664
665           bitmap_set_bit (names_defined_in_bb,
666                           SSA_NAME_VERSION (PHI_RESULT (phi)));
667         }
668
669       /* Now verify all the uses and vuses in every statement of the block.  */
670       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
671         {
672           tree stmt = bsi_stmt (bsi);
673           use_operand_p use_p;
674
675           if (check_modified_stmt && stmt_modified_p (stmt))
676             {
677               error ("stmt (%p) marked modified after optimization pass: ",
678                      (void *)stmt);
679               print_generic_stmt (stderr, stmt, TDF_VOPS);
680               goto err;
681             }
682
683           if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
684               && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) != SSA_NAME)
685             {
686               tree lhs, base_address;
687
688               lhs = GIMPLE_STMT_OPERAND (stmt, 0);
689               base_address = get_base_address (lhs);
690
691               if (base_address
692                   && gimple_aliases_computed_p (cfun)
693                   && SSA_VAR_P (base_address)
694                   && !stmt_ann (stmt)->has_volatile_ops
695                   && ZERO_SSA_OPERANDS (stmt, SSA_OP_VDEF))
696                 {
697                   error ("statement makes a memory store, but has no VDEFS");
698                   print_generic_stmt (stderr, stmt, TDF_VOPS);
699                   goto err;
700                 }
701             }
702
703           FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_ALL_VIRTUALS)
704             {
705               if (verify_ssa_name (op, true))
706                 {
707                   error ("in statement");
708                   print_generic_stmt (stderr, stmt, TDF_VOPS|TDF_MEMSYMS);
709                   goto err;
710                 }
711             }
712
713           FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE|SSA_OP_DEF)
714             {
715               if (verify_ssa_name (op, false))
716                 {
717                   error ("in statement");
718                   print_generic_stmt (stderr, stmt, TDF_VOPS|TDF_MEMSYMS);
719                   goto err;
720                 }
721             }
722
723           FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE|SSA_OP_VUSE)
724             {
725               op = USE_FROM_PTR (use_p);
726               if (verify_use (bb, definition_block[SSA_NAME_VERSION (op)],
727                               use_p, stmt, false, names_defined_in_bb))
728                 goto err;
729             }
730
731           FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_ALL_DEFS)
732             bitmap_set_bit (names_defined_in_bb, SSA_NAME_VERSION (op));
733         }
734
735       bitmap_clear (names_defined_in_bb);
736     }
737
738   /* Finally, verify alias information.  */
739   if (gimple_aliases_computed_p (cfun))
740     verify_alias_info ();
741
742   free (definition_block);
743
744   /* Restore the dominance information to its prior known state, so
745      that we do not perturb the compiler's subsequent behavior.  */
746   if (orig_dom_state == DOM_NONE)
747     free_dominance_info (CDI_DOMINATORS);
748   else
749     set_dom_info_availability (CDI_DOMINATORS, orig_dom_state);
750   
751   BITMAP_FREE (names_defined_in_bb);
752   timevar_pop (TV_TREE_SSA_VERIFY);
753   return;
754
755 err:
756   internal_error ("verify_ssa failed");
757 }
758
759 /* Return true if the uid in both int tree maps are equal.  */
760
761 int
762 int_tree_map_eq (const void *va, const void *vb)
763 {
764   const struct int_tree_map *a = (const struct int_tree_map *) va;
765   const struct int_tree_map *b = (const struct int_tree_map *) vb;
766   return (a->uid == b->uid);
767 }
768
769 /* Hash a UID in a int_tree_map.  */
770
771 unsigned int
772 int_tree_map_hash (const void *item)
773 {
774   return ((const struct int_tree_map *)item)->uid;
775 }
776
777 /* Return true if the DECL_UID in both trees are equal.  */
778
779 int
780 uid_decl_map_eq (const void *va, const void *vb)
781 {
782   const_tree a = (const_tree) va;
783   const_tree b = (const_tree) vb;
784   return (a->decl_minimal.uid == b->decl_minimal.uid);
785 }
786
787 /* Hash a tree in a uid_decl_map.  */
788
789 unsigned int
790 uid_decl_map_hash (const void *item)
791 {
792   return ((const_tree)item)->decl_minimal.uid;
793 }
794
795 /* Return true if the uid in both int tree maps are equal.  */
796
797 static int
798 var_ann_eq (const void *va, const void *vb)
799 {
800   const struct static_var_ann_d *a = (const struct static_var_ann_d *) va;
801   const_tree const b = (const_tree) vb;
802   return (a->uid == DECL_UID (b));
803 }
804
805 /* Hash a UID in a int_tree_map.  */
806
807 static unsigned int
808 var_ann_hash (const void *item)
809 {
810   return ((const struct static_var_ann_d *)item)->uid;
811 }
812
813 /* Return true if the DECL_UID in both trees are equal.  */
814
815 static int
816 uid_ssaname_map_eq (const void *va, const void *vb)
817 {
818   const_tree a = (const_tree) va;
819   const_tree b = (const_tree) vb;
820   return (a->ssa_name.var->decl_minimal.uid == b->ssa_name.var->decl_minimal.uid);
821 }
822
823 /* Hash a tree in a uid_decl_map.  */
824
825 static unsigned int
826 uid_ssaname_map_hash (const void *item)
827 {
828   return ((const_tree)item)->ssa_name.var->decl_minimal.uid;
829 }
830
831
832 /* Initialize global DFA and SSA structures.  */
833
834 void
835 init_tree_ssa (void)
836 {
837   cfun->gimple_df = GGC_CNEW (struct gimple_df);
838   cfun->gimple_df->referenced_vars = htab_create_ggc (20, uid_decl_map_hash, 
839                                                       uid_decl_map_eq, NULL);
840   cfun->gimple_df->default_defs = htab_create_ggc (20, uid_ssaname_map_hash, 
841                                                    uid_ssaname_map_eq, NULL);
842   cfun->gimple_df->var_anns = htab_create_ggc (20, var_ann_hash, 
843                                                var_ann_eq, NULL);
844   cfun->gimple_df->call_clobbered_vars = BITMAP_GGC_ALLOC ();
845   cfun->gimple_df->addressable_vars = BITMAP_GGC_ALLOC ();
846   init_ssanames ();
847   init_phinodes ();
848 }
849
850
851 /* Deallocate memory associated with SSA data structures for FNDECL.  */
852
853 void
854 delete_tree_ssa (void)
855 {
856   size_t i;
857   basic_block bb;
858   block_stmt_iterator bsi;
859   referenced_var_iterator rvi;
860   tree var;
861
862   /* Release any ssa_names still in use.  */
863   for (i = 0; i < num_ssa_names; i++)
864     {
865       tree var = ssa_name (i);
866       if (var && TREE_CODE (var) == SSA_NAME)
867         {
868           SSA_NAME_IMM_USE_NODE (var).prev = &(SSA_NAME_IMM_USE_NODE (var));
869           SSA_NAME_IMM_USE_NODE (var).next = &(SSA_NAME_IMM_USE_NODE (var));
870         }
871       release_ssa_name (var);
872     }
873
874   /* Remove annotations from every tree in the function.  */
875   FOR_EACH_BB (bb)
876     {
877       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
878         {
879           tree stmt = bsi_stmt (bsi);
880           stmt_ann_t ann = get_stmt_ann (stmt);
881
882           free_ssa_operands (&ann->operands);
883           ann->addresses_taken = 0;
884           mark_stmt_modified (stmt);
885         }
886       set_phi_nodes (bb, NULL);
887     }
888
889   /* Remove annotations from every referenced variable.  */
890   FOR_EACH_REFERENCED_VAR (var, rvi)
891     {
892       if (var->base.ann)
893         ggc_free (var->base.ann);
894       var->base.ann = NULL;
895     }
896   htab_delete (gimple_referenced_vars (cfun));
897   cfun->gimple_df->referenced_vars = NULL;
898
899   fini_ssanames ();
900   fini_phinodes ();
901   /* we no longer maintain the SSA operand cache at this point.  */
902   fini_ssa_operands ();
903
904   cfun->gimple_df->global_var = NULL_TREE;
905   
906   htab_delete (cfun->gimple_df->default_defs);
907   cfun->gimple_df->default_defs = NULL;
908   htab_delete (cfun->gimple_df->var_anns);
909   cfun->gimple_df->var_anns = NULL;
910   cfun->gimple_df->call_clobbered_vars = NULL;
911   cfun->gimple_df->addressable_vars = NULL;
912   cfun->gimple_df->modified_noreturn_calls = NULL;
913   if (gimple_aliases_computed_p (cfun))
914     {
915       delete_alias_heapvars ();
916       gcc_assert (!need_ssa_update_p ());
917     }
918   cfun->gimple_df->aliases_computed_p = false;
919   delete_mem_ref_stats (cfun);
920
921   cfun->gimple_df = NULL;
922 }
923
924 /* Helper function for useless_type_conversion_p.  */
925
926 static bool
927 useless_type_conversion_p_1 (tree outer_type, tree inner_type)
928 {
929   /* Qualifiers on value types do not matter.  */
930   inner_type = TYPE_MAIN_VARIANT (inner_type);
931   outer_type = TYPE_MAIN_VARIANT (outer_type);
932
933   if (inner_type == outer_type)
934     return true;
935
936   /* If we know the canonical types, compare them.  */
937   if (TYPE_CANONICAL (inner_type)
938       && TYPE_CANONICAL (inner_type) == TYPE_CANONICAL (outer_type))
939     return true;
940
941   /* Changes in machine mode are never useless conversions.  */
942   if (TYPE_MODE (inner_type) != TYPE_MODE (outer_type))
943     return false;
944
945   /* If both the inner and outer types are integral types, then the
946      conversion is not necessary if they have the same mode and
947      signedness and precision, and both or neither are boolean.  */
948   if (INTEGRAL_TYPE_P (inner_type)
949       && INTEGRAL_TYPE_P (outer_type))
950     {
951       /* Preserve changes in signedness or precision.  */
952       if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
953           || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
954         return false;
955
956       /* Conversions from a non-base to a base type are not useless.
957          This way we preserve the invariant to do arithmetic in
958          base types only.  */
959       if (TREE_TYPE (inner_type)
960           && TREE_TYPE (inner_type) != inner_type
961           && (TREE_TYPE (outer_type) == outer_type
962               || TREE_TYPE (outer_type) == NULL_TREE))
963         return false;
964
965       /* We don't need to preserve changes in the types minimum or
966          maximum value in general as these do not generate code
967          unless the types precisions are different.  */
968
969       return true;
970     }
971
972   /* Scalar floating point types with the same mode are compatible.  */
973   else if (SCALAR_FLOAT_TYPE_P (inner_type)
974            && SCALAR_FLOAT_TYPE_P (outer_type))
975     return true;
976
977   /* We need to take special care recursing to pointed-to types.  */
978   else if (POINTER_TYPE_P (inner_type)
979            && POINTER_TYPE_P (outer_type))
980     {
981       /* Don't lose casts between pointers to volatile and non-volatile
982          qualified types.  Doing so would result in changing the semantics
983          of later accesses.  */
984       if ((TYPE_VOLATILE (TREE_TYPE (outer_type))
985            != TYPE_VOLATILE (TREE_TYPE (inner_type)))
986           && TYPE_VOLATILE (TREE_TYPE (outer_type)))
987         return false;
988
989       /* Do not lose casts between pointers with different
990          TYPE_REF_CAN_ALIAS_ALL setting or alias sets.  */
991       if ((TYPE_REF_CAN_ALIAS_ALL (inner_type)
992            != TYPE_REF_CAN_ALIAS_ALL (outer_type))
993           || (get_alias_set (TREE_TYPE (inner_type))
994               != get_alias_set (TREE_TYPE (outer_type))))
995         return false;
996
997       /* Do not lose casts from const qualified to non-const
998          qualified.  */
999       if ((TYPE_READONLY (TREE_TYPE (outer_type))
1000            != TYPE_READONLY (TREE_TYPE (inner_type)))
1001           && TYPE_READONLY (TREE_TYPE (inner_type)))
1002         return false;
1003
1004       /* Do not lose casts to restrict qualified pointers.  */
1005       if ((TYPE_RESTRICT (outer_type)
1006            != TYPE_RESTRICT (inner_type))
1007           && TYPE_RESTRICT (outer_type))
1008         return false;
1009
1010       /* Otherwise pointers/references are equivalent if their pointed
1011          to types are effectively the same.  We can strip qualifiers
1012          on pointed-to types for further comparison, which is done in
1013          the callee.  */
1014       return useless_type_conversion_p_1 (TREE_TYPE (outer_type),
1015                                           TREE_TYPE (inner_type));
1016     }
1017
1018   /* Recurse for complex types.  */
1019   else if (TREE_CODE (inner_type) == COMPLEX_TYPE
1020            && TREE_CODE (outer_type) == COMPLEX_TYPE)
1021     return useless_type_conversion_p_1 (TREE_TYPE (outer_type),
1022                                         TREE_TYPE (inner_type));
1023
1024   /* Recurse for vector types with the same number of subparts.  */
1025   else if (TREE_CODE (inner_type) == VECTOR_TYPE
1026            && TREE_CODE (outer_type) == VECTOR_TYPE
1027            && TYPE_PRECISION (inner_type) == TYPE_PRECISION (outer_type))
1028     return useless_type_conversion_p_1 (TREE_TYPE (outer_type),
1029                                         TREE_TYPE (inner_type));
1030
1031   /* For aggregates we may need to fall back to structural equality
1032      checks.  */
1033   else if (AGGREGATE_TYPE_P (inner_type)
1034            && AGGREGATE_TYPE_P (outer_type))
1035     {
1036       /* Different types of aggregates are incompatible.  */
1037       if (TREE_CODE (inner_type) != TREE_CODE (outer_type))
1038         return false;
1039
1040       /* ???  Add structural equivalence check.  */
1041
1042       /* ???  This should eventually just return false.  */
1043       return lang_hooks.types_compatible_p (inner_type, outer_type);
1044     }
1045
1046   return false;
1047 }
1048
1049 /* Return true if the conversion from INNER_TYPE to OUTER_TYPE is a
1050    useless type conversion, otherwise return false.
1051
1052    This function implicitly defines the middle-end type system.  With
1053    the notion of 'a < b' meaning that useless_type_conversion_p (a, b)
1054    holds and 'a > b' meaning that useless_type_conversion_p (b, a) holds,
1055    the following invariants shall be fulfilled:
1056
1057      1) useless_type_conversion_p is transitive.
1058         If a < b and b < c then a < c.
1059
1060      2) useless_type_conversion_p is not symmetric.
1061         From a < b does not follow a > b.
1062
1063      3) Types define the available set of operations applicable to values.
1064         A type conversion is useless if the operations for the target type
1065         is a subset of the operations for the source type.  For example
1066         casts to void* are useless, casts from void* are not (void* can't
1067         be dereferenced or offsetted, but copied, hence its set of operations
1068         is a strict subset of that of all other data pointer types).  Casts
1069         to const T* are useless (can't be written to), casts from const T*
1070         to T* are not.  */
1071
1072 bool
1073 useless_type_conversion_p (tree outer_type, tree inner_type)
1074 {
1075   /* If the outer type is (void *), then the conversion is not
1076      necessary.  We have to make sure to not apply this while
1077      recursing though.  */
1078   if (POINTER_TYPE_P (inner_type)
1079       && POINTER_TYPE_P (outer_type)
1080       && TREE_CODE (TREE_TYPE (outer_type)) == VOID_TYPE)
1081     return true;
1082
1083   return useless_type_conversion_p_1 (outer_type, inner_type);
1084 }
1085
1086 /* Return true if a conversion from either type of TYPE1 and TYPE2
1087    to the other is not required.  Otherwise return false.  */
1088
1089 bool
1090 types_compatible_p (tree type1, tree type2)
1091 {
1092   return (type1 == type2
1093           || (useless_type_conversion_p (type1, type2)
1094               && useless_type_conversion_p (type2, type1)));
1095 }
1096
1097 /* Return true if EXPR is a useless type conversion, otherwise return
1098    false.  */
1099
1100 bool
1101 tree_ssa_useless_type_conversion (tree expr)
1102 {
1103   /* If we have an assignment that merely uses a NOP_EXPR to change
1104      the top of the RHS to the type of the LHS and the type conversion
1105      is "safe", then strip away the type conversion so that we can
1106      enter LHS = RHS into the const_and_copies table.  */
1107   if (TREE_CODE (expr) == NOP_EXPR || TREE_CODE (expr) == CONVERT_EXPR
1108       || TREE_CODE (expr) == VIEW_CONVERT_EXPR
1109       || TREE_CODE (expr) == NON_LVALUE_EXPR)
1110     /* FIXME: Use of GENERIC_TREE_TYPE here is a temporary measure to work
1111        around known bugs with GIMPLE_MODIFY_STMTs appearing in places
1112        they shouldn't.  See PR 30391.  */
1113     return useless_type_conversion_p
1114       (TREE_TYPE (expr),
1115        GENERIC_TREE_TYPE (TREE_OPERAND (expr, 0)));
1116
1117   return false;
1118 }
1119
1120
1121 /* Internal helper for walk_use_def_chains.  VAR, FN and DATA are as
1122    described in walk_use_def_chains.
1123    
1124    VISITED is a pointer set used to mark visited SSA_NAMEs to avoid
1125       infinite loops.  We used to have a bitmap for this to just mark
1126       SSA versions we had visited.  But non-sparse bitmaps are way too
1127       expensive, while sparse bitmaps may cause quadratic behavior.
1128
1129    IS_DFS is true if the caller wants to perform a depth-first search
1130       when visiting PHI nodes.  A DFS will visit each PHI argument and
1131       call FN after each one.  Otherwise, all the arguments are
1132       visited first and then FN is called with each of the visited
1133       arguments in a separate pass.  */
1134
1135 static bool
1136 walk_use_def_chains_1 (tree var, walk_use_def_chains_fn fn, void *data,
1137                        struct pointer_set_t *visited, bool is_dfs)
1138 {
1139   tree def_stmt;
1140
1141   if (pointer_set_insert (visited, var))
1142     return false;
1143
1144   def_stmt = SSA_NAME_DEF_STMT (var);
1145
1146   if (TREE_CODE (def_stmt) != PHI_NODE)
1147     {
1148       /* If we reached the end of the use-def chain, call FN.  */
1149       return fn (var, def_stmt, data);
1150     }
1151   else
1152     {
1153       int i;
1154
1155       /* When doing a breadth-first search, call FN before following the
1156          use-def links for each argument.  */
1157       if (!is_dfs)
1158         for (i = 0; i < PHI_NUM_ARGS (def_stmt); i++)
1159           if (fn (PHI_ARG_DEF (def_stmt, i), def_stmt, data))
1160             return true;
1161
1162       /* Follow use-def links out of each PHI argument.  */
1163       for (i = 0; i < PHI_NUM_ARGS (def_stmt); i++)
1164         {
1165           tree arg = PHI_ARG_DEF (def_stmt, i);
1166
1167           /* ARG may be NULL for newly introduced PHI nodes.  */
1168           if (arg
1169               && TREE_CODE (arg) == SSA_NAME
1170               && walk_use_def_chains_1 (arg, fn, data, visited, is_dfs))
1171             return true;
1172         }
1173
1174       /* When doing a depth-first search, call FN after following the
1175          use-def links for each argument.  */
1176       if (is_dfs)
1177         for (i = 0; i < PHI_NUM_ARGS (def_stmt); i++)
1178           if (fn (PHI_ARG_DEF (def_stmt, i), def_stmt, data))
1179             return true;
1180     }
1181   
1182   return false;
1183 }
1184   
1185
1186
1187 /* Walk use-def chains starting at the SSA variable VAR.  Call
1188    function FN at each reaching definition found.  FN takes three
1189    arguments: VAR, its defining statement (DEF_STMT) and a generic
1190    pointer to whatever state information that FN may want to maintain
1191    (DATA).  FN is able to stop the walk by returning true, otherwise
1192    in order to continue the walk, FN should return false.  
1193
1194    Note, that if DEF_STMT is a PHI node, the semantics are slightly
1195    different.  The first argument to FN is no longer the original
1196    variable VAR, but the PHI argument currently being examined.  If FN
1197    wants to get at VAR, it should call PHI_RESULT (PHI).
1198
1199    If IS_DFS is true, this function will:
1200
1201         1- walk the use-def chains for all the PHI arguments, and,
1202         2- call (*FN) (ARG, PHI, DATA) on all the PHI arguments.
1203
1204    If IS_DFS is false, the two steps above are done in reverse order
1205    (i.e., a breadth-first search).  */
1206
1207 void
1208 walk_use_def_chains (tree var, walk_use_def_chains_fn fn, void *data,
1209                      bool is_dfs)
1210 {
1211   tree def_stmt;
1212
1213   gcc_assert (TREE_CODE (var) == SSA_NAME);
1214
1215   def_stmt = SSA_NAME_DEF_STMT (var);
1216
1217   /* We only need to recurse if the reaching definition comes from a PHI
1218      node.  */
1219   if (TREE_CODE (def_stmt) != PHI_NODE)
1220     (*fn) (var, def_stmt, data);
1221   else
1222     {
1223       struct pointer_set_t *visited = pointer_set_create ();
1224       walk_use_def_chains_1 (var, fn, data, visited, is_dfs);
1225       pointer_set_destroy (visited);
1226     }
1227 }
1228
1229 \f
1230 /* Emit warnings for uninitialized variables.  This is done in two passes.
1231
1232    The first pass notices real uses of SSA names with default definitions.
1233    Such uses are unconditionally uninitialized, and we can be certain that
1234    such a use is a mistake.  This pass is run before most optimizations,
1235    so that we catch as many as we can.
1236
1237    The second pass follows PHI nodes to find uses that are potentially
1238    uninitialized.  In this case we can't necessarily prove that the use
1239    is really uninitialized.  This pass is run after most optimizations,
1240    so that we thread as many jumps and possible, and delete as much dead
1241    code as possible, in order to reduce false positives.  We also look
1242    again for plain uninitialized variables, since optimization may have
1243    changed conditionally uninitialized to unconditionally uninitialized.  */
1244
1245 /* Emit a warning for T, an SSA_NAME, being uninitialized.  The exact
1246    warning text is in MSGID and LOCUS may contain a location or be null.  */
1247
1248 static void
1249 warn_uninit (tree t, const char *gmsgid, void *data)
1250 {
1251   tree var = SSA_NAME_VAR (t);
1252   tree def = SSA_NAME_DEF_STMT (t);
1253   tree context = (tree) data;
1254   location_t *locus;
1255   expanded_location xloc, floc;
1256
1257   /* Default uses (indicated by an empty definition statement),
1258      are uninitialized.  */
1259   if (!IS_EMPTY_STMT (def))
1260     return;
1261
1262   /* Except for PARMs of course, which are always initialized.  */
1263   if (TREE_CODE (var) == PARM_DECL)
1264     return;
1265
1266   /* Hard register variables get their initial value from the ether.  */
1267   if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
1268     return;
1269
1270   /* TREE_NO_WARNING either means we already warned, or the front end
1271      wishes to suppress the warning.  */
1272   if (TREE_NO_WARNING (var))
1273     return;
1274
1275   locus = (context != NULL && EXPR_HAS_LOCATION (context)
1276            ? EXPR_LOCUS (context)
1277            : &DECL_SOURCE_LOCATION (var));
1278   warning (OPT_Wuninitialized, gmsgid, locus, var);
1279   xloc = expand_location (*locus);
1280   floc = expand_location (DECL_SOURCE_LOCATION (cfun->decl));
1281   if (xloc.file != floc.file
1282       || xloc.line < floc.line
1283       || xloc.line > LOCATION_LINE (cfun->function_end_locus))
1284     inform ("%J%qD was declared here", var, var);
1285
1286   TREE_NO_WARNING (var) = 1;
1287 }
1288    
1289 /* Called via walk_tree, look for SSA_NAMEs that have empty definitions
1290    and warn about them.  */
1291
1292 static tree
1293 warn_uninitialized_var (tree *tp, int *walk_subtrees, void *data)
1294 {
1295   tree t = *tp;
1296
1297   switch (TREE_CODE (t))
1298     {
1299     case SSA_NAME:
1300       /* We only do data flow with SSA_NAMEs, so that's all we
1301          can warn about.  */
1302       warn_uninit (t, "%H%qD is used uninitialized in this function", data);
1303       *walk_subtrees = 0;
1304       break;
1305
1306     case REALPART_EXPR:
1307     case IMAGPART_EXPR:
1308       /* The total store transformation performed during gimplification
1309          creates uninitialized variable uses.  If all is well, these will
1310          be optimized away, so don't warn now.  */
1311       if (TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME)
1312         *walk_subtrees = 0;
1313       break;
1314
1315     default:
1316       if (IS_TYPE_OR_DECL_P (t))
1317         *walk_subtrees = 0;
1318       break;
1319     }
1320
1321   return NULL_TREE;
1322 }
1323
1324 /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions
1325    and warn about them.  */
1326
1327 static void
1328 warn_uninitialized_phi (tree phi)
1329 {
1330   int i, n = PHI_NUM_ARGS (phi);
1331
1332   /* Don't look at memory tags.  */
1333   if (!is_gimple_reg (PHI_RESULT (phi)))
1334     return;
1335
1336   for (i = 0; i < n; ++i)
1337     {
1338       tree op = PHI_ARG_DEF (phi, i);
1339       if (TREE_CODE (op) == SSA_NAME)
1340         warn_uninit (op, "%H%qD may be used uninitialized in this function",
1341                      NULL);
1342     }
1343 }
1344
1345 static unsigned int
1346 execute_early_warn_uninitialized (void)
1347 {
1348   block_stmt_iterator bsi;
1349   basic_block bb;
1350
1351   FOR_EACH_BB (bb)
1352     for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1353       {
1354         tree context = bsi_stmt (bsi);
1355         walk_tree (bsi_stmt_ptr (bsi), warn_uninitialized_var,
1356                    context, NULL);
1357       }
1358   return 0;
1359 }
1360
1361 static unsigned int
1362 execute_late_warn_uninitialized (void)
1363 {
1364   basic_block bb;
1365   tree phi;
1366
1367   /* Re-do the plain uninitialized variable check, as optimization may have
1368      straightened control flow.  Do this first so that we don't accidentally
1369      get a "may be" warning when we'd have seen an "is" warning later.  */
1370   execute_early_warn_uninitialized ();
1371
1372   FOR_EACH_BB (bb)
1373     for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1374       warn_uninitialized_phi (phi);
1375   return 0;
1376 }
1377
1378 static bool
1379 gate_warn_uninitialized (void)
1380 {
1381   return warn_uninitialized != 0;
1382 }
1383
1384 struct tree_opt_pass pass_early_warn_uninitialized =
1385 {
1386   NULL,                                 /* name */
1387   gate_warn_uninitialized,              /* gate */
1388   execute_early_warn_uninitialized,     /* execute */
1389   NULL,                                 /* sub */
1390   NULL,                                 /* next */
1391   0,                                    /* static_pass_number */
1392   0,                                    /* tv_id */
1393   PROP_ssa,                             /* properties_required */
1394   0,                                    /* properties_provided */
1395   0,                                    /* properties_destroyed */
1396   0,                                    /* todo_flags_start */
1397   0,                                    /* todo_flags_finish */
1398   0                                     /* letter */
1399 };
1400
1401 struct tree_opt_pass pass_late_warn_uninitialized =
1402 {
1403   NULL,                                 /* name */
1404   gate_warn_uninitialized,              /* gate */
1405   execute_late_warn_uninitialized,      /* execute */
1406   NULL,                                 /* sub */
1407   NULL,                                 /* next */
1408   0,                                    /* static_pass_number */
1409   0,                                    /* tv_id */
1410   PROP_ssa,                             /* properties_required */
1411   0,                                    /* properties_provided */
1412   0,                                    /* properties_destroyed */
1413   0,                                    /* todo_flags_start */
1414   0,                                    /* todo_flags_finish */
1415   0                                     /* letter */
1416 };
1417
1418 /* Compute TREE_ADDRESSABLE for local variables.  */
1419
1420 static unsigned int
1421 execute_update_addresses_taken (void)
1422 {
1423   tree var;
1424   referenced_var_iterator rvi;
1425   block_stmt_iterator bsi;
1426   basic_block bb;
1427   bitmap addresses_taken = BITMAP_ALLOC (NULL);
1428   bitmap vars_updated = BITMAP_ALLOC (NULL);
1429   bool update_vops;
1430   tree phi;
1431
1432   /* Collect into ADDRESSES_TAKEN all variables whose address is taken within
1433      the function body.  */
1434   FOR_EACH_BB (bb)
1435     {
1436       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1437         {
1438           stmt_ann_t s_ann = stmt_ann (bsi_stmt (bsi));
1439
1440           if (s_ann->addresses_taken)
1441             bitmap_ior_into (addresses_taken, s_ann->addresses_taken);
1442         }
1443       for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1444         {
1445           unsigned i, phi_num_args = PHI_NUM_ARGS (phi);
1446           for (i = 0; i < phi_num_args; i++)
1447             {
1448               tree op = PHI_ARG_DEF (phi, i), var;
1449               if (TREE_CODE (op) == ADDR_EXPR
1450                   && (var = get_base_address (TREE_OPERAND (op, 0))) != NULL_TREE
1451                   && DECL_P (var))
1452                 bitmap_set_bit (addresses_taken, DECL_UID (var));
1453             }
1454         }
1455     }
1456
1457   /* When possible, clear ADDRESSABLE bit and mark variable for conversion into
1458      SSA.  */
1459   FOR_EACH_REFERENCED_VAR (var, rvi)
1460     if (!is_global_var (var)
1461         && TREE_CODE (var) != RESULT_DECL
1462         && TREE_ADDRESSABLE (var)
1463         && !bitmap_bit_p (addresses_taken, DECL_UID (var)))
1464       {
1465         TREE_ADDRESSABLE (var) = 0;
1466         if (is_gimple_reg (var))
1467           mark_sym_for_renaming (var);
1468         update_vops = true;
1469         bitmap_set_bit (vars_updated, DECL_UID (var));
1470         if (dump_file)
1471           {
1472             fprintf (dump_file, "No longer having address taken ");
1473             print_generic_expr (dump_file, var, 0);
1474             fprintf (dump_file, "\n");
1475           }
1476       }
1477
1478   /* Operand caches needs to be recomputed for operands referencing the updated
1479      variables.  */
1480   if (update_vops)
1481     FOR_EACH_BB (bb)
1482       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1483         {
1484           tree stmt = bsi_stmt (bsi);
1485
1486           if ((LOADED_SYMS (stmt)
1487                && bitmap_intersect_p (LOADED_SYMS (stmt), vars_updated))
1488               || (STORED_SYMS (stmt)
1489                   && bitmap_intersect_p (STORED_SYMS (stmt), vars_updated)))
1490             update_stmt (stmt);
1491         }
1492   BITMAP_FREE (addresses_taken);
1493   BITMAP_FREE (vars_updated);
1494   return 0;
1495 }
1496
1497 struct tree_opt_pass pass_update_address_taken =
1498 {
1499   "addressables",                       /* name */
1500   NULL,                                 /* gate */
1501   execute_update_addresses_taken,       /* execute */
1502   NULL,                                 /* sub */
1503   NULL,                                 /* next */
1504   0,                                    /* static_pass_number */
1505   0,                                    /* tv_id */
1506   PROP_ssa,                             /* properties_required */
1507   0,                                    /* properties_provided */
1508   0,                                    /* properties_destroyed */
1509   0,                                    /* todo_flags_start */
1510   TODO_update_ssa,                      /* todo_flags_finish */
1511   0                                     /* letter */
1512 };