OSDN Git Service

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