OSDN Git Service

2010-04-28 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-uninit.c
1 /* Predicate aware uninitialized variable warning.
2    Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008 Free Software
3    Foundation, Inc.
4    Contributed by Xinliang David Li <davidxl@google.com>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "ggc.h"
31 #include "langhooks.h"
32 #include "hard-reg-set.h"
33 #include "basic-block.h"
34 #include "output.h"
35 #include "expr.h"
36 #include "function.h"
37 #include "diagnostic.h"
38 #include "bitmap.h"
39 #include "pointer-set.h"
40 #include "tree-flow.h"
41 #include "gimple.h"
42 #include "tree-inline.h"
43 #include "timevar.h"
44 #include "hashtab.h"
45 #include "tree-dump.h"
46 #include "tree-pass.h"
47 #include "toplev.h"
48 #include "timevar.h"
49
50 /* This implements the pass that does predicate aware warning on uses of
51    possibly uninitialized variables. The pass first collects the set of
52    possibly uninitialized SSA names. For each such name, it walks through
53    all its immediate uses. For each immediate use, it rebuilds the condition
54    expression (the predicate) that guards the use. The predicate is then
55    examined to see if the variable is always defined under that same condition.
56    This is done either by pruning the unrealizable paths that lead to the
57    default definitions or by checking if the predicate set that guards the
58    defining paths is a superset of the use predicate.  */
59
60
61 /* Pointer set of potentially undefined ssa names, i.e.,
62    ssa names that are defined by phi with operands that
63    are not defined or potentially undefined.  */
64 static struct pointer_set_t *possibly_undefined_names = 0;
65
66 /* Bit mask handling macros.  */
67 #define MASK_SET_BIT(mask, pos) mask |= (1 << pos)
68 #define MASK_TEST_BIT(mask, pos) (mask & (1 << pos))
69 #define MASK_EMPTY(mask) (mask == 0)
70
71 /* Returns the first bit position (starting from LSB)
72    in mask that is non zero. Returns -1 if the mask is empty.  */
73 static int
74 get_mask_first_set_bit (unsigned mask)
75 {
76   int pos = 0;
77   if (mask == 0)
78     return -1;
79
80   while ((mask & (1 << pos)) == 0)
81     pos++;
82
83   return pos;
84 }
85 #define MASK_FIRST_SET_BIT(mask) get_mask_first_set_bit (mask)
86
87
88 /* Return true if T, an SSA_NAME, has an undefined value.  */
89
90 bool
91 ssa_undefined_value_p (tree t)
92 {
93   tree var = SSA_NAME_VAR (t);
94
95   /* Parameters get their initial value from the function entry.  */
96   if (TREE_CODE (var) == PARM_DECL)
97     return false;
98
99   /* Hard register variables get their initial value from the ether.  */
100   if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
101     return false;
102
103   /* The value is undefined iff its definition statement is empty.  */
104   return (gimple_nop_p (SSA_NAME_DEF_STMT (t))
105           || (possibly_undefined_names
106               && pointer_set_contains (possibly_undefined_names, t)));
107 }
108
109 /* Checks if the operand OPND of PHI is defined by 
110    another phi with one operand defined by this PHI, 
111    but the rest operands are all defined. If yes, 
112    returns true to skip this this operand as being
113    redundant. Can be enhanced to be more general.  */
114
115 static bool
116 can_skip_redundant_opnd (tree opnd, gimple phi)
117 {
118   gimple op_def;
119   tree phi_def;
120   int i, n;
121
122   phi_def = gimple_phi_result (phi);
123   op_def = SSA_NAME_DEF_STMT (opnd);
124   if (gimple_code (op_def) != GIMPLE_PHI)
125     return false;
126   n = gimple_phi_num_args (op_def);
127   for (i = 0; i < n; ++i)
128     {
129       tree op = gimple_phi_arg_def (op_def, i);
130       if (TREE_CODE (op) != SSA_NAME)
131         continue;
132       if (op != phi_def && ssa_undefined_value_p (op))
133         return false;
134     }
135
136   return true;
137 }
138
139 /* Returns a bit mask holding the positions of arguments in PHI
140    that have empty (or possibly empty) definitions.  */
141
142 static unsigned
143 compute_uninit_opnds_pos (gimple phi)
144 {
145   size_t i, n;
146   unsigned uninit_opnds = 0;
147
148   n = gimple_phi_num_args (phi);
149
150   for (i = 0; i < n; ++i)
151     {
152       tree op = gimple_phi_arg_def (phi, i);
153       if (TREE_CODE (op) == SSA_NAME
154           && ssa_undefined_value_p (op)
155           && !can_skip_redundant_opnd (op, phi))
156         MASK_SET_BIT (uninit_opnds, i);
157     }
158   return uninit_opnds;
159 }
160
161 /* Find the immediate postdominator PDOM of the specified
162    basic block BLOCK.  */
163
164 static inline basic_block
165 find_pdom (basic_block block)
166 {
167    if (block == EXIT_BLOCK_PTR)
168      return EXIT_BLOCK_PTR;
169    else
170      {
171        basic_block bb
172            = get_immediate_dominator (CDI_POST_DOMINATORS, block);
173        if (! bb)
174          return EXIT_BLOCK_PTR;
175        return bb;
176      }
177 }
178
179 /* Find the immediate DOM of the specified
180    basic block BLOCK.  */
181
182 static inline basic_block
183 find_dom (basic_block block)
184 {
185    if (block == ENTRY_BLOCK_PTR)
186      return ENTRY_BLOCK_PTR;
187    else
188      {
189        basic_block bb = get_immediate_dominator (CDI_DOMINATORS, block);
190        if (! bb)
191          return ENTRY_BLOCK_PTR;
192        return bb;
193      }
194 }
195
196 /* Returns true if BB1 is postdominating BB2 and BB1 is
197    not a loop exit bb. The loop exit bb check is simple and does
198    not cover all cases.  */
199
200 static bool
201 is_non_loop_exit_postdominating (basic_block bb1, basic_block bb2)
202 {
203   if (!dominated_by_p (CDI_POST_DOMINATORS, bb2, bb1))
204     return false;
205
206   if (single_pred_p (bb1) && !single_succ_p (bb2))
207     return false;
208
209   return true;
210 }
211
212 /* Find the closest postdominator of a specified BB, which is control
213    equivalent to BB.  */
214
215 static inline  basic_block
216 find_control_equiv_block (basic_block bb)
217 {
218   basic_block pdom;
219
220   pdom = find_pdom (bb);
221
222   /* Skip the postdominating bb that is also loop exit.  */
223   if (!is_non_loop_exit_postdominating (pdom, bb))
224     return NULL;
225
226   if (dominated_by_p (CDI_DOMINATORS, pdom, bb))
227     return pdom;
228
229   return NULL;
230 }
231
232 #define MAX_NUM_CHAINS 8
233 #define MAX_CHAIN_LEN 5
234
235 /* Computes the control dependence chains (paths of edges)
236    for DEP_BB up to the dominating basic block BB (the head node of a
237    chain should be dominated by it).  CD_CHAINS is pointer to a
238    dynamic array holding the result chains. CUR_CD_CHAIN is the current
239    chain being computed.  *NUM_CHAINS is total number of chains.  The
240    function returns true if the information is successfully computed,
241    return false if there is no control dependence or not computed.  */
242
243 static bool
244 compute_control_dep_chain (basic_block bb, basic_block dep_bb,
245                            VEC(edge, heap) **cd_chains,
246                            size_t *num_chains,
247                            VEC(edge, heap) **cur_cd_chain)
248 {
249   edge_iterator ei;
250   edge e;
251   size_t i;
252   bool found_cd_chain = false;
253   size_t cur_chain_len = 0;
254
255   if (EDGE_COUNT (bb->succs) < 2)
256     return false;
257
258   /* Could  use a set instead.  */
259   cur_chain_len = VEC_length (edge, *cur_cd_chain);
260   if (cur_chain_len > MAX_CHAIN_LEN)
261     return false;
262
263   for (i = 0; i < cur_chain_len; i++)
264     {
265       edge e = VEC_index (edge, *cur_cd_chain, i);
266       /* cycle detected. */
267       if (e->src == bb)
268         return false;
269     }
270
271   FOR_EACH_EDGE (e, ei, bb->succs)
272     {
273       basic_block cd_bb;
274       if (e->flags & (EDGE_FAKE | EDGE_ABNORMAL))
275         continue;
276
277       cd_bb = e->dest;
278       VEC_safe_push (edge, heap, *cur_cd_chain, e);
279       while (!is_non_loop_exit_postdominating (cd_bb, bb))
280         {
281           if (cd_bb == dep_bb)
282             {
283               /* Found a direct control dependence.  */
284               if (*num_chains < MAX_NUM_CHAINS)
285                 {
286                   cd_chains[*num_chains]
287                       = VEC_copy (edge, heap, *cur_cd_chain);
288                   (*num_chains)++;
289                 }
290               found_cd_chain = true;
291               /* check path from next edge.  */
292               break;
293             }
294
295           /* Now check if DEP_BB is indirectly control dependent on BB.  */
296           if (compute_control_dep_chain (cd_bb, dep_bb, cd_chains,
297                                          num_chains, cur_cd_chain))
298             {
299               found_cd_chain = true;
300               break;
301             }
302
303           cd_bb = find_pdom (cd_bb);
304           if (cd_bb == EXIT_BLOCK_PTR)
305             break;
306         }
307       VEC_pop (edge, *cur_cd_chain);
308       gcc_assert (VEC_length (edge, *cur_cd_chain) == cur_chain_len);
309     }
310   gcc_assert (VEC_length (edge, *cur_cd_chain) == cur_chain_len);
311
312   return found_cd_chain;
313 }
314
315 typedef struct use_pred_info
316 {
317   gimple cond;
318   bool invert;
319 } *use_pred_info_t;
320
321 DEF_VEC_P(use_pred_info_t);
322 DEF_VEC_ALLOC_P(use_pred_info_t, heap);
323
324
325 /* Converts the chains of control dependence edges into a set of
326    predicates. A control dependence chain is represented by a vector
327    edges. DEP_CHAINS points to an array of dependence chains.
328    NUM_CHAINS is the size of the chain array. One edge in a dependence
329    chain is mapped to predicate expression represented by use_pred_info_t
330    type. One dependence chain is converted to a composite predicate that
331    is the result of AND operation of use_pred_info_t mapped to each edge.
332    A composite predicate is presented by a vector of use_pred_info_t. On
333    return, *PREDS points to the resulting array of composite predicates.
334    *NUM_PREDS is the number of composite predictes.  */
335
336 static bool
337 convert_control_dep_chain_into_preds (VEC(edge, heap) **dep_chains,
338                                       size_t num_chains,
339                                       VEC(use_pred_info_t, heap) ***preds,
340                                       size_t *num_preds)
341 {
342   bool has_valid_pred = false;
343   size_t i, j;
344   if (num_chains == 0 || num_chains >= MAX_NUM_CHAINS)
345     return false;
346
347   /* Now convert CD chains into predicates  */
348   has_valid_pred = true;
349
350   /* Now convert the control dep chain into a set
351      of predicates.  */
352   *preds = XCNEWVEC (VEC(use_pred_info_t, heap) *,
353                      num_chains);
354   *num_preds = num_chains;
355
356   for (i = 0; i < num_chains; i++)
357     {
358       VEC(edge, heap) *one_cd_chain = dep_chains[i];
359       for (j = 0; j < VEC_length (edge, one_cd_chain); j++)
360         {
361           gimple cond_stmt;
362           gimple_stmt_iterator gsi;
363           basic_block guard_bb;
364           use_pred_info_t one_pred;
365           edge e;
366
367           e = VEC_index (edge, one_cd_chain, j);
368           guard_bb = e->src;
369           gsi = gsi_last_bb (guard_bb);
370           if (gsi_end_p (gsi))
371             {
372               has_valid_pred = false;
373               break;
374             }
375           cond_stmt = gsi_stmt (gsi);
376           if (gimple_code (cond_stmt) == GIMPLE_CALL
377               && EDGE_COUNT (e->src->succs) >= 2)
378             {
379               /* Ignore EH edge. Can add assertion
380                  on the other edge's flag.  */
381               continue;
382             }
383           /* Skip if there is essentially one succesor.  */
384           if (EDGE_COUNT (e->src->succs) == 2)
385             {
386               edge e1;
387               edge_iterator ei1;
388               bool skip = false;
389
390               FOR_EACH_EDGE (e1, ei1, e->src->succs)
391                 {
392                   if (EDGE_COUNT (e1->dest->succs) == 0)
393                     {
394                       skip = true;
395                       break;
396                     }
397                 }
398               if (skip)
399                 continue;
400             }
401           if (gimple_code (cond_stmt) != GIMPLE_COND)
402             {
403               has_valid_pred = false;
404               break;
405             }
406           one_pred = XNEW (struct use_pred_info);
407           one_pred->cond = cond_stmt;
408           one_pred->invert = !!(e->flags & EDGE_FALSE_VALUE);
409           VEC_safe_push (use_pred_info_t, heap, (*preds)[i], one_pred);
410         }
411
412       if (!has_valid_pred)
413         break;
414     }
415   return has_valid_pred;
416 }
417
418 /* Computes all control dependence chains for USE_BB. The control
419    dependence chains are then converted to an array of composite
420    predicates pointed to by PREDS.  PHI_BB is the basic block of
421    the phi whose result is used in USE_BB.  */
422
423 static bool
424 find_predicates (VEC(use_pred_info_t, heap) ***preds,
425                  size_t *num_preds,
426                  basic_block phi_bb,
427                  basic_block use_bb)
428 {
429   size_t num_chains = 0, i;
430   VEC(edge, heap) **dep_chains = 0;
431   VEC(edge, heap) *cur_chain = 0;
432   bool has_valid_pred = false;
433   basic_block cd_root = 0;
434
435   dep_chains = XCNEWVEC (VEC(edge, heap) *, MAX_NUM_CHAINS);
436
437   /* First find the closest bb that is control equivalent to PHI_BB
438      that also dominates USE_BB.  */
439   cd_root = phi_bb;
440   while (dominated_by_p (CDI_DOMINATORS, use_bb, cd_root))
441     {
442       basic_block ctrl_eq_bb = find_control_equiv_block (cd_root);
443       if (ctrl_eq_bb && dominated_by_p (CDI_DOMINATORS, use_bb, ctrl_eq_bb))
444         cd_root = ctrl_eq_bb;
445       else
446         break;
447     }
448
449   compute_control_dep_chain (cd_root, use_bb,
450                              dep_chains, &num_chains,
451                              &cur_chain);
452
453   has_valid_pred
454       = convert_control_dep_chain_into_preds (dep_chains,
455                                               num_chains,
456                                               preds,
457                                               num_preds);
458   /* Free individual chain  */
459   VEC_free (edge, heap, cur_chain);
460   for (i = 0; i < num_chains; i++)
461       VEC_free (edge, heap, dep_chains[i]);
462   free (dep_chains);
463   return has_valid_pred;
464 }
465
466 /* Computes the set of incoming edges of PHI that have non empty
467    definitions of a phi chain.  The collection will be done
468    recursively on operands that are defined by phis. CD_ROOT
469    is the control dependence root. *EDGES holds the result, and
470    VISITED_PHIS is a pointer set for detecting cycles.  */
471
472 static void
473 collect_phi_def_edges (gimple phi, basic_block cd_root,
474                        VEC(edge, heap) **edges,
475                        struct pointer_set_t *visited_phis)
476 {
477   size_t i, n;
478   edge opnd_edge;
479   tree opnd;
480
481   if (pointer_set_insert (visited_phis, phi))
482     return;
483
484   n = gimple_phi_num_args (phi);
485   for (i = 0; i < n; i++)
486     {
487       opnd_edge = gimple_phi_arg_edge (phi, i);
488       opnd = gimple_phi_arg_def (phi, i);
489
490       if (TREE_CODE (opnd) != SSA_NAME
491           || !ssa_undefined_value_p (opnd))
492         VEC_safe_push (edge, heap, *edges, opnd_edge);
493       else
494         {
495           gimple def = SSA_NAME_DEF_STMT (opnd);
496           if (gimple_code (def) == GIMPLE_PHI
497               && dominated_by_p (CDI_DOMINATORS,
498                                  gimple_bb (def), cd_root))
499             collect_phi_def_edges (def, cd_root, edges,
500                                    visited_phis);
501         }
502     }
503 }
504
505 /* For each use edge of PHI, computes all control dependence chains.
506    The control dependence chains are then converted to an array of
507    composite predicates pointed to by PREDS.  */
508
509 static bool
510 find_def_preds (VEC(use_pred_info_t, heap) ***preds,
511                 size_t *num_preds, gimple phi)
512 {
513   size_t num_chains = 0, i, n;
514   VEC(edge, heap) **dep_chains = 0;
515   VEC(edge, heap) *cur_chain = 0;
516   VEC(edge, heap) *def_edges = 0;
517   bool has_valid_pred = false;
518   basic_block phi_bb, cd_root = 0;
519   struct pointer_set_t *visited_phis;
520
521   dep_chains = XCNEWVEC (VEC(edge, heap) *, MAX_NUM_CHAINS);
522
523   phi_bb = gimple_bb (phi);
524   /* First find the closest dominating bb to be
525      the control dependence root  */
526   cd_root = find_dom (phi_bb);
527   if (!cd_root)
528     return false;
529
530   visited_phis = pointer_set_create ();
531   collect_phi_def_edges (phi, cd_root, &def_edges, visited_phis);
532   pointer_set_destroy (visited_phis);
533
534   n = VEC_length (edge, def_edges);
535   if (n == 0)
536     return false;
537
538   for (i = 0; i < n; i++)
539     {
540       size_t prev_nc, j;
541       edge opnd_edge;
542
543       opnd_edge = VEC_index (edge, def_edges, i);
544       prev_nc = num_chains;
545       compute_control_dep_chain (cd_root, opnd_edge->src,
546                                  dep_chains, &num_chains,
547                                  &cur_chain);
548       /* Free individual chain  */
549       VEC_free (edge, heap, cur_chain);
550       cur_chain = 0;
551
552       /* Now update the newly added chains with
553          the phi operand edge:  */
554       if (EDGE_COUNT (opnd_edge->src->succs) > 1)
555         {
556           if (prev_nc == num_chains
557               && num_chains < MAX_NUM_CHAINS)
558             num_chains++;
559           for (j = prev_nc; j < num_chains; j++)
560             {
561               VEC_safe_push (edge, heap, dep_chains[j], opnd_edge);
562             }
563         }
564     }
565
566   has_valid_pred
567       = convert_control_dep_chain_into_preds (dep_chains,
568                                               num_chains,
569                                               preds,
570                                               num_preds);
571   for (i = 0; i < num_chains; i++)
572       VEC_free (edge, heap, dep_chains[i]);
573   free (dep_chains);
574   return has_valid_pred;
575 }
576
577 /* Dumps the predicates (PREDS) for USESTMT.  */
578
579 static void
580 dump_predicates (gimple usestmt, size_t num_preds,
581                  VEC(use_pred_info_t, heap) **preds,
582                  const char* msg)
583 {
584   size_t i, j;
585   VEC(use_pred_info_t, heap) *one_pred_chain;
586   fprintf (dump_file, msg);
587   print_gimple_stmt (dump_file, usestmt, 0, 0);
588   fprintf (dump_file, "is guarded by :\n");
589   /* do some dumping here:  */
590   for (i = 0; i < num_preds; i++)
591     {
592       size_t np;
593
594       one_pred_chain = preds[i];
595       np = VEC_length (use_pred_info_t, one_pred_chain);
596
597       for (j = 0; j < np; j++)
598         {
599           use_pred_info_t one_pred
600               = VEC_index (use_pred_info_t, one_pred_chain, j);
601           if (one_pred->invert)
602             fprintf (dump_file, " (.NOT.) ");
603           print_gimple_stmt (dump_file, one_pred->cond, 0, 0);
604           if (j < np - 1)
605             fprintf (dump_file, "(.AND.)\n");
606         }
607       if (i < num_preds - 1)
608         fprintf (dump_file, "(.OR.)\n");
609     }
610 }
611
612 /* Destroys the predicate set *PREDS.  */
613
614 static void
615 destroy_predicate_vecs (size_t n,
616                         VEC(use_pred_info_t, heap) ** preds)
617 {
618   size_t i, j;
619   for (i = 0; i < n; i++)
620     {
621       for (j = 0; j < VEC_length (use_pred_info_t, preds[i]); j++)
622         free (VEC_index (use_pred_info_t, preds[i], j));
623       VEC_free (use_pred_info_t, heap, preds[i]);
624     }
625   free (preds);
626 }
627
628
629 /* Computes the 'normalized' conditional code with operand 
630    swapping and condition inversion.  */
631
632 static enum tree_code
633 get_cmp_code (enum tree_code orig_cmp_code,
634               bool swap_cond, bool invert)
635 {
636   enum tree_code tc = orig_cmp_code;
637
638   if (swap_cond)
639     tc = swap_tree_comparison (orig_cmp_code);
640   if (invert)
641     tc = invert_tree_comparison (tc, false);
642
643   switch (tc)
644     {
645     case LT_EXPR:
646     case LE_EXPR:
647     case GT_EXPR:
648     case GE_EXPR:
649     case EQ_EXPR:
650     case NE_EXPR:
651       break;
652     default:
653       return ERROR_MARK;
654     }
655   return tc;
656 }
657
658 /* Returns true if VAL falls in the range defined by BOUNDARY and CMPC, i.e.
659    all values in the range satisfies (x CMPC BOUNDARY) == true.  */
660
661 static bool
662 is_value_included_in (tree val, tree boundary, enum tree_code cmpc)
663 {
664   bool inverted = false;
665   bool is_unsigned;
666   bool result;
667
668   /* Only handle integer constant here.  */
669   if (TREE_CODE (val) != INTEGER_CST
670       || TREE_CODE (boundary) != INTEGER_CST)
671     return true;
672
673   is_unsigned = TYPE_UNSIGNED (TREE_TYPE (val));
674
675   if (cmpc == GE_EXPR || cmpc == GT_EXPR
676       || cmpc == NE_EXPR)
677     {
678       cmpc = invert_tree_comparison (cmpc, false);
679       inverted = true;
680     }
681
682   if (is_unsigned)
683     {
684       if (cmpc == EQ_EXPR)
685         result = tree_int_cst_equal (val, boundary);
686       else if (cmpc == LT_EXPR)
687         result = INT_CST_LT_UNSIGNED (val, boundary);
688       else
689         {
690           gcc_assert (cmpc == LE_EXPR);
691           result = (tree_int_cst_equal (val, boundary)
692                     || INT_CST_LT_UNSIGNED (val, boundary));
693         }
694     }
695   else
696     {
697       if (cmpc == EQ_EXPR)
698         result = tree_int_cst_equal (val, boundary);
699       else if (cmpc == LT_EXPR)
700         result = INT_CST_LT (val, boundary);
701       else
702         {
703           gcc_assert (cmpc == LE_EXPR);
704           result = (tree_int_cst_equal (val, boundary)
705                     || INT_CST_LT (val, boundary));
706         }
707     }
708
709   if (inverted)
710     result ^= 1;
711
712   return result;
713 }
714
715 /* Returns true if PRED is common among all the predicate
716    chains (PREDS) (and therefore can be factored out).
717    NUM_PRED_CHAIN is the size of array PREDS.  */
718
719 static bool
720 find_matching_predicate_in_rest_chains (use_pred_info_t pred,
721                                         VEC(use_pred_info_t, heap) **preds,
722                                         size_t num_pred_chains)
723 {
724   size_t i, j, n;
725
726   /* trival case  */
727   if (num_pred_chains == 1)
728     return true;
729
730   for (i = 1; i < num_pred_chains; i++)
731     {
732       bool found = false;
733       VEC(use_pred_info_t, heap) *one_chain = preds[i];
734       n = VEC_length (use_pred_info_t, one_chain);
735       for (j = 0; j < n; j++)
736         {
737           use_pred_info_t pred2
738               = VEC_index (use_pred_info_t, one_chain, j);
739           /* can relax the condition comparison to not
740              use address comparison. However, the most common
741              case is that multiple control dependent paths share
742              a common path prefix, so address comparison should
743              be ok.  */
744
745           if (pred2->cond == pred->cond
746               && pred2->invert == pred->invert)
747             {
748               found = true;
749               break;
750             }
751         }
752       if (!found)
753         return false;
754     }
755   return true;
756 }
757
758 /* Forward declaration.  */
759 static bool
760 is_use_properly_guarded (gimple use_stmt,
761                          basic_block use_bb,
762                          gimple phi,
763                          unsigned uninit_opnds,
764                          struct pointer_set_t *visited_phis);
765
766 /* A helper function that determines if the predicate set
767    of the use is not overlapping with that of the uninit paths.
768    The most common senario of guarded use is in Example 1:
769      Example 1:
770            if (some_cond)
771            {
772               x = ...;
773               flag = true;
774            }
775
776             ... some code ...
777
778            if (flag)
779               use (x);
780
781      The real world examples are usually more complicated, but similar
782      and usually result from inlining:
783
784          bool init_func (int * x)
785          {
786              if (some_cond)
787                 return false;
788              *x  =  ..
789              return true;
790          }
791
792          void foo(..)
793          {
794              int x;
795
796              if (!init_func(&x))
797                 return;
798
799              .. some_code ...
800              use (x);
801          }
802
803      Another possible use scenario is in the following trivial example:
804
805      Example 2:
806           if (n > 0)
807              x = 1;
808           ...
809           if (n > 0)
810             {
811               if (m < 2)
812                  .. = x;
813             }
814
815      Predicate analysis needs to compute the composite predicate:
816
817        1) 'x' use predicate: (n > 0) .AND. (m < 2)
818        2) 'x' default value  (non-def) predicate: .NOT. (n > 0)
819        (the predicate chain for phi operand defs can be computed
820        starting from a bb that is control equivalent to the phi's
821        bb and is dominating the operand def.)
822
823        and check overlapping:
824           (n > 0) .AND. (m < 2) .AND. (.NOT. (n > 0))
825         <==> false
826
827      This implementation provides framework that can handle
828      scenarios. (Note that many simple cases are handled properly
829      without the predicate analysis -- this is due to jump threading
830      transformation which eliminates the merge point thus makes
831      path sensitive analysis unnecessary.)
832
833      NUM_PREDS is the number is the number predicate chains, PREDS is
834      the array of chains, PHI is the phi node whose incoming (undefined)
835      paths need to be pruned, and UNINIT_OPNDS is the bitmap holding
836      uninit operand positions. VISITED_PHIS is the pointer set of phi
837      stmts being checked.  */
838
839
840 static bool
841 use_pred_not_overlap_with_undef_path_pred (
842     size_t num_preds,
843     VEC(use_pred_info_t, heap) **preds,
844     gimple phi, unsigned uninit_opnds,
845     struct pointer_set_t *visited_phis)
846 {
847   unsigned int i, n;
848   gimple flag_def = 0;
849   tree  boundary_cst = 0;
850   enum tree_code cmp_code;
851   bool swap_cond = false;
852   bool invert = false;
853   VEC(use_pred_info_t, heap) *the_pred_chain;
854
855   gcc_assert (num_preds > 0);
856   /* Find within the common prefix of multiple predicate chains
857      a predicate that is a comparison of a flag variable against
858      a constant.  */
859   the_pred_chain = preds[0];
860   n = VEC_length (use_pred_info_t, the_pred_chain);
861   for (i = 0; i < n; i++)
862     {
863       gimple cond;
864       tree cond_lhs, cond_rhs, flag = 0;
865
866       use_pred_info_t the_pred
867           = VEC_index (use_pred_info_t, the_pred_chain, i);
868
869       cond = the_pred->cond;
870       invert = the_pred->invert;
871       cond_lhs = gimple_cond_lhs (cond);
872       cond_rhs = gimple_cond_rhs (cond);
873       cmp_code = gimple_cond_code (cond);
874
875       if (cond_lhs != NULL_TREE && TREE_CODE (cond_lhs) == SSA_NAME
876           && cond_rhs != NULL_TREE && is_gimple_constant (cond_rhs))
877         {
878           boundary_cst = cond_rhs;
879           flag = cond_lhs;
880         }
881       else if (cond_rhs != NULL_TREE && TREE_CODE (cond_rhs) == SSA_NAME
882                && cond_lhs != NULL_TREE && is_gimple_constant (cond_lhs))
883         {
884           boundary_cst = cond_lhs;
885           flag = cond_rhs;
886           swap_cond = true;
887         }
888
889       if (!flag)
890         continue;
891
892       flag_def = SSA_NAME_DEF_STMT (flag);
893
894       if (!flag_def)
895         continue;
896
897       if ((gimple_code (flag_def) == GIMPLE_PHI)
898           && (gimple_bb (flag_def) == gimple_bb (phi))
899           && find_matching_predicate_in_rest_chains (
900               the_pred, preds, num_preds))
901         break;
902
903       flag_def = 0;
904     }
905
906   if (!flag_def)
907     return false;
908
909   /* Now check all the uninit incoming edge has a constant flag value
910      that is in conflict with the use guard/predicate.  */
911   cmp_code = get_cmp_code (cmp_code, swap_cond, invert);
912
913   if (cmp_code == ERROR_MARK)
914     return false;
915
916   for (i = 0; i < sizeof (unsigned); i++)
917     {
918       tree flag_arg;
919
920       if (!MASK_TEST_BIT (uninit_opnds, i))
921         continue;
922
923       flag_arg = gimple_phi_arg_def (flag_def, i);
924       if (!is_gimple_constant (flag_arg))
925         return false;
926
927       /* Now check if the constant is in the guarded range.  */
928       if (is_value_included_in (flag_arg, boundary_cst, cmp_code))
929         {
930           tree opnd;
931           gimple opnd_def;
932
933           /* Now that we know that this undefined edge is not
934              pruned. If the operand is defined by another phi,
935              we can further prune the incoming edges of that
936              phi by checking the predicates of this operands.  */
937
938           opnd = gimple_phi_arg_def (phi, i);
939           opnd_def = SSA_NAME_DEF_STMT (opnd);
940           if (gimple_code (opnd_def) == GIMPLE_PHI)
941             {
942               edge opnd_edge;
943               unsigned uninit_opnds2
944                   = compute_uninit_opnds_pos (opnd_def);
945               gcc_assert (!MASK_EMPTY (uninit_opnds2));
946               opnd_edge = gimple_phi_arg_edge (phi, i);
947               if (!is_use_properly_guarded (phi,
948                                             opnd_edge->src,
949                                             opnd_def,
950                                             uninit_opnds2,
951                                             visited_phis))
952                   return false;
953             }
954           else
955             return false;
956         }
957     }
958
959   return true;
960 }
961
962 /* Returns true if TC is AND or OR */
963
964 static inline bool
965 is_and_or_or (enum tree_code tc, tree typ)
966 {
967   return (tc == TRUTH_AND_EXPR
968           || tc == TRUTH_OR_EXPR
969           || tc == BIT_IOR_EXPR
970           || (tc == BIT_AND_EXPR
971               && (typ == 0 || TREE_CODE (typ) == BOOLEAN_TYPE)));
972 }
973
974 typedef struct norm_cond
975 {
976   VEC(gimple, heap) *conds;
977   enum tree_code cond_code;
978   bool invert;
979 } *norm_cond_t;
980
981
982 /* Normalizes gimple condition COND. The normalization follows
983    UD chains to form larger condition expression trees. NORM_COND
984    holds the normalized result. COND_CODE is the logical opcode
985    (AND or OR) of the normalized tree.  */
986
987 static void
988 normalize_cond_1 (gimple cond,
989                   norm_cond_t norm_cond,
990                   enum tree_code cond_code)
991 {
992   enum gimple_code gc;
993   enum tree_code cur_cond_code;
994   tree rhs1, rhs2;
995
996   gc = gimple_code (cond);
997   if (gc != GIMPLE_ASSIGN)
998     {
999       VEC_safe_push (gimple, heap, norm_cond->conds, cond);
1000       return;
1001     }
1002
1003   cur_cond_code = gimple_assign_rhs_code (cond);
1004   rhs1 = gimple_assign_rhs1 (cond);
1005   rhs2 = gimple_assign_rhs2 (cond);
1006   if (cur_cond_code == NE_EXPR)
1007     {
1008       if (integer_zerop (rhs2)
1009           && (TREE_CODE (rhs1) == SSA_NAME))
1010         normalize_cond_1 (
1011             SSA_NAME_DEF_STMT (rhs1),
1012             norm_cond, cond_code);
1013       else if (integer_zerop (rhs1)
1014                && (TREE_CODE (rhs2) == SSA_NAME))
1015         normalize_cond_1 (
1016             SSA_NAME_DEF_STMT (rhs2),
1017             norm_cond, cond_code);
1018       else
1019         VEC_safe_push (gimple, heap, norm_cond->conds, cond);
1020
1021       return;
1022     }
1023
1024   if (is_and_or_or (cur_cond_code, TREE_TYPE (rhs1))
1025       && (cond_code == cur_cond_code || cond_code == ERROR_MARK)
1026       && (TREE_CODE (rhs1) == SSA_NAME && TREE_CODE (rhs2) == SSA_NAME))
1027     {
1028       normalize_cond_1 (SSA_NAME_DEF_STMT (rhs1),
1029                         norm_cond, cur_cond_code);
1030       normalize_cond_1 (SSA_NAME_DEF_STMT (rhs2),
1031                         norm_cond, cur_cond_code);
1032       norm_cond->cond_code = cur_cond_code;
1033     }
1034   else
1035     VEC_safe_push (gimple, heap, norm_cond->conds, cond);
1036 }
1037
1038 /* See normalize_cond_1 for details. INVERT is a flag to indicate
1039    if COND needs to be inverted or not.  */
1040
1041 static void
1042 normalize_cond (gimple cond, norm_cond_t norm_cond, bool invert)
1043 {
1044   enum tree_code cond_code;
1045
1046   norm_cond->cond_code = ERROR_MARK;
1047   norm_cond->invert = false;
1048   norm_cond->conds = NULL;
1049   gcc_assert (gimple_code (cond) == GIMPLE_COND);
1050   cond_code = gimple_cond_code (cond);
1051   if (invert)
1052     cond_code = invert_tree_comparison (cond_code, false);
1053
1054   if (cond_code == NE_EXPR)
1055     {
1056       if (integer_zerop (gimple_cond_rhs (cond))
1057           && (TREE_CODE (gimple_cond_lhs (cond)) == SSA_NAME))
1058         normalize_cond_1 (
1059             SSA_NAME_DEF_STMT (gimple_cond_lhs (cond)),
1060             norm_cond, ERROR_MARK);
1061       else if (integer_zerop (gimple_cond_lhs (cond))
1062                && (TREE_CODE (gimple_cond_rhs (cond)) == SSA_NAME))
1063         normalize_cond_1 (
1064             SSA_NAME_DEF_STMT (gimple_cond_rhs (cond)),
1065             norm_cond, ERROR_MARK);
1066       else
1067         {
1068           VEC_safe_push (gimple, heap, norm_cond->conds, cond);
1069           norm_cond->invert = invert;
1070         }
1071     }
1072   else
1073     {
1074       VEC_safe_push (gimple, heap, norm_cond->conds, cond);
1075       norm_cond->invert = invert;
1076     }
1077
1078   gcc_assert (VEC_length (gimple, norm_cond->conds) == 1
1079               || is_and_or_or (norm_cond->cond_code, NULL));
1080 }
1081
1082 /* Returns true if the domain for condition COND1 is a subset of
1083    COND2. REVERSE is a flag. when it is true the function checks
1084    if COND1 is a superset of COND2. INVERT1 and INVERT2 are flags
1085    to indicate if COND1 and COND2 need to be inverted or not.  */
1086
1087 static bool
1088 is_gcond_subset_of (gimple cond1, bool invert1,
1089                     gimple cond2, bool invert2,
1090                     bool reverse)
1091 {
1092   enum gimple_code gc1, gc2;
1093   enum tree_code cond1_code, cond2_code;
1094   gimple tmp;
1095   tree cond1_lhs, cond1_rhs, cond2_lhs, cond2_rhs;
1096
1097   /* Take the short cut.  */
1098   if (cond1 == cond2)
1099     return true;
1100
1101   if (reverse)
1102     {
1103       tmp = cond1;
1104       cond1 = cond2;
1105       cond2 = tmp;
1106     }
1107
1108   gc1 = gimple_code (cond1);
1109   gc2 = gimple_code (cond2);
1110
1111   if ((gc1 != GIMPLE_ASSIGN && gc1 != GIMPLE_COND)
1112       || (gc2 != GIMPLE_ASSIGN && gc2 != GIMPLE_COND))
1113     return cond1 == cond2;
1114
1115   cond1_code = ((gc1 == GIMPLE_ASSIGN)
1116                 ? gimple_assign_rhs_code (cond1)
1117                 : gimple_cond_code (cond1));
1118
1119   cond2_code = ((gc2 == GIMPLE_ASSIGN)
1120                 ? gimple_assign_rhs_code (cond2)
1121                 : gimple_cond_code (cond2));
1122
1123   if (TREE_CODE_CLASS (cond1_code) != tcc_comparison
1124       || TREE_CODE_CLASS (cond2_code) != tcc_comparison)
1125     return false;
1126
1127   if (invert1)
1128     cond1_code = invert_tree_comparison (cond1_code, false);
1129   if (invert2)
1130     cond2_code = invert_tree_comparison (cond2_code, false);
1131
1132   cond1_lhs = ((gc1 == GIMPLE_ASSIGN)
1133                ? gimple_assign_rhs1 (cond1)
1134                : gimple_cond_lhs (cond1));
1135   cond1_rhs = ((gc1 == GIMPLE_ASSIGN)
1136                ? gimple_assign_rhs2 (cond1)
1137                : gimple_cond_rhs (cond1));
1138   cond2_lhs = ((gc2 == GIMPLE_ASSIGN)
1139                ? gimple_assign_rhs1 (cond2)
1140                : gimple_cond_lhs (cond2));
1141   cond2_rhs = ((gc2 == GIMPLE_ASSIGN)
1142                ? gimple_assign_rhs2 (cond2)
1143                : gimple_cond_rhs (cond2));
1144
1145   /* Assuming const operands have been swapped to the
1146      rhs at this point of the analysis.  */
1147
1148   if (cond1_lhs != cond2_lhs)
1149     return false;
1150
1151   if (!is_gimple_constant (cond1_rhs)
1152       || TREE_CODE (cond1_rhs) != INTEGER_CST)
1153     return (cond1_rhs == cond2_rhs);
1154
1155   if (!is_gimple_constant (cond2_rhs)
1156       || TREE_CODE (cond2_rhs) != INTEGER_CST)
1157     return (cond1_rhs == cond2_rhs);
1158
1159   if (cond1_code == EQ_EXPR)
1160     return is_value_included_in (cond1_rhs,
1161                                  cond2_rhs, cond2_code);
1162   if (cond1_code == NE_EXPR || cond2_code == EQ_EXPR)
1163     return ((cond2_code == cond1_code)
1164             && tree_int_cst_equal (cond1_rhs, cond2_rhs));
1165
1166   if (((cond1_code == GE_EXPR || cond1_code == GT_EXPR)
1167        && (cond2_code == LE_EXPR || cond2_code == LT_EXPR))
1168       || ((cond1_code == LE_EXPR || cond1_code == LT_EXPR)
1169           && (cond2_code == GE_EXPR || cond2_code == GT_EXPR)))
1170     return false;
1171
1172   if (cond1_code != GE_EXPR && cond1_code != GT_EXPR
1173       && cond1_code != LE_EXPR && cond1_code != LT_EXPR)
1174     return false;
1175
1176   if (cond1_code == GT_EXPR)
1177     {
1178       cond1_code = GE_EXPR;
1179       cond1_rhs = fold_binary (PLUS_EXPR, TREE_TYPE (cond1_rhs),
1180                                cond1_rhs,
1181                                fold_convert (TREE_TYPE (cond1_rhs),
1182                                              integer_one_node));
1183     }
1184   else if (cond1_code == LT_EXPR)
1185     {
1186       cond1_code = LE_EXPR;
1187       cond1_rhs = fold_binary (MINUS_EXPR, TREE_TYPE (cond1_rhs),
1188                                cond1_rhs,
1189                                fold_convert (TREE_TYPE (cond1_rhs),
1190                                              integer_one_node));
1191     }
1192
1193   if (!cond1_rhs)
1194     return false;
1195
1196   gcc_assert (cond1_code == GE_EXPR || cond1_code == LE_EXPR);
1197
1198   if (cond2_code == GE_EXPR || cond2_code == GT_EXPR ||
1199       cond2_code == LE_EXPR || cond2_code == LT_EXPR)
1200     return is_value_included_in (cond1_rhs,
1201                                  cond2_rhs, cond2_code);
1202   else if (cond2_code == NE_EXPR)
1203     return
1204         (is_value_included_in (cond1_rhs,
1205                                cond2_rhs, cond2_code)
1206          && !is_value_included_in (cond2_rhs,
1207                                    cond1_rhs, cond1_code));
1208   return false;
1209 }
1210
1211 /* Returns true if the domain of the condition expression 
1212    in COND is a subset of any of the sub-conditions
1213    of the normalized condtion NORM_COND.  INVERT is a flag
1214    to indicate of the COND needs to be inverted.
1215    REVERSE is a flag. When it is true, the check is reversed --
1216    it returns true if COND is a superset of any of the subconditions
1217    of NORM_COND.  */
1218
1219 static bool
1220 is_subset_of_any (gimple cond, bool invert,
1221                   norm_cond_t norm_cond, bool reverse)
1222 {
1223   size_t i;
1224   size_t len = VEC_length (gimple, norm_cond->conds);
1225
1226   for (i = 0; i < len; i++)
1227     {
1228       if (is_gcond_subset_of (cond, invert,
1229                               VEC_index (gimple, norm_cond->conds, i),
1230                               false, reverse))
1231         return true;
1232     }
1233   return false;
1234 }
1235
1236 /* NORM_COND1 and NORM_COND2 are normalized logical/BIT OR
1237    expressions (formed by following UD chains not control
1238    dependence chains). The function returns true of domain
1239    of and expression NORM_COND1 is a subset of NORM_COND2's.
1240    The implementation is conservative, and it returns false if
1241    it the inclusion relationship may not hold.  */
1242
1243 static bool
1244 is_or_set_subset_of (norm_cond_t norm_cond1,
1245                      norm_cond_t norm_cond2)
1246 {
1247   size_t i;
1248   size_t len = VEC_length (gimple, norm_cond1->conds);
1249
1250   for (i = 0; i < len; i++)
1251     {
1252       if (!is_subset_of_any (VEC_index (gimple, norm_cond1->conds, i),
1253                              false, norm_cond2, false))
1254         return false;
1255     }
1256   return true;
1257 }
1258
1259 /* NORM_COND1 and NORM_COND2 are normalized logical AND
1260    expressions (formed by following UD chains not control
1261    dependence chains). The function returns true of domain
1262    of and expression NORM_COND1 is a subset of NORM_COND2's.  */
1263
1264 static bool
1265 is_and_set_subset_of (norm_cond_t norm_cond1,
1266                       norm_cond_t norm_cond2)
1267 {
1268   size_t i;
1269   size_t len = VEC_length (gimple, norm_cond2->conds);
1270
1271   for (i = 0; i < len; i++)
1272     {
1273       if (!is_subset_of_any (VEC_index (gimple, norm_cond2->conds, i),
1274                              false, norm_cond1, true))
1275         return false;
1276     }
1277   return true;
1278 }
1279
1280 /* Returns true of the domain if NORM_COND1 is a subset 
1281    of that of NORM_COND2. Returns false if it can not be 
1282    proved to be so.  */
1283
1284 static bool
1285 is_norm_cond_subset_of (norm_cond_t norm_cond1,
1286                         norm_cond_t norm_cond2)
1287 {
1288   size_t i;
1289   enum tree_code code1, code2;
1290
1291   code1 = norm_cond1->cond_code;
1292   code2 = norm_cond2->cond_code;
1293
1294   if (code1 == TRUTH_AND_EXPR || code1 == BIT_AND_EXPR)
1295     {
1296       /* Both conditions are AND expressions.  */
1297       if (code2 == TRUTH_AND_EXPR || code2 == BIT_AND_EXPR)
1298         return is_and_set_subset_of (norm_cond1, norm_cond2);
1299       /* NORM_COND1 is an AND expression, and NORM_COND2 is an OR
1300          expression. In this case, returns true if any subexpression
1301          of NORM_COND1 is a subset of any subexpression of NORM_COND2.  */
1302       else if (code2 == TRUTH_OR_EXPR || code2 == BIT_IOR_EXPR)
1303         {
1304           size_t len1;
1305           len1 = VEC_length (gimple, norm_cond1->conds);
1306           for (i = 0; i < len1; i++)
1307             {
1308               gimple cond1 = VEC_index (gimple, norm_cond1->conds, i);
1309               if (is_subset_of_any (cond1, false, norm_cond2, false))
1310                 return true;
1311             }
1312           return false;
1313         }
1314       else
1315         {
1316           gcc_assert (code2 == ERROR_MARK);
1317           gcc_assert (VEC_length (gimple, norm_cond2->conds) == 1);
1318           return is_subset_of_any (VEC_index (gimple, norm_cond2->conds, 0),
1319                                    norm_cond2->invert, norm_cond1, true);
1320         }
1321     }
1322   /* NORM_COND1 is an OR expression  */
1323   else if (code1 == TRUTH_OR_EXPR || code1 == BIT_IOR_EXPR)
1324     {
1325       if (code2 != code1)
1326         return false;
1327
1328       return is_or_set_subset_of (norm_cond1, norm_cond2);
1329     }
1330   else
1331     {
1332       gcc_assert (code1 == ERROR_MARK);
1333       gcc_assert (VEC_length (gimple, norm_cond1->conds) == 1);
1334       /* Conservatively returns false if NORM_COND1 is non-decomposible
1335          and NORM_COND2 is an AND expression.  */
1336       if (code2 == TRUTH_AND_EXPR || code2 == BIT_AND_EXPR)
1337         return false;
1338
1339       if (code2 == TRUTH_OR_EXPR || code2 == BIT_IOR_EXPR)
1340         return is_subset_of_any (VEC_index (gimple, norm_cond1->conds, 0),
1341                                  norm_cond1->invert, norm_cond2, false);
1342
1343       gcc_assert (code2 == ERROR_MARK);
1344       gcc_assert (VEC_length (gimple, norm_cond2->conds) == 1);
1345       return is_gcond_subset_of (VEC_index (gimple, norm_cond1->conds, 0),
1346                                  norm_cond1->invert,
1347                                  VEC_index (gimple, norm_cond2->conds, 0),
1348                                  norm_cond2->invert, false);
1349     }
1350 }
1351
1352 /* Returns true of the domain of single predicate expression
1353    EXPR1 is a subset of that of EXPR2. Returns false if it
1354    can not be proved.  */
1355
1356 static bool
1357 is_pred_expr_subset_of (use_pred_info_t expr1,
1358                         use_pred_info_t expr2)
1359 {
1360   gimple cond1, cond2;
1361   enum tree_code code1, code2;
1362   struct norm_cond norm_cond1, norm_cond2;
1363   bool is_subset = false;
1364
1365   cond1 = expr1->cond;
1366   cond2 = expr2->cond;
1367   code1 = gimple_cond_code (cond1);
1368   code2 = gimple_cond_code (cond2);
1369
1370   if (expr1->invert)
1371     code1 = invert_tree_comparison (code1, false);
1372   if (expr2->invert)
1373     code2 = invert_tree_comparison (code2, false);
1374
1375   /* Fast path -- match exactly  */
1376   if ((gimple_cond_lhs (cond1) == gimple_cond_lhs (cond2))
1377       && (gimple_cond_rhs (cond1) == gimple_cond_rhs (cond2))
1378       && (code1 == code2))
1379     return true;
1380
1381   /* Normalize conditions. To keep NE_EXPR, do not invert
1382      with both need inversion.  */
1383   normalize_cond (cond1, &norm_cond1, (expr1->invert));
1384   normalize_cond (cond2, &norm_cond2, (expr2->invert));
1385
1386   is_subset = is_norm_cond_subset_of (&norm_cond1, &norm_cond2);
1387
1388   /* Free memory  */
1389   VEC_free (gimple, heap, norm_cond1.conds);
1390   VEC_free (gimple, heap, norm_cond2.conds);
1391   return is_subset ;
1392 }
1393
1394 /* Returns true if the domain of PRED1 is a subset
1395    of that of PRED2. Returns false if it can not be proved so.  */
1396
1397 static bool
1398 is_pred_chain_subset_of (VEC(use_pred_info_t, heap) *pred1,
1399                          VEC(use_pred_info_t, heap) *pred2)
1400 {
1401   size_t np1, np2, i1, i2;
1402
1403   np1 = VEC_length (use_pred_info_t, pred1);
1404   np2 = VEC_length (use_pred_info_t, pred2);
1405
1406   for (i2 = 0; i2 < np2; i2++)
1407     {
1408       bool found = false;
1409       use_pred_info_t info2
1410           = VEC_index (use_pred_info_t, pred2, i2);
1411       for (i1 = 0; i1 < np1; i1++)
1412         {
1413           use_pred_info_t info1
1414               = VEC_index (use_pred_info_t, pred1, i1);
1415           if (is_pred_expr_subset_of (info1, info2))
1416             {
1417               found = true;
1418               break;
1419             }
1420         }
1421       if (!found)
1422         return false;
1423     }
1424   return true;
1425 }
1426
1427 /* Returns true if the domain defined by
1428    one pred chain ONE_PRED is a subset of the domain
1429    of *PREDS. It returns false if ONE_PRED's domain is
1430    not a subset of any of the sub-domains of PREDS (
1431    corresponding to each individual chains in it), even
1432    though it may be still be a subset of whole domain
1433    of PREDS which is the union (ORed) of all its subdomains.
1434    In other words, the result is conservative.  */
1435
1436 static bool
1437 is_included_in (VEC(use_pred_info_t, heap) *one_pred,
1438                 VEC(use_pred_info_t, heap) **preds,
1439                 size_t n)
1440 {
1441   size_t i;
1442
1443   for (i = 0; i < n; i++)
1444     {
1445       if (is_pred_chain_subset_of (one_pred, preds[i]))
1446         return true;
1447     }
1448
1449   return false;
1450 }
1451
1452 /* compares two predicate sets PREDS1 and PREDS2 and returns
1453    true if the domain defined by PREDS1 is a superset
1454    of PREDS2's domain. N1 and N2 are array sizes of PREDS1 and
1455    PREDS2 respectively. The implementation chooses not to build
1456    generic trees (and relying on the folding capability of the
1457    compiler), but instead performs brute force comparison of
1458    individual predicate chains (won't be a compile time problem
1459    as the chains are pretty short). When the function returns
1460    false, it does not necessarily mean *PREDS1 is not a superset
1461    of *PREDS2, but mean it may not be so since the analysis can
1462    not prove it. In such cases, false warnings may still be
1463    emitted.  */
1464
1465 static bool
1466 is_superset_of (VEC(use_pred_info_t, heap) **preds1,
1467                 size_t n1,
1468                 VEC(use_pred_info_t, heap) **preds2,
1469                 size_t n2)
1470 {
1471   size_t i;
1472   VEC(use_pred_info_t, heap) *one_pred_chain;
1473
1474   for (i = 0; i < n2; i++)
1475     {
1476       one_pred_chain = preds2[i];
1477       if (!is_included_in (one_pred_chain, preds1, n1))
1478         return false;
1479     }
1480
1481   return true;
1482 }
1483
1484 /* Computes the predicates that guard the use and checks
1485    if the incoming paths that have empty (or possibly
1486    empty) defintion can be pruned/filtered. The function returns
1487    true if it can be determined that the use of PHI's def in
1488    USE_STMT is guarded with a predicate set not overlapping with
1489    predicate sets of all runtime paths that do not have a definition.
1490    Returns false if it is not or it can not be determined. USE_BB is
1491    the bb of the use (for phi operand use, the bb is not the bb of
1492    the phi stmt, but the src bb of the operand edge). UNINIT_OPNDS
1493    is a bit vector. If an operand of PHI is uninitialized, the
1494    correponding bit in the vector is 1.  VISIED_PHIS is a pointer
1495    set of phis being visted.  */
1496
1497 static bool
1498 is_use_properly_guarded (gimple use_stmt,
1499                          basic_block use_bb,
1500                          gimple phi,
1501                          unsigned uninit_opnds,
1502                          struct pointer_set_t *visited_phis)
1503 {
1504   basic_block phi_bb;
1505   VEC(use_pred_info_t, heap) **preds = 0;
1506   VEC(use_pred_info_t, heap) **def_preds = 0;
1507   size_t num_preds = 0, num_def_preds = 0;
1508   bool has_valid_preds = false;
1509   bool is_properly_guarded = false;
1510
1511   if (pointer_set_insert (visited_phis, phi))
1512     return false;
1513
1514   phi_bb = gimple_bb (phi);
1515
1516   if (is_non_loop_exit_postdominating (use_bb, phi_bb))
1517     return false;
1518
1519   has_valid_preds = find_predicates (&preds, &num_preds,
1520                                      phi_bb, use_bb);
1521
1522   if (!has_valid_preds)
1523     {
1524       destroy_predicate_vecs (num_preds, preds);
1525       return false;
1526     }
1527
1528   if (dump_file)
1529     dump_predicates (use_stmt, num_preds, preds,
1530                      "Use in stmt ");
1531
1532   has_valid_preds = find_def_preds (&def_preds,
1533                                     &num_def_preds, phi);
1534
1535   if (has_valid_preds)
1536     {
1537       if (dump_file)
1538         dump_predicates (phi, num_def_preds, def_preds,
1539                          "Operand defs of phi ");
1540       is_properly_guarded =
1541           is_superset_of (def_preds, num_def_preds,
1542                           preds, num_preds);
1543     }
1544
1545   /* further prune the dead incoming phi edges. */
1546   if (!is_properly_guarded)
1547     is_properly_guarded
1548         = use_pred_not_overlap_with_undef_path_pred (
1549             num_preds, preds, phi, uninit_opnds, visited_phis);
1550
1551   destroy_predicate_vecs (num_preds, preds);
1552   destroy_predicate_vecs (num_def_preds, def_preds);
1553   return is_properly_guarded;
1554 }
1555
1556 /* Searches through all uses of a potentially
1557    uninitialized variable defined by PHI and returns a use
1558    statement if the use is not properly guarded. It returns
1559    NULL if all uses are guarded. UNINIT_OPNDS is a bitvector
1560    holding the position(s) of uninit PHI operands. WORKLIST
1561    is the vector of candidate phis that may be updated by this
1562    function. ADDED_TO_WORKLIST is the pointer set tracking
1563    if the new phi is already in the worklist.  */
1564
1565 static gimple
1566 find_uninit_use (gimple phi, unsigned uninit_opnds,
1567                  VEC(gimple, heap) **worklist,
1568                  struct pointer_set_t *added_to_worklist)
1569 {
1570   tree phi_result;
1571   use_operand_p use_p;
1572   gimple use_stmt;
1573   imm_use_iterator iter;
1574
1575   phi_result = gimple_phi_result (phi);
1576
1577   FOR_EACH_IMM_USE_FAST (use_p, iter, phi_result)
1578     {
1579       struct pointer_set_t *visited_phis;
1580       basic_block use_bb;
1581
1582       use_stmt = use_p->loc.stmt;
1583
1584       visited_phis = pointer_set_create ();
1585
1586       use_bb = gimple_bb (use_stmt);
1587       if (gimple_code (use_stmt) == GIMPLE_PHI)
1588         {
1589           unsigned i, n;
1590           n = gimple_phi_num_args (use_stmt);
1591
1592           /* Find the matching phi argument of the use.  */
1593           for (i = 0; i < n; ++i)
1594             {
1595                if (gimple_phi_arg_def_ptr (use_stmt, i) == use_p->use)
1596                  {
1597                     edge e = gimple_phi_arg_edge (use_stmt, i);
1598                     use_bb = e->src;
1599                     break;
1600                  }
1601             }
1602         }
1603
1604       if (is_use_properly_guarded (use_stmt,
1605                                    use_bb, 
1606                                    phi,
1607                                    uninit_opnds,
1608                                    visited_phis))
1609         {
1610           pointer_set_destroy (visited_phis);
1611           continue;
1612         }
1613       pointer_set_destroy (visited_phis);
1614
1615       /* Found one real use, return.  */
1616       if (gimple_code (use_stmt) != GIMPLE_PHI)
1617          return use_stmt;
1618
1619       /* Found a phi use that is not guarded,
1620          add the phi to the worklist.  */
1621       if (!pointer_set_insert (added_to_worklist,
1622                                use_stmt))
1623         {
1624           VEC_safe_push (gimple, heap, *worklist, use_stmt);
1625           pointer_set_insert (possibly_undefined_names,
1626                               phi_result);
1627         }
1628     }
1629
1630   return NULL;
1631 }
1632
1633 /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions
1634    and gives warning if there exists a runtime path from the entry to a
1635    use of the PHI def that does not contain a definition. In other words,
1636    the warning is on the real use. The more dead paths that can be pruned
1637    by the compiler, the fewer false positives the warning is. WORKLIST
1638    is a vector of candidate phis to be examined. ADDED_TO_WORKLIST is
1639    a pointer set tracking if the new phi is added to the worklist or not.  */
1640
1641 static void
1642 warn_uninitialized_phi (gimple phi, VEC(gimple, heap) **worklist,
1643                         struct pointer_set_t *added_to_worklist)
1644 {
1645   unsigned uninit_opnds;
1646   gimple uninit_use_stmt = 0;
1647   tree uninit_op;
1648
1649   /* Don't look at memory tags.  */
1650   if (!is_gimple_reg (gimple_phi_result (phi)))
1651     return;
1652
1653   uninit_opnds = compute_uninit_opnds_pos (phi);
1654
1655   if  (MASK_EMPTY (uninit_opnds))
1656     return;
1657
1658   /* Now check if we have any use of the value without proper guard.  */
1659   uninit_use_stmt = find_uninit_use (phi, uninit_opnds,
1660                                      worklist, added_to_worklist);
1661
1662   /* All uses are properly guarded.  */
1663   if (!uninit_use_stmt)
1664     return;
1665
1666   uninit_op = gimple_phi_arg_def (phi, MASK_FIRST_SET_BIT (uninit_opnds));
1667   warn_uninit (uninit_op,
1668                "%qD may be used uninitialized in this function",
1669                uninit_use_stmt);
1670
1671 }
1672
1673
1674 /* Entry point to the late uninitialized warning pass.  */
1675
1676 static unsigned int
1677 execute_late_warn_uninitialized (void)
1678 {
1679   basic_block bb;
1680   gimple_stmt_iterator gsi;
1681   VEC(gimple, heap) *worklist = 0;
1682   struct pointer_set_t *added_to_worklist;
1683
1684   calculate_dominance_info (CDI_DOMINATORS);
1685   calculate_dominance_info (CDI_POST_DOMINATORS);
1686   /* Re-do the plain uninitialized variable check, as optimization may have
1687      straightened control flow.  Do this first so that we don't accidentally
1688      get a "may be" warning when we'd have seen an "is" warning later.  */
1689   warn_uninitialized_vars (/*warn_possibly_uninitialized=*/1);
1690
1691   timevar_push (TV_TREE_UNINIT);
1692
1693   possibly_undefined_names = pointer_set_create ();
1694   added_to_worklist = pointer_set_create ();
1695
1696   /* Initialize worklist  */
1697   FOR_EACH_BB (bb)
1698     for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1699       {
1700         gimple phi = gsi_stmt (gsi);
1701         size_t n, i;
1702
1703         n = gimple_phi_num_args (phi);
1704
1705         /* Don't look at memory tags.  */
1706         if (!is_gimple_reg (gimple_phi_result (phi)))
1707           continue;
1708
1709         for (i = 0; i < n; ++i)
1710           {
1711             tree op = gimple_phi_arg_def (phi, i);
1712             if (TREE_CODE (op) == SSA_NAME
1713                 && ssa_undefined_value_p (op))
1714               {
1715                 VEC_safe_push (gimple, heap, worklist, phi);
1716                 pointer_set_insert (added_to_worklist, phi);
1717                 break;
1718               }
1719           }
1720       }
1721
1722   while (VEC_length (gimple, worklist) != 0)
1723     {
1724       gimple cur_phi = 0;
1725       cur_phi = VEC_pop (gimple, worklist);
1726       warn_uninitialized_phi (cur_phi, &worklist, added_to_worklist);
1727     }
1728   
1729   VEC_free (gimple, heap, worklist);
1730   pointer_set_destroy (added_to_worklist);
1731   pointer_set_destroy (possibly_undefined_names);
1732   possibly_undefined_names = NULL;
1733   free_dominance_info (CDI_POST_DOMINATORS);
1734   timevar_pop (TV_TREE_UNINIT);
1735   return 0;
1736 }
1737
1738 static bool
1739 gate_warn_uninitialized (void)
1740 {
1741   return warn_uninitialized != 0;
1742 }
1743
1744 struct gimple_opt_pass pass_late_warn_uninitialized =
1745 {
1746  {
1747   GIMPLE_PASS,
1748   "uninit",                             /* name */
1749   gate_warn_uninitialized,              /* gate */
1750   execute_late_warn_uninitialized,      /* execute */
1751   NULL,                                 /* sub */
1752   NULL,                                 /* next */
1753   0,                                    /* static_pass_number */
1754   TV_NONE,                              /* tv_id */
1755   PROP_ssa,                             /* properties_required */
1756   0,                                    /* properties_provided */
1757   0,                                    /* properties_destroyed */
1758   0,                                    /* todo_flags_start */
1759   0                                     /* todo_flags_finish */
1760  }
1761 };