OSDN Git Service

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