OSDN Git Service

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