OSDN Git Service

5b9b3ccd185253ce97a112a9857b2478c2b2084e
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa.c
1 /* Miscellaneous SSA utility functions.
2    Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
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 "errors.h"
35 #include "expr.h"
36 #include "function.h"
37 #include "diagnostic.h"
38 #include "bitmap.h"
39 #include "tree-flow.h"
40 #include "tree-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
48
49 /* Remove edge E and remove the corresponding arguments from the PHI nodes
50    in E's destination block.  */
51
52 void
53 ssa_remove_edge (edge e)
54 {
55   tree phi, next;
56
57   /* Remove the appropriate PHI arguments in E's destination block.  */
58   for (phi = phi_nodes (e->dest); phi; phi = next)
59     {
60       next = PHI_CHAIN (phi);
61       remove_phi_arg (phi, e->src);
62     }
63
64   remove_edge (e);
65 }
66
67 /* Remove the corresponding arguments from the PHI nodes in E's
68    destination block and redirect it to DEST.  Return redirected edge.
69    The list of removed arguments is stored in PENDING_STMT (e).  */
70
71 edge
72 ssa_redirect_edge (edge e, basic_block dest)
73 {
74   tree phi, next;
75   tree list = NULL, *last = &list;
76   tree src, dst, node;
77   int i;
78
79   /* Remove the appropriate PHI arguments in E's destination block.  */
80   for (phi = phi_nodes (e->dest); phi; phi = next)
81     {
82       next = PHI_CHAIN (phi);
83
84       i = phi_arg_from_edge (phi, e);
85       if (i < 0)
86         continue;
87
88       src = PHI_ARG_DEF (phi, i);
89       dst = PHI_RESULT (phi);
90       node = build_tree_list (dst, src);
91       *last = node;
92       last = &TREE_CHAIN (node);
93
94       remove_phi_arg_num (phi, i);
95     }
96
97   e = redirect_edge_succ_nodup (e, dest);
98   PENDING_STMT (e) = list;
99
100   return e;
101 }
102
103
104 /* Return true if SSA_NAME is malformed and mark it visited.
105
106    IS_VIRTUAL is true if this SSA_NAME was found inside a virtual
107       operand.  */
108
109 static bool
110 verify_ssa_name (tree ssa_name, bool is_virtual)
111 {
112   TREE_VISITED (ssa_name) = 1;
113
114   if (TREE_CODE (ssa_name) != SSA_NAME)
115     {
116       error ("Expected an SSA_NAME object");
117       return true;
118     }
119
120   if (TREE_TYPE (ssa_name) != TREE_TYPE (SSA_NAME_VAR (ssa_name)))
121     {
122       error ("Type mismatch between an SSA_NAME and its symbol.");
123       return true;
124     }
125
126   if (SSA_NAME_IN_FREE_LIST (ssa_name))
127     {
128       error ("Found an SSA_NAME that had been released into the free pool");
129       return true;
130     }
131
132   if (is_virtual && is_gimple_reg (ssa_name))
133     {
134       error ("Found a virtual definition for a GIMPLE register");
135       return true;
136     }
137
138   if (!is_virtual && !is_gimple_reg (ssa_name))
139     {
140       error ("Found a real definition for a non-register");
141       return true;
142     }
143
144   return false;
145 }
146
147
148 /* Return true if the definition of SSA_NAME at block BB is malformed.
149
150    STMT is the statement where SSA_NAME is created.
151
152    DEFINITION_BLOCK is an array of basic blocks indexed by SSA_NAME
153       version numbers.  If DEFINITION_BLOCK[SSA_NAME_VERSION] is set,
154       it means that the block in that array slot contains the
155       definition of SSA_NAME.
156
157    IS_VIRTUAL is true if SSA_NAME is created by a V_MAY_DEF or a
158       V_MUST_DEF.  */
159
160 static bool
161 verify_def (basic_block bb, basic_block *definition_block, tree ssa_name,
162             tree stmt, bool is_virtual)
163 {
164   if (verify_ssa_name (ssa_name, is_virtual))
165     goto err;
166
167   if (definition_block[SSA_NAME_VERSION (ssa_name)])
168     {
169       error ("SSA_NAME created in two different blocks %i and %i",
170              definition_block[SSA_NAME_VERSION (ssa_name)]->index, bb->index);
171       goto err;
172     }
173
174   definition_block[SSA_NAME_VERSION (ssa_name)] = bb;
175
176   if (SSA_NAME_DEF_STMT (ssa_name) != stmt)
177     {
178       error ("SSA_NAME_DEF_STMT is wrong");
179       fprintf (stderr, "Expected definition statement:\n");
180       print_generic_stmt (stderr, SSA_NAME_DEF_STMT (ssa_name), TDF_VOPS);
181       fprintf (stderr, "\nActual definition statement:\n");
182       print_generic_stmt (stderr, stmt, TDF_VOPS);
183       goto err;
184     }
185
186   return false;
187
188 err:
189   fprintf (stderr, "while verifying SSA_NAME ");
190   print_generic_expr (stderr, ssa_name, 0);
191   fprintf (stderr, " in statement\n");
192   print_generic_stmt (stderr, stmt, TDF_VOPS);
193
194   return true;
195 }
196
197
198 /* Return true if the use of SSA_NAME at statement STMT in block BB is
199    malformed.
200
201    DEF_BB is the block where SSA_NAME was found to be created.
202
203    IDOM contains immediate dominator information for the flowgraph.
204
205    CHECK_ABNORMAL is true if the caller wants to check whether this use
206       is flowing through an abnormal edge (only used when checking PHI
207       arguments).
208
209    IS_VIRTUAL is true if SSA_NAME is created by a V_MAY_DEF or a
210       V_MUST_DEF.
211    
212    If NAMES_DEFINED_IN_BB is not NULL, it contains a bitmap of ssa names
213      that are defined before STMT in basic block BB.  */
214
215 static bool
216 verify_use (basic_block bb, basic_block def_bb, tree ssa_name,
217             tree stmt, bool check_abnormal, bool is_virtual,
218             bitmap names_defined_in_bb)
219 {
220   bool err = false;
221
222   err = verify_ssa_name (ssa_name, is_virtual);
223
224   if (IS_EMPTY_STMT (SSA_NAME_DEF_STMT (ssa_name))
225       && var_ann (SSA_NAME_VAR (ssa_name))->default_def == ssa_name)
226     ; /* Default definitions have empty statements.  Nothing to do.  */
227   else if (!def_bb)
228     {
229       error ("Missing definition");
230       err = true;
231     }
232   else if (bb != def_bb
233            && !dominated_by_p (CDI_DOMINATORS, bb, def_bb))
234     {
235       error ("Definition in block %i does not dominate use in block %i",
236              def_bb->index, bb->index);
237       err = true;
238     }
239   else if (bb == def_bb
240            && names_defined_in_bb != NULL
241            && !bitmap_bit_p (names_defined_in_bb, SSA_NAME_VERSION (ssa_name)))
242     {
243       error ("Definition in block %i follows the use", def_bb->index);
244       err = true;
245     }
246
247   if (check_abnormal
248       && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
249     {
250       error ("SSA_NAME_OCCURS_IN_ABNORMAL_PHI should be set");
251       err = true;
252     }
253
254   if (err)
255     {
256       fprintf (stderr, "for SSA_NAME: ");
257       print_generic_expr (stderr, ssa_name, TDF_VOPS);
258       fprintf (stderr, "in statement:\n");
259       print_generic_stmt (stderr, stmt, TDF_VOPS);
260     }
261
262   return err;
263 }
264
265
266 /* Return true if any of the arguments for PHI node PHI at block BB is
267    malformed.
268
269    IDOM contains immediate dominator information for the flowgraph.
270
271    DEFINITION_BLOCK is an array of basic blocks indexed by SSA_NAME version
272       numbers.  If DEFINITION_BLOCK[SSA_NAME_VERSION] is set, it means that the
273       block in that array slot contains the definition of SSA_NAME.  */
274
275 static bool
276 verify_phi_args (tree phi, basic_block bb, basic_block *definition_block)
277 {
278   edge e;
279   bool err = false;
280   int i, phi_num_args = PHI_NUM_ARGS (phi);
281   edge_iterator ei;
282
283   /* Mark all the incoming edges.  */
284   FOR_EACH_EDGE (e, ei, bb->preds)
285     e->aux = (void *) 1;
286
287   for (i = 0; i < phi_num_args; i++)
288     {
289       tree op = PHI_ARG_DEF (phi, i);
290
291       e = PHI_ARG_EDGE (phi, i);
292
293       if (TREE_CODE (op) == SSA_NAME)
294         err = verify_use (e->src, definition_block[SSA_NAME_VERSION (op)], op,
295                           phi, e->flags & EDGE_ABNORMAL,
296                           !is_gimple_reg (PHI_RESULT (phi)),
297                           NULL);
298
299       if (e->dest != bb)
300         {
301           error ("Wrong edge %d->%d for PHI argument\n",
302                  e->src->index, e->dest->index, bb->index);
303           err = true;
304         }
305
306       if (e->aux == (void *) 0)
307         {
308           error ("PHI argument flowing through dead edge %d->%d\n",
309                  e->src->index, e->dest->index);
310           err = true;
311         }
312
313       if (e->aux == (void *) 2)
314         {
315           error ("PHI argument duplicated for edge %d->%d\n", e->src->index,
316                  e->dest->index);
317           err = true;
318         }
319
320       if (err)
321         {
322           fprintf (stderr, "PHI argument\n");
323           print_generic_stmt (stderr, op, TDF_VOPS);
324           goto error;
325         }
326
327       e->aux = (void *) 2;
328     }
329
330   FOR_EACH_EDGE (e, ei, bb->preds)
331     {
332       if (e->aux != (void *) 2)
333         {
334           error ("No argument flowing through edge %d->%d\n", e->src->index,
335                  e->dest->index);
336           err = true;
337           goto error;
338         }
339       e->aux = (void *) 0;
340     }
341
342 error:
343   if (err)
344     {
345       fprintf (stderr, "for PHI node\n");
346       print_generic_stmt (stderr, phi, TDF_VOPS);
347     }
348
349
350   return err;
351 }
352
353
354 static void
355 verify_flow_insensitive_alias_info (void)
356 {
357   size_t i;
358   tree var;
359   bitmap visited = BITMAP_XMALLOC ();
360
361   for (i = 0; i < num_referenced_vars; i++)
362     {
363       size_t j;
364       var_ann_t ann;
365       varray_type may_aliases;
366
367       var = referenced_var (i);
368       ann = var_ann (var);
369       may_aliases = ann->may_aliases;
370
371       for (j = 0; may_aliases && j < VARRAY_ACTIVE_SIZE (may_aliases); j++)
372         {
373           tree alias = VARRAY_TREE (may_aliases, j);
374
375           bitmap_set_bit (visited, var_ann (alias)->uid);
376
377           if (!may_be_aliased (alias))
378             {
379               error ("Non-addressable variable inside an alias set.");
380               debug_variable (alias);
381               goto err;
382             }
383         }
384     }
385
386   for (i = 0; i < num_referenced_vars; i++)
387     {
388       var_ann_t ann;
389
390       var = referenced_var (i);
391       ann = var_ann (var);
392
393       if (ann->mem_tag_kind == NOT_A_TAG
394           && ann->is_alias_tag
395           && !bitmap_bit_p (visited, ann->uid))
396         {
397           error ("Addressable variable that is an alias tag but is not in any alias set.");
398           goto err;
399         }
400     }
401
402   BITMAP_XFREE (visited);
403   return;
404
405 err:
406   debug_variable (var);
407   internal_error ("verify_flow_insensitive_alias_info failed.");
408 }
409
410
411 static void
412 verify_flow_sensitive_alias_info (void)
413 {
414   size_t i;
415   tree ptr;
416
417   for (i = 1; i < num_ssa_names; i++)
418     {
419       var_ann_t ann;
420       struct ptr_info_def *pi;
421
422       ptr = ssa_name (i);
423       if (!ptr)
424         continue;
425       ann = var_ann (SSA_NAME_VAR (ptr));
426       pi = SSA_NAME_PTR_INFO (ptr);
427
428       /* We only care for pointers that are actually referenced in the
429          program.  */
430       if (!TREE_VISITED (ptr) || !POINTER_TYPE_P (TREE_TYPE (ptr)))
431         continue;
432
433       /* RESULT_DECL is special.  If it's a GIMPLE register, then it
434          is only written-to only once in the return statement.
435          Otherwise, aggregate RESULT_DECLs may be written-to more than
436          once in virtual operands.  */
437       if (TREE_CODE (SSA_NAME_VAR (ptr)) == RESULT_DECL
438           && is_gimple_reg (ptr))
439         continue;
440
441       if (pi == NULL)
442         continue;
443
444       if (pi->is_dereferenced && !pi->name_mem_tag && !ann->type_mem_tag)
445         {
446           error ("Dereferenced pointers should have a name or a type tag");
447           goto err;
448         }
449
450       if (pi->name_mem_tag
451           && !pi->pt_malloc
452           && (pi->pt_vars == NULL
453               || bitmap_first_set_bit (pi->pt_vars) < 0))
454         {
455           error ("Pointers with a memory tag, should have points-to sets or point to malloc");
456           goto err;
457         }
458
459       if (pi->value_escapes_p
460           && pi->name_mem_tag
461           && !is_call_clobbered (pi->name_mem_tag))
462         {
463           error ("Pointer escapes but its name tag is not call-clobbered.");
464           goto err;
465         }
466
467       if (pi->name_mem_tag && pi->pt_vars)
468         {
469           size_t j;
470
471           for (j = i + 1; j < num_ssa_names; j++)
472             if (ssa_name (j))
473               {
474                 tree ptr2 = ssa_name (j);
475                 struct ptr_info_def *pi2 = SSA_NAME_PTR_INFO (ptr2);
476
477                 if (!TREE_VISITED (ptr2) || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
478                   continue;
479
480                 if (pi2
481                     && pi2->name_mem_tag
482                     && pi2->pt_vars
483                     && bitmap_first_set_bit (pi2->pt_vars) >= 0
484                     && pi->name_mem_tag != pi2->name_mem_tag
485                     && bitmap_equal_p (pi->pt_vars, pi2->pt_vars))
486                   {
487                     error ("Two pointers with different name tags and identical points-to sets");
488                     debug_variable (ptr2);
489                     goto err;
490                   }
491               }
492         }
493     }
494
495   return;
496
497 err:
498   debug_variable (ptr);
499   internal_error ("verify_flow_sensitive_alias_info failed.");
500 }
501
502
503 /* Verify the consistency of aliasing information.  */
504
505 static void
506 verify_alias_info (void)
507 {
508   verify_flow_sensitive_alias_info ();
509   verify_flow_insensitive_alias_info ();
510 }
511
512
513 /* Verify common invariants in the SSA web.
514    TODO: verify the variable annotations.  */
515
516 void
517 verify_ssa (void)
518 {
519   size_t i;
520   basic_block bb;
521   basic_block *definition_block = xcalloc (num_ssa_names, sizeof (basic_block));
522   ssa_op_iter iter;
523   tree op;
524   enum dom_state orig_dom_state = dom_computed[CDI_DOMINATORS];
525   bitmap names_defined_in_bb = BITMAP_XMALLOC ();
526
527   timevar_push (TV_TREE_SSA_VERIFY);
528
529   /* Keep track of SSA names present in the IL.  */
530   for (i = 1; i < num_ssa_names; i++)
531     if (ssa_name (i))
532       TREE_VISITED (ssa_name (i)) = 0;
533
534   calculate_dominance_info (CDI_DOMINATORS);
535
536   /* Verify and register all the SSA_NAME definitions found in the
537      function.  */
538   FOR_EACH_BB (bb)
539     {
540       tree phi;
541       block_stmt_iterator bsi;
542
543       for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
544         if (verify_def (bb, definition_block, PHI_RESULT (phi), phi,
545                         !is_gimple_reg (PHI_RESULT (phi))))
546           goto err;
547
548       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
549         {
550           tree stmt;
551
552           stmt = bsi_stmt (bsi);
553           get_stmt_operands (stmt);
554
555           if (stmt_ann (stmt)->makes_aliased_stores 
556               && NUM_V_MAY_DEFS (STMT_V_MAY_DEF_OPS (stmt)) == 0)
557             {
558               error ("Statement makes aliased stores, but has no V_MAY_DEFS");
559               print_generic_stmt (stderr, stmt, TDF_VOPS);
560               goto err;
561             }
562             
563           FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_VIRTUAL_DEFS)
564             {
565               if (verify_def (bb, definition_block, op, stmt, true))
566                 goto err;
567             }
568           
569           FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
570             {
571               if (verify_def (bb, definition_block, op, stmt, false))
572                 goto err;
573             }
574         }
575     }
576
577
578   /* Now verify all the uses and make sure they agree with the definitions
579      found in the previous pass.  */
580   FOR_EACH_BB (bb)
581     {
582       edge e;
583       tree phi;
584       edge_iterator ei;
585       block_stmt_iterator bsi;
586
587       /* Make sure that all edges have a clear 'aux' field.  */
588       FOR_EACH_EDGE (e, ei, bb->preds)
589         {
590           if (e->aux)
591             {
592               error ("AUX pointer initialized for edge %d->%d\n", e->src->index,
593                       e->dest->index);
594               goto err;
595             }
596         }
597
598       /* Verify the arguments for every PHI node in the block.  */
599       for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
600         {
601           if (verify_phi_args (phi, bb, definition_block))
602             goto err;
603           bitmap_set_bit (names_defined_in_bb,
604                           SSA_NAME_VERSION (PHI_RESULT (phi)));
605         }
606
607       /* Now verify all the uses and vuses in every statement of the block.  */
608       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
609         {
610           tree stmt = bsi_stmt (bsi);
611
612           FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_VIRTUAL_USES)
613             {
614               if (verify_use (bb, definition_block[SSA_NAME_VERSION (op)],
615                               op, stmt, false, true,
616                               names_defined_in_bb))
617                 goto err;
618             }
619
620           FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
621             {
622               if (verify_use (bb, definition_block[SSA_NAME_VERSION (op)],
623                               op, stmt, false, false,
624                               names_defined_in_bb))
625                 goto err;
626             }
627
628           FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_ALL_DEFS)
629             {
630               bitmap_set_bit (names_defined_in_bb, SSA_NAME_VERSION (op));
631             }
632         }
633
634       /* Verify the uses in arguments of PHI nodes at the exits from the
635          block.  */
636       FOR_EACH_EDGE (e, ei, bb->succs)
637         {
638           for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
639             {
640               bool virtual = !is_gimple_reg (PHI_RESULT (phi));
641               op = PHI_ARG_DEF_FROM_EDGE (phi, e);
642               if (TREE_CODE (op) != SSA_NAME)
643                 continue;
644
645               if (verify_use (bb, definition_block[SSA_NAME_VERSION (op)],
646                               op, phi, false, virtual,
647                               names_defined_in_bb))
648                 goto err;
649             }
650         }
651
652       bitmap_clear (names_defined_in_bb);
653     }
654
655   /* Finally, verify alias information.  */
656   verify_alias_info ();
657
658   free (definition_block);
659   /* Restore the dominance information to its prior known state, so
660      that we do not perturb the compiler's subsequent behavior.  */
661   if (orig_dom_state == DOM_NONE)
662     free_dominance_info (CDI_DOMINATORS);
663   else
664     dom_computed[CDI_DOMINATORS] = orig_dom_state;
665   
666   BITMAP_XFREE (names_defined_in_bb);
667   timevar_pop (TV_TREE_SSA_VERIFY);
668   return;
669
670 err:
671   internal_error ("verify_ssa failed.");
672 }
673
674
675 /* Initialize global DFA and SSA structures.  */
676
677 void
678 init_tree_ssa (void)
679 {
680   VARRAY_TREE_INIT (referenced_vars, 20, "referenced_vars");
681   call_clobbered_vars = BITMAP_XMALLOC ();
682   addressable_vars = BITMAP_XMALLOC ();
683   init_ssa_operands ();
684   init_ssanames ();
685   init_phinodes ();
686   global_var = NULL_TREE;
687 }
688
689
690 /* Deallocate memory associated with SSA data structures for FNDECL.  */
691
692 void
693 delete_tree_ssa (void)
694 {
695   size_t i;
696   basic_block bb;
697   block_stmt_iterator bsi;
698
699   /* Remove annotations from every tree in the function.  */
700   FOR_EACH_BB (bb)
701     for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
702       {
703         tree stmt = bsi_stmt (bsi);
704         release_defs (stmt);
705         ggc_free (stmt->common.ann);
706         stmt->common.ann = NULL;
707       }
708
709   /* Remove annotations from every referenced variable.  */
710   if (referenced_vars)
711     {
712       for (i = 0; i < num_referenced_vars; i++)
713         {
714           tree var = referenced_var (i);
715           ggc_free (var->common.ann);
716           var->common.ann = NULL;
717         }
718       referenced_vars = NULL;
719     }
720
721   fini_ssanames ();
722   fini_phinodes ();
723   fini_ssa_operands ();
724
725   global_var = NULL_TREE;
726   BITMAP_XFREE (call_clobbered_vars);
727   call_clobbered_vars = NULL;
728   BITMAP_XFREE (addressable_vars);
729   addressable_vars = NULL;
730 }
731
732
733 /* Return true if EXPR is a useless type conversion, otherwise return
734    false.  */
735
736 bool
737 tree_ssa_useless_type_conversion_1 (tree outer_type, tree inner_type)
738 {
739   /* If the inner and outer types are effectively the same, then
740      strip the type conversion and enter the equivalence into
741      the table.  */
742   if (inner_type == outer_type
743      || (lang_hooks.types_compatible_p (inner_type, outer_type)))
744     return true;
745
746   /* If both types are pointers and the outer type is a (void *), then
747      the conversion is not necessary.  The opposite is not true since
748      that conversion would result in a loss of information if the
749      equivalence was used.  Consider an indirect function call where
750      we need to know the exact type of the function to correctly
751      implement the ABI.  */
752   else if (POINTER_TYPE_P (inner_type)
753            && POINTER_TYPE_P (outer_type)
754            && TREE_CODE (TREE_TYPE (outer_type)) == VOID_TYPE)
755     return true;
756
757   /* Pointers and references are equivalent once we get to GENERIC,
758      so strip conversions that just switch between them.  */
759   else if (POINTER_TYPE_P (inner_type)
760            && POINTER_TYPE_P (outer_type)
761            && lang_hooks.types_compatible_p (TREE_TYPE (inner_type),
762                                              TREE_TYPE (outer_type)))
763     return true;
764
765   /* If both the inner and outer types are integral types, then the
766      conversion is not necessary if they have the same mode and
767      signedness and precision, and both or neither are boolean.  Some
768      code assumes an invariant that boolean types stay boolean and do
769      not become 1-bit bit-field types.  Note that types with precision
770      not using all bits of the mode (such as bit-field types in C)
771      mean that testing of precision is necessary.  */
772   else if (INTEGRAL_TYPE_P (inner_type)
773            && INTEGRAL_TYPE_P (outer_type)
774            && TYPE_MODE (inner_type) == TYPE_MODE (outer_type)
775            && TYPE_UNSIGNED (inner_type) == TYPE_UNSIGNED (outer_type)
776            && TYPE_PRECISION (inner_type) == TYPE_PRECISION (outer_type))
777     {
778       bool first_boolean = (TREE_CODE (inner_type) == BOOLEAN_TYPE);
779       bool second_boolean = (TREE_CODE (outer_type) == BOOLEAN_TYPE);
780       if (first_boolean == second_boolean)
781         return true;
782     }
783
784   /* Recurse for complex types.  */
785   else if (TREE_CODE (inner_type) == COMPLEX_TYPE
786            && TREE_CODE (outer_type) == COMPLEX_TYPE
787            && tree_ssa_useless_type_conversion_1 (TREE_TYPE (outer_type),
788                                                   TREE_TYPE (inner_type)))
789     return true;
790
791   return false;
792 }
793
794 /* Return true if EXPR is a useless type conversion, otherwise return
795    false.  */
796
797 bool
798 tree_ssa_useless_type_conversion (tree expr)
799 {
800   /* If we have an assignment that merely uses a NOP_EXPR to change
801      the top of the RHS to the type of the LHS and the type conversion
802      is "safe", then strip away the type conversion so that we can
803      enter LHS = RHS into the const_and_copies table.  */
804   if (TREE_CODE (expr) == NOP_EXPR || TREE_CODE (expr) == CONVERT_EXPR
805       || TREE_CODE (expr) == VIEW_CONVERT_EXPR
806       || TREE_CODE (expr) == NON_LVALUE_EXPR)
807     return tree_ssa_useless_type_conversion_1 (TREE_TYPE (expr),
808                                                TREE_TYPE (TREE_OPERAND (expr,
809                                                                         0)));
810
811
812   return false;
813 }
814
815
816 /* Internal helper for walk_use_def_chains.  VAR, FN and DATA are as
817    described in walk_use_def_chains.
818    
819    VISITED is a bitmap used to mark visited SSA_NAMEs to avoid
820       infinite loops.
821
822    IS_DFS is true if the caller wants to perform a depth-first search
823       when visiting PHI nodes.  A DFS will visit each PHI argument and
824       call FN after each one.  Otherwise, all the arguments are
825       visited first and then FN is called with each of the visited
826       arguments in a separate pass.  */
827
828 static bool
829 walk_use_def_chains_1 (tree var, walk_use_def_chains_fn fn, void *data,
830                        bitmap visited, bool is_dfs)
831 {
832   tree def_stmt;
833
834   if (bitmap_bit_p (visited, SSA_NAME_VERSION (var)))
835     return false;
836
837   bitmap_set_bit (visited, SSA_NAME_VERSION (var));
838
839   def_stmt = SSA_NAME_DEF_STMT (var);
840
841   if (TREE_CODE (def_stmt) != PHI_NODE)
842     {
843       /* If we reached the end of the use-def chain, call FN.  */
844       return fn (var, def_stmt, data);
845     }
846   else
847     {
848       int i;
849
850       /* When doing a breadth-first search, call FN before following the
851          use-def links for each argument.  */
852       if (!is_dfs)
853         for (i = 0; i < PHI_NUM_ARGS (def_stmt); i++)
854           if (fn (PHI_ARG_DEF (def_stmt, i), def_stmt, data))
855             return true;
856
857       /* Follow use-def links out of each PHI argument.  */
858       for (i = 0; i < PHI_NUM_ARGS (def_stmt); i++)
859         {
860           tree arg = PHI_ARG_DEF (def_stmt, i);
861           if (TREE_CODE (arg) == SSA_NAME
862               && walk_use_def_chains_1 (arg, fn, data, visited, is_dfs))
863             return true;
864         }
865
866       /* When doing a depth-first search, call FN after following the
867          use-def links for each argument.  */
868       if (is_dfs)
869         for (i = 0; i < PHI_NUM_ARGS (def_stmt); i++)
870           if (fn (PHI_ARG_DEF (def_stmt, i), def_stmt, data))
871             return true;
872     }
873   
874   return false;
875 }
876   
877
878
879 /* Walk use-def chains starting at the SSA variable VAR.  Call
880    function FN at each reaching definition found.  FN takes three
881    arguments: VAR, its defining statement (DEF_STMT) and a generic
882    pointer to whatever state information that FN may want to maintain
883    (DATA).  FN is able to stop the walk by returning true, otherwise
884    in order to continue the walk, FN should return false.  
885
886    Note, that if DEF_STMT is a PHI node, the semantics are slightly
887    different.  The first argument to FN is no longer the original
888    variable VAR, but the PHI argument currently being examined.  If FN
889    wants to get at VAR, it should call PHI_RESULT (PHI).
890
891    If IS_DFS is true, this function will:
892
893         1- walk the use-def chains for all the PHI arguments, and,
894         2- call (*FN) (ARG, PHI, DATA) on all the PHI arguments.
895
896    If IS_DFS is false, the two steps above are done in reverse order
897    (i.e., a breadth-first search).  */
898
899
900 void
901 walk_use_def_chains (tree var, walk_use_def_chains_fn fn, void *data,
902                      bool is_dfs)
903 {
904   tree def_stmt;
905
906   gcc_assert (TREE_CODE (var) == SSA_NAME);
907
908   def_stmt = SSA_NAME_DEF_STMT (var);
909
910   /* We only need to recurse if the reaching definition comes from a PHI
911      node.  */
912   if (TREE_CODE (def_stmt) != PHI_NODE)
913     (*fn) (var, def_stmt, data);
914   else
915     {
916       bitmap visited = BITMAP_XMALLOC ();
917       walk_use_def_chains_1 (var, fn, data, visited, is_dfs);
918       BITMAP_XFREE (visited);
919     }
920 }
921
922
923 /* Replaces VAR with REPL in memory reference expression *X in
924    statement STMT.  */
925
926 static void
927 propagate_into_addr (tree stmt, tree var, tree *x, tree repl)
928 {
929   tree new_var, ass_stmt, addr_var;
930   basic_block bb;
931   block_stmt_iterator bsi;
932
933   /* There is nothing special to handle in the other cases.  */
934   if (TREE_CODE (repl) != ADDR_EXPR)
935     return;
936   addr_var = TREE_OPERAND (repl, 0);
937
938   while (handled_component_p (*x)
939          || TREE_CODE (*x) == REALPART_EXPR
940          || TREE_CODE (*x) == IMAGPART_EXPR)
941     x = &TREE_OPERAND (*x, 0);
942
943   if (TREE_CODE (*x) != INDIRECT_REF
944       || TREE_OPERAND (*x, 0) != var)
945     return;
946
947   if (TREE_TYPE (*x) == TREE_TYPE (addr_var))
948     {
949       *x = addr_var;
950       mark_new_vars_to_rename (stmt, vars_to_rename);
951       return;
952     }
953
954
955   /* Frontends sometimes produce expressions like *&a instead of a[0].
956      Create a temporary variable to handle this case.  */
957   ass_stmt = build2 (MODIFY_EXPR, void_type_node, NULL_TREE, repl);
958   new_var = duplicate_ssa_name (var, ass_stmt);
959   TREE_OPERAND (*x, 0) = new_var;
960   TREE_OPERAND (ass_stmt, 0) = new_var;
961
962   bb = bb_for_stmt (stmt);
963   tree_block_label (bb);
964   bsi = bsi_after_labels (bb);
965   bsi_insert_after (&bsi, ass_stmt, BSI_NEW_STMT);
966
967   mark_new_vars_to_rename (stmt, vars_to_rename);
968 }
969
970 /* Replaces immediate uses of VAR by REPL.  */
971
972 static void
973 replace_immediate_uses (tree var, tree repl)
974 {
975   int i, j, n;
976   dataflow_t df;
977   tree stmt;
978   bool mark_new_vars;
979   ssa_op_iter iter;
980   use_operand_p use_p;
981
982   df = get_immediate_uses (SSA_NAME_DEF_STMT (var));
983   n = num_immediate_uses (df);
984
985   for (i = 0; i < n; i++)
986     {
987       stmt = immediate_use (df, i);
988
989       if (TREE_CODE (stmt) == PHI_NODE)
990         {
991           for (j = 0; j < PHI_NUM_ARGS (stmt); j++)
992             if (PHI_ARG_DEF (stmt, j) == var)
993               {
994                 SET_PHI_ARG_DEF (stmt, j, repl);
995                 if (TREE_CODE (repl) == SSA_NAME
996                     && PHI_ARG_EDGE (stmt, j)->flags & EDGE_ABNORMAL)
997                   SSA_NAME_OCCURS_IN_ABNORMAL_PHI (repl) = 1;
998               }
999
1000           continue;
1001         }
1002
1003       get_stmt_operands (stmt);
1004       mark_new_vars = false;
1005       if (is_gimple_reg (SSA_NAME_VAR (var)))
1006         {
1007           if (TREE_CODE (stmt) == MODIFY_EXPR)
1008             {
1009               propagate_into_addr (stmt, var, &TREE_OPERAND (stmt, 0), repl);
1010               propagate_into_addr (stmt, var, &TREE_OPERAND (stmt, 1), repl);
1011             }
1012
1013           FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1014             if (USE_FROM_PTR (use_p) == var)
1015               {
1016                 propagate_value (use_p, repl);
1017                 mark_new_vars = POINTER_TYPE_P (TREE_TYPE (repl));
1018               }
1019         }
1020       else
1021         {
1022           FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_VIRTUAL_USES)
1023             if (USE_FROM_PTR (use_p) == var)
1024               propagate_value (use_p, repl);
1025         }
1026
1027       /* FIXME.  If REPL is a constant, we need to fold STMT.
1028          However, fold_stmt wants a pointer to the statement, because
1029          it may happen that it needs to replace the whole statement
1030          with a new expression.  Since the current def-use machinery
1031          does not return pointers to statements, we call fold_stmt
1032          with the address of a local temporary, if that call changes
1033          the temporary then we fall on our swords.
1034
1035          Note that all this will become unnecessary soon.  This
1036          pass is being replaced with a proper copy propagation pass
1037          for 4.1 (dnovillo, 2004-09-17).  */
1038       if (TREE_CODE (repl) != SSA_NAME)
1039         {
1040           tree tmp = stmt;
1041           fold_stmt (&tmp);
1042           if (tmp != stmt)
1043             abort ();
1044         }
1045
1046       /* If REPL is a pointer, it may have different memory tags associated
1047          with it.  For instance, VAR may have had a name tag while REPL
1048          only had a type tag.  In these cases, the virtual operands (if
1049          any) in the statement will refer to different symbols which need
1050          to be renamed.  */
1051       if (mark_new_vars)
1052         mark_new_vars_to_rename (stmt, vars_to_rename);
1053       else
1054         modify_stmt (stmt);
1055     }
1056 }
1057
1058 /* Gets the value VAR is equivalent to according to EQ_TO.  */
1059
1060 static tree
1061 get_eq_name (tree *eq_to, tree var)
1062 {
1063   unsigned ver;
1064   tree val = var;
1065
1066   while (TREE_CODE (val) == SSA_NAME)
1067     {
1068       ver = SSA_NAME_VERSION (val);
1069       if (!eq_to[ver])
1070         break;
1071
1072       val = eq_to[ver];
1073     }
1074
1075   while (TREE_CODE (var) == SSA_NAME)
1076     {
1077       ver = SSA_NAME_VERSION (var);
1078       if (!eq_to[ver])
1079         break;
1080
1081       var = eq_to[ver];
1082       eq_to[ver] = val;
1083     }
1084
1085   return val;
1086 }
1087
1088 /* Checks whether phi node PHI is redundant and if it is, records the ssa name
1089    its result is redundant to to EQ_TO array.  */
1090
1091 static void
1092 check_phi_redundancy (tree phi, tree *eq_to)
1093 {
1094   tree val = NULL_TREE, def, res = PHI_RESULT (phi), stmt;
1095   unsigned i, ver = SSA_NAME_VERSION (res), n;
1096   dataflow_t df;
1097
1098   /* It is unlikely that such large phi node would be redundant.  */
1099   if (PHI_NUM_ARGS (phi) > 16)
1100     return;
1101
1102   for (i = 0; i < (unsigned) PHI_NUM_ARGS (phi); i++)
1103     {
1104       def = PHI_ARG_DEF (phi, i);
1105
1106       if (TREE_CODE (def) == SSA_NAME)
1107         {
1108           def = get_eq_name (eq_to, def);
1109           if (def == res)
1110             continue;
1111         }
1112
1113       if (val
1114           && !operand_equal_p (val, def, 0))
1115         return;
1116
1117       val = def;
1118     }
1119
1120   /* At least one of the arguments should not be equal to the result, or
1121      something strange is happening.  */
1122   gcc_assert (val);
1123
1124   if (get_eq_name (eq_to, res) == val)
1125     return;
1126
1127   if (!may_propagate_copy (res, val))
1128     return;
1129
1130   eq_to[ver] = val;
1131
1132   df = get_immediate_uses (SSA_NAME_DEF_STMT (res));
1133   n = num_immediate_uses (df);
1134
1135   for (i = 0; i < n; i++)
1136     {
1137       stmt = immediate_use (df, i);
1138
1139       if (TREE_CODE (stmt) == PHI_NODE)
1140         check_phi_redundancy (stmt, eq_to);
1141     }
1142 }
1143
1144 /* Removes redundant phi nodes.
1145
1146    A redundant PHI node is a PHI node where all of its PHI arguments
1147    are the same value, excluding any PHI arguments which are the same
1148    as the PHI result.
1149
1150    A redundant PHI node is effectively a copy, so we forward copy propagate
1151    which removes all uses of the destination of the PHI node then
1152    finally we delete the redundant PHI node.
1153
1154    Note that if we can not copy propagate the PHI node, then the PHI
1155    will not be removed.  Thus we do not have to worry about dependencies
1156    between PHIs and the problems serializing PHIs into copies creates. 
1157    
1158    The most important effect of this pass is to remove degenerate PHI
1159    nodes created by removing unreachable code.  */
1160
1161 void
1162 kill_redundant_phi_nodes (void)
1163 {
1164   tree *eq_to;
1165   unsigned i, old_num_ssa_names;
1166   basic_block bb;
1167   tree phi, var, repl, stmt;
1168
1169   /* The EQ_TO[VER] holds the value by that the ssa name VER should be
1170      replaced.  If EQ_TO[VER] is ssa name and it is decided to replace it by
1171      other value, it may be necessary to follow the chain till the final value.
1172      We perform path shortening (replacing the entries of the EQ_TO array with
1173      heads of these chains) whenever we access the field to prevent quadratic
1174      complexity (probably would not occur in practice anyway, but let us play
1175      it safe).  */
1176   eq_to = xcalloc (num_ssa_names, sizeof (tree));
1177
1178   /* We have had cases where computing immediate uses takes a
1179      significant amount of compile time.  If we run into such
1180      problems here, we may want to only compute immediate uses for
1181      a subset of all the SSA_NAMEs instead of computing it for
1182      all of the SSA_NAMEs.  */
1183   compute_immediate_uses (TDFA_USE_OPS | TDFA_USE_VOPS, NULL);
1184   old_num_ssa_names = num_ssa_names;
1185
1186   FOR_EACH_BB (bb)
1187     {
1188       for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
1189         {
1190           var = PHI_RESULT (phi);
1191           check_phi_redundancy (phi, eq_to);
1192         }
1193     }
1194
1195   /* Now propagate the values.  */
1196   for (i = 0; i < old_num_ssa_names; i++)
1197     {
1198       if (!ssa_name (i))
1199         continue;
1200
1201       repl = get_eq_name (eq_to, ssa_name (i));
1202       if (repl != ssa_name (i))
1203         replace_immediate_uses (ssa_name (i), repl);
1204     }
1205
1206   /* And remove the dead phis.  */
1207   for (i = 0; i < old_num_ssa_names; i++)
1208     {
1209       if (!ssa_name (i))
1210         continue;
1211
1212       repl = get_eq_name (eq_to, ssa_name (i));
1213       if (repl != ssa_name (i))
1214         {
1215           stmt = SSA_NAME_DEF_STMT (ssa_name (i));
1216           remove_phi_node (stmt, NULL_TREE, bb_for_stmt (stmt));
1217         }
1218     }
1219
1220   free_df ();
1221   free (eq_to);
1222 }
1223
1224 struct tree_opt_pass pass_redundant_phi =
1225 {
1226   "redphi",                             /* name */
1227   NULL,                                 /* gate */
1228   kill_redundant_phi_nodes,             /* execute */
1229   NULL,                                 /* sub */
1230   NULL,                                 /* next */
1231   0,                                    /* static_pass_number */
1232   0,                                    /* tv_id */
1233   PROP_cfg | PROP_ssa | PROP_alias,     /* properties_required */
1234   0,                                    /* properties_provided */
1235   0,                                    /* properties_destroyed */
1236   0,                                    /* todo_flags_start */
1237   TODO_dump_func | TODO_rename_vars 
1238     | TODO_ggc_collect | TODO_verify_ssa, /* todo_flags_finish */
1239   0                                     /* letter */
1240 };
1241 \f
1242 /* Emit warnings for uninitialized variables.  This is done in two passes.
1243
1244    The first pass notices real uses of SSA names with default definitions.
1245    Such uses are unconditionally uninitialized, and we can be certain that
1246    such a use is a mistake.  This pass is run before most optimizations,
1247    so that we catch as many as we can.
1248
1249    The second pass follows PHI nodes to find uses that are potentially
1250    uninitialized.  In this case we can't necessarily prove that the use
1251    is really uninitialized.  This pass is run after most optimizations,
1252    so that we thread as many jumps and possible, and delete as much dead
1253    code as possible, in order to reduce false positives.  We also look
1254    again for plain uninitialized variables, since optimization may have
1255    changed conditionally uninitialized to unconditionally uninitialized.  */
1256
1257 /* Emit a warning for T, an SSA_NAME, being uninitialized.  The exact
1258    warning text is in MSGID and LOCUS may contain a location or be null.  */
1259
1260 static void
1261 warn_uninit (tree t, const char *msgid, location_t *locus)
1262 {
1263   tree var = SSA_NAME_VAR (t);
1264   tree def = SSA_NAME_DEF_STMT (t);
1265
1266   /* Default uses (indicated by an empty definition statement),
1267      are uninitialized.  */
1268   if (!IS_EMPTY_STMT (def))
1269     return;
1270
1271   /* Except for PARMs of course, which are always initialized.  */
1272   if (TREE_CODE (var) == PARM_DECL)
1273     return;
1274
1275   /* Hard register variables get their initial value from the ether.  */
1276   if (DECL_HARD_REGISTER (var))
1277     return;
1278
1279   /* TREE_NO_WARNING either means we already warned, or the front end
1280      wishes to suppress the warning.  */
1281   if (TREE_NO_WARNING (var))
1282     return;
1283
1284   if (!locus)
1285     locus = &DECL_SOURCE_LOCATION (var);
1286   warning (msgid, locus, var);
1287   TREE_NO_WARNING (var) = 1;
1288 }
1289    
1290 /* Called via walk_tree, look for SSA_NAMEs that have empty definitions
1291    and warn about them.  */
1292
1293 static tree
1294 warn_uninitialized_var (tree *tp, int *walk_subtrees, void *data)
1295 {
1296   location_t *locus = data;
1297   tree t = *tp;
1298
1299   /* We only do data flow with SSA_NAMEs, so that's all we can warn about.  */
1300   if (TREE_CODE (t) == SSA_NAME)
1301     {
1302       warn_uninit (t, "%H'%D' is used uninitialized in this function", locus);
1303       *walk_subtrees = 0;
1304     }
1305   else if (IS_TYPE_OR_DECL_P (t))
1306     *walk_subtrees = 0;
1307
1308   return NULL_TREE;
1309 }
1310
1311 /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions
1312    and warn about them.  */
1313
1314 static void
1315 warn_uninitialized_phi (tree phi)
1316 {
1317   int i, n = PHI_NUM_ARGS (phi);
1318
1319   /* Don't look at memory tags.  */
1320   if (!is_gimple_reg (PHI_RESULT (phi)))
1321     return;
1322
1323   for (i = 0; i < n; ++i)
1324     {
1325       tree op = PHI_ARG_DEF (phi, i);
1326       if (TREE_CODE (op) == SSA_NAME)
1327         warn_uninit (op, "%H'%D' may be used uninitialized in this function",
1328                      NULL);
1329     }
1330 }
1331
1332 static void
1333 execute_early_warn_uninitialized (void)
1334 {
1335   block_stmt_iterator bsi;
1336   basic_block bb;
1337
1338   FOR_EACH_BB (bb)
1339     for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1340       walk_tree (bsi_stmt_ptr (bsi), warn_uninitialized_var,
1341                  EXPR_LOCUS (bsi_stmt (bsi)), NULL);
1342 }
1343
1344 static void
1345 execute_late_warn_uninitialized (void)
1346 {
1347   basic_block bb;
1348   tree phi;
1349
1350   /* Re-do the plain uninitialized variable check, as optimization may have
1351      straightened control flow.  Do this first so that we don't accidentally
1352      get a "may be" warning when we'd have seen an "is" warning later.  */
1353   execute_early_warn_uninitialized ();
1354
1355   FOR_EACH_BB (bb)
1356     for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1357       warn_uninitialized_phi (phi);
1358 }
1359
1360 static bool
1361 gate_warn_uninitialized (void)
1362 {
1363   return warn_uninitialized != 0;
1364 }
1365
1366 struct tree_opt_pass pass_early_warn_uninitialized =
1367 {
1368   NULL,                                 /* name */
1369   gate_warn_uninitialized,              /* gate */
1370   execute_early_warn_uninitialized,     /* execute */
1371   NULL,                                 /* sub */
1372   NULL,                                 /* next */
1373   0,                                    /* static_pass_number */
1374   0,                                    /* tv_id */
1375   PROP_ssa,                             /* properties_required */
1376   0,                                    /* properties_provided */
1377   0,                                    /* properties_destroyed */
1378   0,                                    /* todo_flags_start */
1379   0,                                    /* todo_flags_finish */
1380   0                                     /* letter */
1381 };
1382
1383 struct tree_opt_pass pass_late_warn_uninitialized =
1384 {
1385   NULL,                                 /* name */
1386   gate_warn_uninitialized,              /* gate */
1387   execute_late_warn_uninitialized,      /* execute */
1388   NULL,                                 /* sub */
1389   NULL,                                 /* next */
1390   0,                                    /* static_pass_number */
1391   0,                                    /* tv_id */
1392   PROP_ssa,                             /* properties_required */
1393   0,                                    /* properties_provided */
1394   0,                                    /* properties_destroyed */
1395   0,                                    /* todo_flags_start */
1396   0,                                    /* todo_flags_finish */
1397   0                                     /* letter */
1398 };