OSDN Git Service

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