remove pointer-set.[ch]
[official-gcc.git] / gcc / tree-ssa-uninit.c
blob3b38fd480d32f143befdacc935b8a86b04fda1f0
1 /* Predicate aware uninitialized variable warning.
2 Copyright (C) 2001-2014 Free Software Foundation, Inc.
3 Contributed by Xinliang David Li <davidxl@google.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "tm_p.h"
28 #include "basic-block.h"
29 #include "function.h"
30 #include "gimple-pretty-print.h"
31 #include "bitmap.h"
32 #include "hash-set.h"
33 #include "tree-ssa-alias.h"
34 #include "internal-fn.h"
35 #include "gimple-expr.h"
36 #include "is-a.h"
37 #include "gimple.h"
38 #include "gimple-iterator.h"
39 #include "gimple-ssa.h"
40 #include "tree-phinodes.h"
41 #include "ssa-iterators.h"
42 #include "tree-ssa.h"
43 #include "tree-inline.h"
44 #include "hashtab.h"
45 #include "tree-pass.h"
46 #include "diagnostic-core.h"
47 #include "params.h"
49 /* This implements the pass that does predicate aware warning on uses of
50 possibly uninitialized variables. The pass first collects the set of
51 possibly uninitialized SSA names. For each such name, it walks through
52 all its immediate uses. For each immediate use, it rebuilds the condition
53 expression (the predicate) that guards the use. The predicate is then
54 examined to see if the variable is always defined under that same condition.
55 This is done either by pruning the unrealizable paths that lead to the
56 default definitions or by checking if the predicate set that guards the
57 defining paths is a superset of the use predicate. */
60 /* Pointer set of potentially undefined ssa names, i.e.,
61 ssa names that are defined by phi with operands that
62 are not defined or potentially undefined. */
63 static hash_set<tree> *possibly_undefined_names = 0;
65 /* Bit mask handling macros. */
66 #define MASK_SET_BIT(mask, pos) mask |= (1 << pos)
67 #define MASK_TEST_BIT(mask, pos) (mask & (1 << pos))
68 #define MASK_EMPTY(mask) (mask == 0)
70 /* Returns the first bit position (starting from LSB)
71 in mask that is non zero. Returns -1 if the mask is empty. */
72 static int
73 get_mask_first_set_bit (unsigned mask)
75 int pos = 0;
76 if (mask == 0)
77 return -1;
79 while ((mask & (1 << pos)) == 0)
80 pos++;
82 return pos;
84 #define MASK_FIRST_SET_BIT(mask) get_mask_first_set_bit (mask)
86 /* Return true if T, an SSA_NAME, has an undefined value. */
87 static bool
88 has_undefined_value_p (tree t)
90 return (ssa_undefined_value_p (t)
91 || (possibly_undefined_names
92 && possibly_undefined_names->contains (t)));
97 /* Like has_undefined_value_p, but don't return true if TREE_NO_WARNING
98 is set on SSA_NAME_VAR. */
100 static inline bool
101 uninit_undefined_value_p (tree t) {
102 if (!has_undefined_value_p (t))
103 return false;
104 if (SSA_NAME_VAR (t) && TREE_NO_WARNING (SSA_NAME_VAR (t)))
105 return false;
106 return true;
109 /* Emit warnings for uninitialized variables. This is done in two passes.
111 The first pass notices real uses of SSA names with undefined values.
112 Such uses are unconditionally uninitialized, and we can be certain that
113 such a use is a mistake. This pass is run before most optimizations,
114 so that we catch as many as we can.
116 The second pass follows PHI nodes to find uses that are potentially
117 uninitialized. In this case we can't necessarily prove that the use
118 is really uninitialized. This pass is run after most optimizations,
119 so that we thread as many jumps and possible, and delete as much dead
120 code as possible, in order to reduce false positives. We also look
121 again for plain uninitialized variables, since optimization may have
122 changed conditionally uninitialized to unconditionally uninitialized. */
124 /* Emit a warning for EXPR based on variable VAR at the point in the
125 program T, an SSA_NAME, is used being uninitialized. The exact
126 warning text is in MSGID and DATA is the gimple stmt with info about
127 the location in source code. When DATA is a GIMPLE_PHI, PHIARG_IDX
128 gives which argument of the phi node to take the location from. WC
129 is the warning code. */
131 static void
132 warn_uninit (enum opt_code wc, tree t, tree expr, tree var,
133 const char *gmsgid, void *data, location_t phiarg_loc)
135 gimple context = (gimple) data;
136 location_t location, cfun_loc;
137 expanded_location xloc, floc;
139 /* Ignore COMPLEX_EXPR as initializing only a part of a complex
140 turns in a COMPLEX_EXPR with the not initialized part being
141 set to its previous (undefined) value. */
142 if (is_gimple_assign (context)
143 && gimple_assign_rhs_code (context) == COMPLEX_EXPR)
144 return;
145 if (!has_undefined_value_p (t))
146 return;
148 /* TREE_NO_WARNING either means we already warned, or the front end
149 wishes to suppress the warning. */
150 if ((context
151 && (gimple_no_warning_p (context)
152 || (gimple_assign_single_p (context)
153 && TREE_NO_WARNING (gimple_assign_rhs1 (context)))))
154 || TREE_NO_WARNING (expr))
155 return;
157 if (context != NULL && gimple_has_location (context))
158 location = gimple_location (context);
159 else if (phiarg_loc != UNKNOWN_LOCATION)
160 location = phiarg_loc;
161 else
162 location = DECL_SOURCE_LOCATION (var);
163 location = linemap_resolve_location (line_table, location,
164 LRK_SPELLING_LOCATION,
165 NULL);
166 cfun_loc = DECL_SOURCE_LOCATION (cfun->decl);
167 xloc = expand_location (location);
168 floc = expand_location (cfun_loc);
169 if (warning_at (location, wc, gmsgid, expr))
171 TREE_NO_WARNING (expr) = 1;
173 if (location == DECL_SOURCE_LOCATION (var))
174 return;
175 if (xloc.file != floc.file
176 || linemap_location_before_p (line_table,
177 location, cfun_loc)
178 || linemap_location_before_p (line_table,
179 cfun->function_end_locus,
180 location))
181 inform (DECL_SOURCE_LOCATION (var), "%qD was declared here", var);
185 static unsigned int
186 warn_uninitialized_vars (bool warn_possibly_uninitialized)
188 gimple_stmt_iterator gsi;
189 basic_block bb;
191 FOR_EACH_BB_FN (bb, cfun)
193 bool always_executed = dominated_by_p (CDI_POST_DOMINATORS,
194 single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)), bb);
195 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
197 gimple stmt = gsi_stmt (gsi);
198 use_operand_p use_p;
199 ssa_op_iter op_iter;
200 tree use;
202 if (is_gimple_debug (stmt))
203 continue;
205 /* We only do data flow with SSA_NAMEs, so that's all we
206 can warn about. */
207 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, op_iter, SSA_OP_USE)
209 use = USE_FROM_PTR (use_p);
210 if (always_executed)
211 warn_uninit (OPT_Wuninitialized, use,
212 SSA_NAME_VAR (use), SSA_NAME_VAR (use),
213 "%qD is used uninitialized in this function",
214 stmt, UNKNOWN_LOCATION);
215 else if (warn_possibly_uninitialized)
216 warn_uninit (OPT_Wmaybe_uninitialized, use,
217 SSA_NAME_VAR (use), SSA_NAME_VAR (use),
218 "%qD may be used uninitialized in this function",
219 stmt, UNKNOWN_LOCATION);
222 /* For memory the only cheap thing we can do is see if we
223 have a use of the default def of the virtual operand.
224 ??? Not so cheap would be to use the alias oracle via
225 walk_aliased_vdefs, if we don't find any aliasing vdef
226 warn as is-used-uninitialized, if we don't find an aliasing
227 vdef that kills our use (stmt_kills_ref_p), warn as
228 may-be-used-uninitialized. But this walk is quadratic and
229 so must be limited which means we would miss warning
230 opportunities. */
231 use = gimple_vuse (stmt);
232 if (use
233 && gimple_assign_single_p (stmt)
234 && !gimple_vdef (stmt)
235 && SSA_NAME_IS_DEFAULT_DEF (use))
237 tree rhs = gimple_assign_rhs1 (stmt);
238 tree base = get_base_address (rhs);
240 /* Do not warn if it can be initialized outside this function. */
241 if (TREE_CODE (base) != VAR_DECL
242 || DECL_HARD_REGISTER (base)
243 || is_global_var (base))
244 continue;
246 if (always_executed)
247 warn_uninit (OPT_Wuninitialized, use,
248 gimple_assign_rhs1 (stmt), base,
249 "%qE is used uninitialized in this function",
250 stmt, UNKNOWN_LOCATION);
251 else if (warn_possibly_uninitialized)
252 warn_uninit (OPT_Wmaybe_uninitialized, use,
253 gimple_assign_rhs1 (stmt), base,
254 "%qE may be used uninitialized in this function",
255 stmt, UNKNOWN_LOCATION);
260 return 0;
263 /* Checks if the operand OPND of PHI is defined by
264 another phi with one operand defined by this PHI,
265 but the rest operands are all defined. If yes,
266 returns true to skip this this operand as being
267 redundant. Can be enhanced to be more general. */
269 static bool
270 can_skip_redundant_opnd (tree opnd, gimple phi)
272 gimple op_def;
273 tree phi_def;
274 int i, n;
276 phi_def = gimple_phi_result (phi);
277 op_def = SSA_NAME_DEF_STMT (opnd);
278 if (gimple_code (op_def) != GIMPLE_PHI)
279 return false;
280 n = gimple_phi_num_args (op_def);
281 for (i = 0; i < n; ++i)
283 tree op = gimple_phi_arg_def (op_def, i);
284 if (TREE_CODE (op) != SSA_NAME)
285 continue;
286 if (op != phi_def && uninit_undefined_value_p (op))
287 return false;
290 return true;
293 /* Returns a bit mask holding the positions of arguments in PHI
294 that have empty (or possibly empty) definitions. */
296 static unsigned
297 compute_uninit_opnds_pos (gimple phi)
299 size_t i, n;
300 unsigned uninit_opnds = 0;
302 n = gimple_phi_num_args (phi);
303 /* Bail out for phi with too many args. */
304 if (n > 32)
305 return 0;
307 for (i = 0; i < n; ++i)
309 tree op = gimple_phi_arg_def (phi, i);
310 if (TREE_CODE (op) == SSA_NAME
311 && uninit_undefined_value_p (op)
312 && !can_skip_redundant_opnd (op, phi))
314 if (cfun->has_nonlocal_label || cfun->calls_setjmp)
316 /* Ignore SSA_NAMEs that appear on abnormal edges
317 somewhere. */
318 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
319 continue;
321 MASK_SET_BIT (uninit_opnds, i);
324 return uninit_opnds;
327 /* Find the immediate postdominator PDOM of the specified
328 basic block BLOCK. */
330 static inline basic_block
331 find_pdom (basic_block block)
333 if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
334 return EXIT_BLOCK_PTR_FOR_FN (cfun);
335 else
337 basic_block bb
338 = get_immediate_dominator (CDI_POST_DOMINATORS, block);
339 if (! bb)
340 return EXIT_BLOCK_PTR_FOR_FN (cfun);
341 return bb;
345 /* Find the immediate DOM of the specified
346 basic block BLOCK. */
348 static inline basic_block
349 find_dom (basic_block block)
351 if (block == ENTRY_BLOCK_PTR_FOR_FN (cfun))
352 return ENTRY_BLOCK_PTR_FOR_FN (cfun);
353 else
355 basic_block bb = get_immediate_dominator (CDI_DOMINATORS, block);
356 if (! bb)
357 return ENTRY_BLOCK_PTR_FOR_FN (cfun);
358 return bb;
362 /* Returns true if BB1 is postdominating BB2 and BB1 is
363 not a loop exit bb. The loop exit bb check is simple and does
364 not cover all cases. */
366 static bool
367 is_non_loop_exit_postdominating (basic_block bb1, basic_block bb2)
369 if (!dominated_by_p (CDI_POST_DOMINATORS, bb2, bb1))
370 return false;
372 if (single_pred_p (bb1) && !single_succ_p (bb2))
373 return false;
375 return true;
378 /* Find the closest postdominator of a specified BB, which is control
379 equivalent to BB. */
381 static inline basic_block
382 find_control_equiv_block (basic_block bb)
384 basic_block pdom;
386 pdom = find_pdom (bb);
388 /* Skip the postdominating bb that is also loop exit. */
389 if (!is_non_loop_exit_postdominating (pdom, bb))
390 return NULL;
392 if (dominated_by_p (CDI_DOMINATORS, pdom, bb))
393 return pdom;
395 return NULL;
398 #define MAX_NUM_CHAINS 8
399 #define MAX_CHAIN_LEN 5
400 #define MAX_POSTDOM_CHECK 8
402 /* Computes the control dependence chains (paths of edges)
403 for DEP_BB up to the dominating basic block BB (the head node of a
404 chain should be dominated by it). CD_CHAINS is pointer to an
405 array holding the result chains. CUR_CD_CHAIN is the current
406 chain being computed. *NUM_CHAINS is total number of chains. The
407 function returns true if the information is successfully computed,
408 return false if there is no control dependence or not computed. */
410 static bool
411 compute_control_dep_chain (basic_block bb, basic_block dep_bb,
412 vec<edge> *cd_chains,
413 size_t *num_chains,
414 vec<edge> *cur_cd_chain,
415 int *num_calls)
417 edge_iterator ei;
418 edge e;
419 size_t i;
420 bool found_cd_chain = false;
421 size_t cur_chain_len = 0;
423 if (EDGE_COUNT (bb->succs) < 2)
424 return false;
426 if (*num_calls > PARAM_VALUE (PARAM_UNINIT_CONTROL_DEP_ATTEMPTS))
427 return false;
428 ++*num_calls;
430 /* Could use a set instead. */
431 cur_chain_len = cur_cd_chain->length ();
432 if (cur_chain_len > MAX_CHAIN_LEN)
433 return false;
435 for (i = 0; i < cur_chain_len; i++)
437 edge e = (*cur_cd_chain)[i];
438 /* Cycle detected. */
439 if (e->src == bb)
440 return false;
443 FOR_EACH_EDGE (e, ei, bb->succs)
445 basic_block cd_bb;
446 int post_dom_check = 0;
447 if (e->flags & (EDGE_FAKE | EDGE_ABNORMAL))
448 continue;
450 cd_bb = e->dest;
451 cur_cd_chain->safe_push (e);
452 while (!is_non_loop_exit_postdominating (cd_bb, bb))
454 if (cd_bb == dep_bb)
456 /* Found a direct control dependence. */
457 if (*num_chains < MAX_NUM_CHAINS)
459 cd_chains[*num_chains] = cur_cd_chain->copy ();
460 (*num_chains)++;
462 found_cd_chain = true;
463 /* Check path from next edge. */
464 break;
467 /* Now check if DEP_BB is indirectly control dependent on BB. */
468 if (compute_control_dep_chain (cd_bb, dep_bb, cd_chains,
469 num_chains, cur_cd_chain, num_calls))
471 found_cd_chain = true;
472 break;
475 cd_bb = find_pdom (cd_bb);
476 post_dom_check++;
477 if (cd_bb == EXIT_BLOCK_PTR_FOR_FN (cfun) || post_dom_check >
478 MAX_POSTDOM_CHECK)
479 break;
481 cur_cd_chain->pop ();
482 gcc_assert (cur_cd_chain->length () == cur_chain_len);
484 gcc_assert (cur_cd_chain->length () == cur_chain_len);
486 return found_cd_chain;
489 /* The type to represent a simple predicate */
491 typedef struct use_def_pred_info
493 tree pred_lhs;
494 tree pred_rhs;
495 enum tree_code cond_code;
496 bool invert;
497 } pred_info;
499 /* The type to represent a sequence of predicates grouped
500 with .AND. operation. */
502 typedef vec<pred_info, va_heap, vl_ptr> pred_chain;
504 /* The type to represent a sequence of pred_chains grouped
505 with .OR. operation. */
507 typedef vec<pred_chain, va_heap, vl_ptr> pred_chain_union;
509 /* Converts the chains of control dependence edges into a set of
510 predicates. A control dependence chain is represented by a vector
511 edges. DEP_CHAINS points to an array of dependence chains.
512 NUM_CHAINS is the size of the chain array. One edge in a dependence
513 chain is mapped to predicate expression represented by pred_info
514 type. One dependence chain is converted to a composite predicate that
515 is the result of AND operation of pred_info mapped to each edge.
516 A composite predicate is presented by a vector of pred_info. On
517 return, *PREDS points to the resulting array of composite predicates.
518 *NUM_PREDS is the number of composite predictes. */
520 static bool
521 convert_control_dep_chain_into_preds (vec<edge> *dep_chains,
522 size_t num_chains,
523 pred_chain_union *preds)
525 bool has_valid_pred = false;
526 size_t i, j;
527 if (num_chains == 0 || num_chains >= MAX_NUM_CHAINS)
528 return false;
530 /* Now convert the control dep chain into a set
531 of predicates. */
532 preds->reserve (num_chains);
534 for (i = 0; i < num_chains; i++)
536 vec<edge> one_cd_chain = dep_chains[i];
538 has_valid_pred = false;
539 pred_chain t_chain = vNULL;
540 for (j = 0; j < one_cd_chain.length (); j++)
542 gimple cond_stmt;
543 gimple_stmt_iterator gsi;
544 basic_block guard_bb;
545 pred_info one_pred;
546 edge e;
548 e = one_cd_chain[j];
549 guard_bb = e->src;
550 gsi = gsi_last_bb (guard_bb);
551 if (gsi_end_p (gsi))
553 has_valid_pred = false;
554 break;
556 cond_stmt = gsi_stmt (gsi);
557 if (is_gimple_call (cond_stmt)
558 && EDGE_COUNT (e->src->succs) >= 2)
560 /* Ignore EH edge. Can add assertion
561 on the other edge's flag. */
562 continue;
564 /* Skip if there is essentially one succesor. */
565 if (EDGE_COUNT (e->src->succs) == 2)
567 edge e1;
568 edge_iterator ei1;
569 bool skip = false;
571 FOR_EACH_EDGE (e1, ei1, e->src->succs)
573 if (EDGE_COUNT (e1->dest->succs) == 0)
575 skip = true;
576 break;
579 if (skip)
580 continue;
582 if (gimple_code (cond_stmt) != GIMPLE_COND)
584 has_valid_pred = false;
585 break;
587 one_pred.pred_lhs = gimple_cond_lhs (cond_stmt);
588 one_pred.pred_rhs = gimple_cond_rhs (cond_stmt);
589 one_pred.cond_code = gimple_cond_code (cond_stmt);
590 one_pred.invert = !!(e->flags & EDGE_FALSE_VALUE);
591 t_chain.safe_push (one_pred);
592 has_valid_pred = true;
595 if (!has_valid_pred)
596 break;
597 else
598 preds->safe_push (t_chain);
600 return has_valid_pred;
603 /* Computes all control dependence chains for USE_BB. The control
604 dependence chains are then converted to an array of composite
605 predicates pointed to by PREDS. PHI_BB is the basic block of
606 the phi whose result is used in USE_BB. */
608 static bool
609 find_predicates (pred_chain_union *preds,
610 basic_block phi_bb,
611 basic_block use_bb)
613 size_t num_chains = 0, i;
614 int num_calls = 0;
615 vec<edge> dep_chains[MAX_NUM_CHAINS];
616 auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
617 bool has_valid_pred = false;
618 basic_block cd_root = 0;
620 /* First find the closest bb that is control equivalent to PHI_BB
621 that also dominates USE_BB. */
622 cd_root = phi_bb;
623 while (dominated_by_p (CDI_DOMINATORS, use_bb, cd_root))
625 basic_block ctrl_eq_bb = find_control_equiv_block (cd_root);
626 if (ctrl_eq_bb && dominated_by_p (CDI_DOMINATORS, use_bb, ctrl_eq_bb))
627 cd_root = ctrl_eq_bb;
628 else
629 break;
632 compute_control_dep_chain (cd_root, use_bb, dep_chains, &num_chains,
633 &cur_chain, &num_calls);
635 has_valid_pred
636 = convert_control_dep_chain_into_preds (dep_chains, num_chains, preds);
637 for (i = 0; i < num_chains; i++)
638 dep_chains[i].release ();
639 return has_valid_pred;
642 /* Computes the set of incoming edges of PHI that have non empty
643 definitions of a phi chain. The collection will be done
644 recursively on operands that are defined by phis. CD_ROOT
645 is the control dependence root. *EDGES holds the result, and
646 VISITED_PHIS is a pointer set for detecting cycles. */
648 static void
649 collect_phi_def_edges (gimple phi, basic_block cd_root,
650 vec<edge> *edges,
651 hash_set<gimple> *visited_phis)
653 size_t i, n;
654 edge opnd_edge;
655 tree opnd;
657 if (visited_phis->add (phi))
658 return;
660 n = gimple_phi_num_args (phi);
661 for (i = 0; i < n; i++)
663 opnd_edge = gimple_phi_arg_edge (phi, i);
664 opnd = gimple_phi_arg_def (phi, i);
666 if (TREE_CODE (opnd) != SSA_NAME)
668 if (dump_file && (dump_flags & TDF_DETAILS))
670 fprintf (dump_file, "\n[CHECK] Found def edge %d in ", (int)i);
671 print_gimple_stmt (dump_file, phi, 0, 0);
673 edges->safe_push (opnd_edge);
675 else
677 gimple def = SSA_NAME_DEF_STMT (opnd);
679 if (gimple_code (def) == GIMPLE_PHI
680 && dominated_by_p (CDI_DOMINATORS,
681 gimple_bb (def), cd_root))
682 collect_phi_def_edges (def, cd_root, edges,
683 visited_phis);
684 else if (!uninit_undefined_value_p (opnd))
686 if (dump_file && (dump_flags & TDF_DETAILS))
688 fprintf (dump_file, "\n[CHECK] Found def edge %d in ", (int)i);
689 print_gimple_stmt (dump_file, phi, 0, 0);
691 edges->safe_push (opnd_edge);
697 /* For each use edge of PHI, computes all control dependence chains.
698 The control dependence chains are then converted to an array of
699 composite predicates pointed to by PREDS. */
701 static bool
702 find_def_preds (pred_chain_union *preds, gimple phi)
704 size_t num_chains = 0, i, n;
705 vec<edge> dep_chains[MAX_NUM_CHAINS];
706 auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
707 vec<edge> def_edges = vNULL;
708 bool has_valid_pred = false;
709 basic_block phi_bb, cd_root = 0;
711 phi_bb = gimple_bb (phi);
712 /* First find the closest dominating bb to be
713 the control dependence root */
714 cd_root = find_dom (phi_bb);
715 if (!cd_root)
716 return false;
718 hash_set<gimple> visited_phis;
719 collect_phi_def_edges (phi, cd_root, &def_edges, &visited_phis);
721 n = def_edges.length ();
722 if (n == 0)
723 return false;
725 for (i = 0; i < n; i++)
727 size_t prev_nc, j;
728 int num_calls = 0;
729 edge opnd_edge;
731 opnd_edge = def_edges[i];
732 prev_nc = num_chains;
733 compute_control_dep_chain (cd_root, opnd_edge->src, dep_chains,
734 &num_chains, &cur_chain, &num_calls);
736 /* Now update the newly added chains with
737 the phi operand edge: */
738 if (EDGE_COUNT (opnd_edge->src->succs) > 1)
740 if (prev_nc == num_chains && num_chains < MAX_NUM_CHAINS)
741 dep_chains[num_chains++] = vNULL;
742 for (j = prev_nc; j < num_chains; j++)
743 dep_chains[j].safe_push (opnd_edge);
747 has_valid_pred
748 = convert_control_dep_chain_into_preds (dep_chains, num_chains, preds);
749 for (i = 0; i < num_chains; i++)
750 dep_chains[i].release ();
751 return has_valid_pred;
754 /* Dumps the predicates (PREDS) for USESTMT. */
756 static void
757 dump_predicates (gimple usestmt, pred_chain_union preds,
758 const char* msg)
760 size_t i, j;
761 pred_chain one_pred_chain = vNULL;
762 fprintf (dump_file, msg);
763 print_gimple_stmt (dump_file, usestmt, 0, 0);
764 fprintf (dump_file, "is guarded by :\n\n");
765 size_t num_preds = preds.length ();
766 /* Do some dumping here: */
767 for (i = 0; i < num_preds; i++)
769 size_t np;
771 one_pred_chain = preds[i];
772 np = one_pred_chain.length ();
774 for (j = 0; j < np; j++)
776 pred_info one_pred = one_pred_chain[j];
777 if (one_pred.invert)
778 fprintf (dump_file, " (.NOT.) ");
779 print_generic_expr (dump_file, one_pred.pred_lhs, 0);
780 fprintf (dump_file, " %s ", op_symbol_code (one_pred.cond_code));
781 print_generic_expr (dump_file, one_pred.pred_rhs, 0);
782 if (j < np - 1)
783 fprintf (dump_file, " (.AND.) ");
784 else
785 fprintf (dump_file, "\n");
787 if (i < num_preds - 1)
788 fprintf (dump_file, "(.OR.)\n");
789 else
790 fprintf (dump_file, "\n\n");
794 /* Destroys the predicate set *PREDS. */
796 static void
797 destroy_predicate_vecs (pred_chain_union preds)
799 size_t i;
801 size_t n = preds.length ();
802 for (i = 0; i < n; i++)
803 preds[i].release ();
804 preds.release ();
808 /* Computes the 'normalized' conditional code with operand
809 swapping and condition inversion. */
811 static enum tree_code
812 get_cmp_code (enum tree_code orig_cmp_code,
813 bool swap_cond, bool invert)
815 enum tree_code tc = orig_cmp_code;
817 if (swap_cond)
818 tc = swap_tree_comparison (orig_cmp_code);
819 if (invert)
820 tc = invert_tree_comparison (tc, false);
822 switch (tc)
824 case LT_EXPR:
825 case LE_EXPR:
826 case GT_EXPR:
827 case GE_EXPR:
828 case EQ_EXPR:
829 case NE_EXPR:
830 break;
831 default:
832 return ERROR_MARK;
834 return tc;
837 /* Returns true if VAL falls in the range defined by BOUNDARY and CMPC, i.e.
838 all values in the range satisfies (x CMPC BOUNDARY) == true. */
840 static bool
841 is_value_included_in (tree val, tree boundary, enum tree_code cmpc)
843 bool inverted = false;
844 bool is_unsigned;
845 bool result;
847 /* Only handle integer constant here. */
848 if (TREE_CODE (val) != INTEGER_CST
849 || TREE_CODE (boundary) != INTEGER_CST)
850 return true;
852 is_unsigned = TYPE_UNSIGNED (TREE_TYPE (val));
854 if (cmpc == GE_EXPR || cmpc == GT_EXPR
855 || cmpc == NE_EXPR)
857 cmpc = invert_tree_comparison (cmpc, false);
858 inverted = true;
861 if (is_unsigned)
863 if (cmpc == EQ_EXPR)
864 result = tree_int_cst_equal (val, boundary);
865 else if (cmpc == LT_EXPR)
866 result = tree_int_cst_lt (val, boundary);
867 else
869 gcc_assert (cmpc == LE_EXPR);
870 result = tree_int_cst_le (val, boundary);
873 else
875 if (cmpc == EQ_EXPR)
876 result = tree_int_cst_equal (val, boundary);
877 else if (cmpc == LT_EXPR)
878 result = tree_int_cst_lt (val, boundary);
879 else
881 gcc_assert (cmpc == LE_EXPR);
882 result = (tree_int_cst_equal (val, boundary)
883 || tree_int_cst_lt (val, boundary));
887 if (inverted)
888 result ^= 1;
890 return result;
893 /* Returns true if PRED is common among all the predicate
894 chains (PREDS) (and therefore can be factored out).
895 NUM_PRED_CHAIN is the size of array PREDS. */
897 static bool
898 find_matching_predicate_in_rest_chains (pred_info pred,
899 pred_chain_union preds,
900 size_t num_pred_chains)
902 size_t i, j, n;
904 /* Trival case. */
905 if (num_pred_chains == 1)
906 return true;
908 for (i = 1; i < num_pred_chains; i++)
910 bool found = false;
911 pred_chain one_chain = preds[i];
912 n = one_chain.length ();
913 for (j = 0; j < n; j++)
915 pred_info pred2 = one_chain[j];
916 /* Can relax the condition comparison to not
917 use address comparison. However, the most common
918 case is that multiple control dependent paths share
919 a common path prefix, so address comparison should
920 be ok. */
922 if (operand_equal_p (pred2.pred_lhs, pred.pred_lhs, 0)
923 && operand_equal_p (pred2.pred_rhs, pred.pred_rhs, 0)
924 && pred2.invert == pred.invert)
926 found = true;
927 break;
930 if (!found)
931 return false;
933 return true;
936 /* Forward declaration. */
937 static bool
938 is_use_properly_guarded (gimple use_stmt,
939 basic_block use_bb,
940 gimple phi,
941 unsigned uninit_opnds,
942 hash_set<gimple> *visited_phis);
944 /* Returns true if all uninitialized opnds are pruned. Returns false
945 otherwise. PHI is the phi node with uninitialized operands,
946 UNINIT_OPNDS is the bitmap of the uninitialize operand positions,
947 FLAG_DEF is the statement defining the flag guarding the use of the
948 PHI output, BOUNDARY_CST is the const value used in the predicate
949 associated with the flag, CMP_CODE is the comparison code used in
950 the predicate, VISITED_PHIS is the pointer set of phis visited, and
951 VISITED_FLAG_PHIS is the pointer to the pointer set of flag definitions
952 that are also phis.
954 Example scenario:
956 BB1:
957 flag_1 = phi <0, 1> // (1)
958 var_1 = phi <undef, some_val>
961 BB2:
962 flag_2 = phi <0, flag_1, flag_1> // (2)
963 var_2 = phi <undef, var_1, var_1>
964 if (flag_2 == 1)
965 goto BB3;
967 BB3:
968 use of var_2 // (3)
970 Because some flag arg in (1) is not constant, if we do not look into the
971 flag phis recursively, it is conservatively treated as unknown and var_1
972 is thought to be flowed into use at (3). Since var_1 is potentially uninitialized
973 a false warning will be emitted. Checking recursively into (1), the compiler can
974 find out that only some_val (which is defined) can flow into (3) which is OK.
978 static bool
979 prune_uninit_phi_opnds_in_unrealizable_paths (gimple phi,
980 unsigned uninit_opnds,
981 gimple flag_def,
982 tree boundary_cst,
983 enum tree_code cmp_code,
984 hash_set<gimple> *visited_phis,
985 bitmap *visited_flag_phis)
987 unsigned i;
989 for (i = 0; i < MIN (32, gimple_phi_num_args (flag_def)); i++)
991 tree flag_arg;
993 if (!MASK_TEST_BIT (uninit_opnds, i))
994 continue;
996 flag_arg = gimple_phi_arg_def (flag_def, i);
997 if (!is_gimple_constant (flag_arg))
999 gimple flag_arg_def, phi_arg_def;
1000 tree phi_arg;
1001 unsigned uninit_opnds_arg_phi;
1003 if (TREE_CODE (flag_arg) != SSA_NAME)
1004 return false;
1005 flag_arg_def = SSA_NAME_DEF_STMT (flag_arg);
1006 if (gimple_code (flag_arg_def) != GIMPLE_PHI)
1007 return false;
1009 phi_arg = gimple_phi_arg_def (phi, i);
1010 if (TREE_CODE (phi_arg) != SSA_NAME)
1011 return false;
1013 phi_arg_def = SSA_NAME_DEF_STMT (phi_arg);
1014 if (gimple_code (phi_arg_def) != GIMPLE_PHI)
1015 return false;
1017 if (gimple_bb (phi_arg_def) != gimple_bb (flag_arg_def))
1018 return false;
1020 if (!*visited_flag_phis)
1021 *visited_flag_phis = BITMAP_ALLOC (NULL);
1023 if (bitmap_bit_p (*visited_flag_phis,
1024 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def))))
1025 return false;
1027 bitmap_set_bit (*visited_flag_phis,
1028 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def)));
1030 /* Now recursively prune the uninitialized phi args. */
1031 uninit_opnds_arg_phi = compute_uninit_opnds_pos (phi_arg_def);
1032 if (!prune_uninit_phi_opnds_in_unrealizable_paths
1033 (phi_arg_def, uninit_opnds_arg_phi, flag_arg_def,
1034 boundary_cst, cmp_code, visited_phis, visited_flag_phis))
1035 return false;
1037 bitmap_clear_bit (*visited_flag_phis,
1038 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def)));
1039 continue;
1042 /* Now check if the constant is in the guarded range. */
1043 if (is_value_included_in (flag_arg, boundary_cst, cmp_code))
1045 tree opnd;
1046 gimple opnd_def;
1048 /* Now that we know that this undefined edge is not
1049 pruned. If the operand is defined by another phi,
1050 we can further prune the incoming edges of that
1051 phi by checking the predicates of this operands. */
1053 opnd = gimple_phi_arg_def (phi, i);
1054 opnd_def = SSA_NAME_DEF_STMT (opnd);
1055 if (gimple_code (opnd_def) == GIMPLE_PHI)
1057 edge opnd_edge;
1058 unsigned uninit_opnds2
1059 = compute_uninit_opnds_pos (opnd_def);
1060 gcc_assert (!MASK_EMPTY (uninit_opnds2));
1061 opnd_edge = gimple_phi_arg_edge (phi, i);
1062 if (!is_use_properly_guarded (phi,
1063 opnd_edge->src,
1064 opnd_def,
1065 uninit_opnds2,
1066 visited_phis))
1067 return false;
1069 else
1070 return false;
1074 return true;
1077 /* A helper function that determines if the predicate set
1078 of the use is not overlapping with that of the uninit paths.
1079 The most common senario of guarded use is in Example 1:
1080 Example 1:
1081 if (some_cond)
1083 x = ...;
1084 flag = true;
1087 ... some code ...
1089 if (flag)
1090 use (x);
1092 The real world examples are usually more complicated, but similar
1093 and usually result from inlining:
1095 bool init_func (int * x)
1097 if (some_cond)
1098 return false;
1099 *x = ..
1100 return true;
1103 void foo(..)
1105 int x;
1107 if (!init_func(&x))
1108 return;
1110 .. some_code ...
1111 use (x);
1114 Another possible use scenario is in the following trivial example:
1116 Example 2:
1117 if (n > 0)
1118 x = 1;
1120 if (n > 0)
1122 if (m < 2)
1123 .. = x;
1126 Predicate analysis needs to compute the composite predicate:
1128 1) 'x' use predicate: (n > 0) .AND. (m < 2)
1129 2) 'x' default value (non-def) predicate: .NOT. (n > 0)
1130 (the predicate chain for phi operand defs can be computed
1131 starting from a bb that is control equivalent to the phi's
1132 bb and is dominating the operand def.)
1134 and check overlapping:
1135 (n > 0) .AND. (m < 2) .AND. (.NOT. (n > 0))
1136 <==> false
1138 This implementation provides framework that can handle
1139 scenarios. (Note that many simple cases are handled properly
1140 without the predicate analysis -- this is due to jump threading
1141 transformation which eliminates the merge point thus makes
1142 path sensitive analysis unnecessary.)
1144 NUM_PREDS is the number is the number predicate chains, PREDS is
1145 the array of chains, PHI is the phi node whose incoming (undefined)
1146 paths need to be pruned, and UNINIT_OPNDS is the bitmap holding
1147 uninit operand positions. VISITED_PHIS is the pointer set of phi
1148 stmts being checked. */
1151 static bool
1152 use_pred_not_overlap_with_undef_path_pred (pred_chain_union preds,
1153 gimple phi, unsigned uninit_opnds,
1154 hash_set<gimple> *visited_phis)
1156 unsigned int i, n;
1157 gimple flag_def = 0;
1158 tree boundary_cst = 0;
1159 enum tree_code cmp_code;
1160 bool swap_cond = false;
1161 bool invert = false;
1162 pred_chain the_pred_chain = vNULL;
1163 bitmap visited_flag_phis = NULL;
1164 bool all_pruned = false;
1165 size_t num_preds = preds.length ();
1167 gcc_assert (num_preds > 0);
1168 /* Find within the common prefix of multiple predicate chains
1169 a predicate that is a comparison of a flag variable against
1170 a constant. */
1171 the_pred_chain = preds[0];
1172 n = the_pred_chain.length ();
1173 for (i = 0; i < n; i++)
1175 tree cond_lhs, cond_rhs, flag = 0;
1177 pred_info the_pred = the_pred_chain[i];
1179 invert = the_pred.invert;
1180 cond_lhs = the_pred.pred_lhs;
1181 cond_rhs = the_pred.pred_rhs;
1182 cmp_code = the_pred.cond_code;
1184 if (cond_lhs != NULL_TREE && TREE_CODE (cond_lhs) == SSA_NAME
1185 && cond_rhs != NULL_TREE && is_gimple_constant (cond_rhs))
1187 boundary_cst = cond_rhs;
1188 flag = cond_lhs;
1190 else if (cond_rhs != NULL_TREE && TREE_CODE (cond_rhs) == SSA_NAME
1191 && cond_lhs != NULL_TREE && is_gimple_constant (cond_lhs))
1193 boundary_cst = cond_lhs;
1194 flag = cond_rhs;
1195 swap_cond = true;
1198 if (!flag)
1199 continue;
1201 flag_def = SSA_NAME_DEF_STMT (flag);
1203 if (!flag_def)
1204 continue;
1206 if ((gimple_code (flag_def) == GIMPLE_PHI)
1207 && (gimple_bb (flag_def) == gimple_bb (phi))
1208 && find_matching_predicate_in_rest_chains (the_pred, preds,
1209 num_preds))
1210 break;
1212 flag_def = 0;
1215 if (!flag_def)
1216 return false;
1218 /* Now check all the uninit incoming edge has a constant flag value
1219 that is in conflict with the use guard/predicate. */
1220 cmp_code = get_cmp_code (cmp_code, swap_cond, invert);
1222 if (cmp_code == ERROR_MARK)
1223 return false;
1225 all_pruned = prune_uninit_phi_opnds_in_unrealizable_paths (phi,
1226 uninit_opnds,
1227 flag_def,
1228 boundary_cst,
1229 cmp_code,
1230 visited_phis,
1231 &visited_flag_phis);
1233 if (visited_flag_phis)
1234 BITMAP_FREE (visited_flag_phis);
1236 return all_pruned;
1239 /* The helper function returns true if two predicates X1 and X2
1240 are equivalent. It assumes the expressions have already
1241 properly re-associated. */
1243 static inline bool
1244 pred_equal_p (pred_info x1, pred_info x2)
1246 enum tree_code c1, c2;
1247 if (!operand_equal_p (x1.pred_lhs, x2.pred_lhs, 0)
1248 || !operand_equal_p (x1.pred_rhs, x2.pred_rhs, 0))
1249 return false;
1251 c1 = x1.cond_code;
1252 if (x1.invert != x2.invert)
1253 c2 = invert_tree_comparison (x2.cond_code, false);
1254 else
1255 c2 = x2.cond_code;
1257 return c1 == c2;
1260 /* Returns true if the predication is testing !=. */
1262 static inline bool
1263 is_neq_relop_p (pred_info pred)
1266 return (pred.cond_code == NE_EXPR && !pred.invert)
1267 || (pred.cond_code == EQ_EXPR && pred.invert);
1270 /* Returns true if pred is of the form X != 0. */
1272 static inline bool
1273 is_neq_zero_form_p (pred_info pred)
1275 if (!is_neq_relop_p (pred) || !integer_zerop (pred.pred_rhs)
1276 || TREE_CODE (pred.pred_lhs) != SSA_NAME)
1277 return false;
1278 return true;
1281 /* The helper function returns true if two predicates X1
1282 is equivalent to X2 != 0. */
1284 static inline bool
1285 pred_expr_equal_p (pred_info x1, tree x2)
1287 if (!is_neq_zero_form_p (x1))
1288 return false;
1290 return operand_equal_p (x1.pred_lhs, x2, 0);
1293 /* Returns true of the domain of single predicate expression
1294 EXPR1 is a subset of that of EXPR2. Returns false if it
1295 can not be proved. */
1297 static bool
1298 is_pred_expr_subset_of (pred_info expr1, pred_info expr2)
1300 enum tree_code code1, code2;
1302 if (pred_equal_p (expr1, expr2))
1303 return true;
1305 if ((TREE_CODE (expr1.pred_rhs) != INTEGER_CST)
1306 || (TREE_CODE (expr2.pred_rhs) != INTEGER_CST))
1307 return false;
1309 if (!operand_equal_p (expr1.pred_lhs, expr2.pred_lhs, 0))
1310 return false;
1312 code1 = expr1.cond_code;
1313 if (expr1.invert)
1314 code1 = invert_tree_comparison (code1, false);
1315 code2 = expr2.cond_code;
1316 if (expr2.invert)
1317 code2 = invert_tree_comparison (code2, false);
1319 if (code1 != code2 && code2 != NE_EXPR)
1320 return false;
1322 if (is_value_included_in (expr1.pred_rhs, expr2.pred_rhs, code2))
1323 return true;
1325 return false;
1328 /* Returns true if the domain of PRED1 is a subset
1329 of that of PRED2. Returns false if it can not be proved so. */
1331 static bool
1332 is_pred_chain_subset_of (pred_chain pred1,
1333 pred_chain pred2)
1335 size_t np1, np2, i1, i2;
1337 np1 = pred1.length ();
1338 np2 = pred2.length ();
1340 for (i2 = 0; i2 < np2; i2++)
1342 bool found = false;
1343 pred_info info2 = pred2[i2];
1344 for (i1 = 0; i1 < np1; i1++)
1346 pred_info info1 = pred1[i1];
1347 if (is_pred_expr_subset_of (info1, info2))
1349 found = true;
1350 break;
1353 if (!found)
1354 return false;
1356 return true;
1359 /* Returns true if the domain defined by
1360 one pred chain ONE_PRED is a subset of the domain
1361 of *PREDS. It returns false if ONE_PRED's domain is
1362 not a subset of any of the sub-domains of PREDS
1363 (corresponding to each individual chains in it), even
1364 though it may be still be a subset of whole domain
1365 of PREDS which is the union (ORed) of all its subdomains.
1366 In other words, the result is conservative. */
1368 static bool
1369 is_included_in (pred_chain one_pred, pred_chain_union preds)
1371 size_t i;
1372 size_t n = preds.length ();
1374 for (i = 0; i < n; i++)
1376 if (is_pred_chain_subset_of (one_pred, preds[i]))
1377 return true;
1380 return false;
1383 /* Compares two predicate sets PREDS1 and PREDS2 and returns
1384 true if the domain defined by PREDS1 is a superset
1385 of PREDS2's domain. N1 and N2 are array sizes of PREDS1 and
1386 PREDS2 respectively. The implementation chooses not to build
1387 generic trees (and relying on the folding capability of the
1388 compiler), but instead performs brute force comparison of
1389 individual predicate chains (won't be a compile time problem
1390 as the chains are pretty short). When the function returns
1391 false, it does not necessarily mean *PREDS1 is not a superset
1392 of *PREDS2, but mean it may not be so since the analysis can
1393 not prove it. In such cases, false warnings may still be
1394 emitted. */
1396 static bool
1397 is_superset_of (pred_chain_union preds1, pred_chain_union preds2)
1399 size_t i, n2;
1400 pred_chain one_pred_chain = vNULL;
1402 n2 = preds2.length ();
1404 for (i = 0; i < n2; i++)
1406 one_pred_chain = preds2[i];
1407 if (!is_included_in (one_pred_chain, preds1))
1408 return false;
1411 return true;
1414 /* Returns true if TC is AND or OR. */
1416 static inline bool
1417 is_and_or_or_p (enum tree_code tc, tree type)
1419 return (tc == BIT_IOR_EXPR
1420 || (tc == BIT_AND_EXPR
1421 && (type == 0 || TREE_CODE (type) == BOOLEAN_TYPE)));
1424 /* Returns true if X1 is the negate of X2. */
1426 static inline bool
1427 pred_neg_p (pred_info x1, pred_info x2)
1429 enum tree_code c1, c2;
1430 if (!operand_equal_p (x1.pred_lhs, x2.pred_lhs, 0)
1431 || !operand_equal_p (x1.pred_rhs, x2.pred_rhs, 0))
1432 return false;
1434 c1 = x1.cond_code;
1435 if (x1.invert == x2.invert)
1436 c2 = invert_tree_comparison (x2.cond_code, false);
1437 else
1438 c2 = x2.cond_code;
1440 return c1 == c2;
1443 /* 1) ((x IOR y) != 0) AND (x != 0) is equivalent to (x != 0);
1444 2) (X AND Y) OR (!X AND Y) is equivalent to Y;
1445 3) X OR (!X AND Y) is equivalent to (X OR Y);
1446 4) ((x IAND y) != 0) || (x != 0 AND y != 0)) is equivalent to
1447 (x != 0 AND y != 0)
1448 5) (X AND Y) OR (!X AND Z) OR (!Y AND Z) is equivalent to
1449 (X AND Y) OR Z
1451 PREDS is the predicate chains, and N is the number of chains. */
1453 /* Helper function to implement rule 1 above. ONE_CHAIN is
1454 the AND predication to be simplified. */
1456 static void
1457 simplify_pred (pred_chain *one_chain)
1459 size_t i, j, n;
1460 bool simplified = false;
1461 pred_chain s_chain = vNULL;
1463 n = one_chain->length ();
1465 for (i = 0; i < n; i++)
1467 pred_info *a_pred = &(*one_chain)[i];
1469 if (!a_pred->pred_lhs)
1470 continue;
1471 if (!is_neq_zero_form_p (*a_pred))
1472 continue;
1474 gimple def_stmt = SSA_NAME_DEF_STMT (a_pred->pred_lhs);
1475 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1476 continue;
1477 if (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR)
1479 for (j = 0; j < n; j++)
1481 pred_info *b_pred = &(*one_chain)[j];
1483 if (!b_pred->pred_lhs)
1484 continue;
1485 if (!is_neq_zero_form_p (*b_pred))
1486 continue;
1488 if (pred_expr_equal_p (*b_pred, gimple_assign_rhs1 (def_stmt))
1489 || pred_expr_equal_p (*b_pred, gimple_assign_rhs2 (def_stmt)))
1491 /* Mark a_pred for removal. */
1492 a_pred->pred_lhs = NULL;
1493 a_pred->pred_rhs = NULL;
1494 simplified = true;
1495 break;
1501 if (!simplified)
1502 return;
1504 for (i = 0; i < n; i++)
1506 pred_info *a_pred = &(*one_chain)[i];
1507 if (!a_pred->pred_lhs)
1508 continue;
1509 s_chain.safe_push (*a_pred);
1512 one_chain->release ();
1513 *one_chain = s_chain;
1516 /* The helper function implements the rule 2 for the
1517 OR predicate PREDS.
1519 2) (X AND Y) OR (!X AND Y) is equivalent to Y. */
1521 static bool
1522 simplify_preds_2 (pred_chain_union *preds)
1524 size_t i, j, n;
1525 bool simplified = false;
1526 pred_chain_union s_preds = vNULL;
1528 /* (X AND Y) OR (!X AND Y) is equivalent to Y.
1529 (X AND Y) OR (X AND !Y) is equivalent to X. */
1531 n = preds->length ();
1532 for (i = 0; i < n; i++)
1534 pred_info x, y;
1535 pred_chain *a_chain = &(*preds)[i];
1537 if (a_chain->length () != 2)
1538 continue;
1540 x = (*a_chain)[0];
1541 y = (*a_chain)[1];
1543 for (j = 0; j < n; j++)
1545 pred_chain *b_chain;
1546 pred_info x2, y2;
1548 if (j == i)
1549 continue;
1551 b_chain = &(*preds)[j];
1552 if (b_chain->length () != 2)
1553 continue;
1555 x2 = (*b_chain)[0];
1556 y2 = (*b_chain)[1];
1558 if (pred_equal_p (x, x2) && pred_neg_p (y, y2))
1560 /* Kill a_chain. */
1561 a_chain->release ();
1562 b_chain->release ();
1563 b_chain->safe_push (x);
1564 simplified = true;
1565 break;
1567 if (pred_neg_p (x, x2) && pred_equal_p (y, y2))
1569 /* Kill a_chain. */
1570 a_chain->release ();
1571 b_chain->release ();
1572 b_chain->safe_push (y);
1573 simplified = true;
1574 break;
1578 /* Now clean up the chain. */
1579 if (simplified)
1581 for (i = 0; i < n; i++)
1583 if ((*preds)[i].is_empty ())
1584 continue;
1585 s_preds.safe_push ((*preds)[i]);
1587 preds->release ();
1588 (*preds) = s_preds;
1589 s_preds = vNULL;
1592 return simplified;
1595 /* The helper function implements the rule 2 for the
1596 OR predicate PREDS.
1598 3) x OR (!x AND y) is equivalent to x OR y. */
1600 static bool
1601 simplify_preds_3 (pred_chain_union *preds)
1603 size_t i, j, n;
1604 bool simplified = false;
1606 /* Now iteratively simplify X OR (!X AND Z ..)
1607 into X OR (Z ...). */
1609 n = preds->length ();
1610 if (n < 2)
1611 return false;
1613 for (i = 0; i < n; i++)
1615 pred_info x;
1616 pred_chain *a_chain = &(*preds)[i];
1618 if (a_chain->length () != 1)
1619 continue;
1621 x = (*a_chain)[0];
1623 for (j = 0; j < n; j++)
1625 pred_chain *b_chain;
1626 pred_info x2;
1627 size_t k;
1629 if (j == i)
1630 continue;
1632 b_chain = &(*preds)[j];
1633 if (b_chain->length () < 2)
1634 continue;
1636 for (k = 0; k < b_chain->length (); k++)
1638 x2 = (*b_chain)[k];
1639 if (pred_neg_p (x, x2))
1641 b_chain->unordered_remove (k);
1642 simplified = true;
1643 break;
1648 return simplified;
1651 /* The helper function implements the rule 4 for the
1652 OR predicate PREDS.
1654 2) ((x AND y) != 0) OR (x != 0 AND y != 0) is equivalent to
1655 (x != 0 ANd y != 0). */
1657 static bool
1658 simplify_preds_4 (pred_chain_union *preds)
1660 size_t i, j, n;
1661 bool simplified = false;
1662 pred_chain_union s_preds = vNULL;
1663 gimple def_stmt;
1665 n = preds->length ();
1666 for (i = 0; i < n; i++)
1668 pred_info z;
1669 pred_chain *a_chain = &(*preds)[i];
1671 if (a_chain->length () != 1)
1672 continue;
1674 z = (*a_chain)[0];
1676 if (!is_neq_zero_form_p (z))
1677 continue;
1679 def_stmt = SSA_NAME_DEF_STMT (z.pred_lhs);
1680 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1681 continue;
1683 if (gimple_assign_rhs_code (def_stmt) != BIT_AND_EXPR)
1684 continue;
1686 for (j = 0; j < n; j++)
1688 pred_chain *b_chain;
1689 pred_info x2, y2;
1691 if (j == i)
1692 continue;
1694 b_chain = &(*preds)[j];
1695 if (b_chain->length () != 2)
1696 continue;
1698 x2 = (*b_chain)[0];
1699 y2 = (*b_chain)[1];
1700 if (!is_neq_zero_form_p (x2)
1701 || !is_neq_zero_form_p (y2))
1702 continue;
1704 if ((pred_expr_equal_p (x2, gimple_assign_rhs1 (def_stmt))
1705 && pred_expr_equal_p (y2, gimple_assign_rhs2 (def_stmt)))
1706 || (pred_expr_equal_p (x2, gimple_assign_rhs2 (def_stmt))
1707 && pred_expr_equal_p (y2, gimple_assign_rhs1 (def_stmt))))
1709 /* Kill a_chain. */
1710 a_chain->release ();
1711 simplified = true;
1712 break;
1716 /* Now clean up the chain. */
1717 if (simplified)
1719 for (i = 0; i < n; i++)
1721 if ((*preds)[i].is_empty ())
1722 continue;
1723 s_preds.safe_push ((*preds)[i]);
1725 preds->release ();
1726 (*preds) = s_preds;
1727 s_preds = vNULL;
1730 return simplified;
1734 /* This function simplifies predicates in PREDS. */
1736 static void
1737 simplify_preds (pred_chain_union *preds, gimple use_or_def, bool is_use)
1739 size_t i, n;
1740 bool changed = false;
1742 if (dump_file && dump_flags & TDF_DETAILS)
1744 fprintf (dump_file, "[BEFORE SIMPLICATION -- ");
1745 dump_predicates (use_or_def, *preds, is_use ? "[USE]:\n" : "[DEF]:\n");
1748 for (i = 0; i < preds->length (); i++)
1749 simplify_pred (&(*preds)[i]);
1751 n = preds->length ();
1752 if (n < 2)
1753 return;
1757 changed = false;
1758 if (simplify_preds_2 (preds))
1759 changed = true;
1761 /* Now iteratively simplify X OR (!X AND Z ..)
1762 into X OR (Z ...). */
1763 if (simplify_preds_3 (preds))
1764 changed = true;
1766 if (simplify_preds_4 (preds))
1767 changed = true;
1769 } while (changed);
1771 return;
1774 /* This is a helper function which attempts to normalize predicate chains
1775 by following UD chains. It basically builds up a big tree of either IOR
1776 operations or AND operations, and convert the IOR tree into a
1777 pred_chain_union or BIT_AND tree into a pred_chain.
1778 Example:
1780 _3 = _2 RELOP1 _1;
1781 _6 = _5 RELOP2 _4;
1782 _9 = _8 RELOP3 _7;
1783 _10 = _3 | _6;
1784 _12 = _9 | _0;
1785 _t = _10 | _12;
1787 then _t != 0 will be normalized into a pred_chain_union
1789 (_2 RELOP1 _1) OR (_5 RELOP2 _4) OR (_8 RELOP3 _7) OR (_0 != 0)
1791 Similarly given,
1793 _3 = _2 RELOP1 _1;
1794 _6 = _5 RELOP2 _4;
1795 _9 = _8 RELOP3 _7;
1796 _10 = _3 & _6;
1797 _12 = _9 & _0;
1799 then _t != 0 will be normalized into a pred_chain:
1800 (_2 RELOP1 _1) AND (_5 RELOP2 _4) AND (_8 RELOP3 _7) AND (_0 != 0)
1804 /* This is a helper function that stores a PRED into NORM_PREDS. */
1806 inline static void
1807 push_pred (pred_chain_union *norm_preds, pred_info pred)
1809 pred_chain pred_chain = vNULL;
1810 pred_chain.safe_push (pred);
1811 norm_preds->safe_push (pred_chain);
1814 /* A helper function that creates a predicate of the form
1815 OP != 0 and push it WORK_LIST. */
1817 inline static void
1818 push_to_worklist (tree op, vec<pred_info, va_heap, vl_ptr> *work_list,
1819 hash_set<tree> *mark_set)
1821 if (mark_set->contains (op))
1822 return;
1823 mark_set->add (op);
1825 pred_info arg_pred;
1826 arg_pred.pred_lhs = op;
1827 arg_pred.pred_rhs = integer_zero_node;
1828 arg_pred.cond_code = NE_EXPR;
1829 arg_pred.invert = false;
1830 work_list->safe_push (arg_pred);
1833 /* A helper that generates a pred_info from a gimple assignment
1834 CMP_ASSIGN with comparison rhs. */
1836 static pred_info
1837 get_pred_info_from_cmp (gimple cmp_assign)
1839 pred_info n_pred;
1840 n_pred.pred_lhs = gimple_assign_rhs1 (cmp_assign);
1841 n_pred.pred_rhs = gimple_assign_rhs2 (cmp_assign);
1842 n_pred.cond_code = gimple_assign_rhs_code (cmp_assign);
1843 n_pred.invert = false;
1844 return n_pred;
1847 /* Returns true if the PHI is a degenerated phi with
1848 all args with the same value (relop). In that case, *PRED
1849 will be updated to that value. */
1851 static bool
1852 is_degenerated_phi (gimple phi, pred_info *pred_p)
1854 int i, n;
1855 tree op0;
1856 gimple def0;
1857 pred_info pred0;
1859 n = gimple_phi_num_args (phi);
1860 op0 = gimple_phi_arg_def (phi, 0);
1862 if (TREE_CODE (op0) != SSA_NAME)
1863 return false;
1865 def0 = SSA_NAME_DEF_STMT (op0);
1866 if (gimple_code (def0) != GIMPLE_ASSIGN)
1867 return false;
1868 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def0))
1869 != tcc_comparison)
1870 return false;
1871 pred0 = get_pred_info_from_cmp (def0);
1873 for (i = 1; i < n; ++i)
1875 gimple def;
1876 pred_info pred;
1877 tree op = gimple_phi_arg_def (phi, i);
1879 if (TREE_CODE (op) != SSA_NAME)
1880 return false;
1882 def = SSA_NAME_DEF_STMT (op);
1883 if (gimple_code (def) != GIMPLE_ASSIGN)
1884 return false;
1885 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def))
1886 != tcc_comparison)
1887 return false;
1888 pred = get_pred_info_from_cmp (def);
1889 if (!pred_equal_p (pred, pred0))
1890 return false;
1893 *pred_p = pred0;
1894 return true;
1897 /* Normalize one predicate PRED
1898 1) if PRED can no longer be normlized, put it into NORM_PREDS.
1899 2) otherwise if PRED is of the form x != 0, follow x's definition
1900 and put normalized predicates into WORK_LIST. */
1902 static void
1903 normalize_one_pred_1 (pred_chain_union *norm_preds,
1904 pred_chain *norm_chain,
1905 pred_info pred,
1906 enum tree_code and_or_code,
1907 vec<pred_info, va_heap, vl_ptr> *work_list,
1908 hash_set<tree> *mark_set)
1910 if (!is_neq_zero_form_p (pred))
1912 if (and_or_code == BIT_IOR_EXPR)
1913 push_pred (norm_preds, pred);
1914 else
1915 norm_chain->safe_push (pred);
1916 return;
1919 gimple def_stmt = SSA_NAME_DEF_STMT (pred.pred_lhs);
1921 if (gimple_code (def_stmt) == GIMPLE_PHI
1922 && is_degenerated_phi (def_stmt, &pred))
1923 work_list->safe_push (pred);
1924 else if (gimple_code (def_stmt) == GIMPLE_PHI
1925 && and_or_code == BIT_IOR_EXPR)
1927 int i, n;
1928 n = gimple_phi_num_args (def_stmt);
1930 /* If we see non zero constant, we should punt. The predicate
1931 * should be one guarding the phi edge. */
1932 for (i = 0; i < n; ++i)
1934 tree op = gimple_phi_arg_def (def_stmt, i);
1935 if (TREE_CODE (op) == INTEGER_CST && !integer_zerop (op))
1937 push_pred (norm_preds, pred);
1938 return;
1942 for (i = 0; i < n; ++i)
1944 tree op = gimple_phi_arg_def (def_stmt, i);
1945 if (integer_zerop (op))
1946 continue;
1948 push_to_worklist (op, work_list, mark_set);
1951 else if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1953 if (and_or_code == BIT_IOR_EXPR)
1954 push_pred (norm_preds, pred);
1955 else
1956 norm_chain->safe_push (pred);
1958 else if (gimple_assign_rhs_code (def_stmt) == and_or_code)
1960 push_to_worklist (gimple_assign_rhs1 (def_stmt), work_list, mark_set);
1961 push_to_worklist (gimple_assign_rhs2 (def_stmt), work_list, mark_set);
1963 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt))
1964 == tcc_comparison)
1966 pred_info n_pred = get_pred_info_from_cmp (def_stmt);
1967 if (and_or_code == BIT_IOR_EXPR)
1968 push_pred (norm_preds, n_pred);
1969 else
1970 norm_chain->safe_push (n_pred);
1972 else
1974 if (and_or_code == BIT_IOR_EXPR)
1975 push_pred (norm_preds, pred);
1976 else
1977 norm_chain->safe_push (pred);
1981 /* Normalize PRED and store the normalized predicates into NORM_PREDS. */
1983 static void
1984 normalize_one_pred (pred_chain_union *norm_preds,
1985 pred_info pred)
1987 vec<pred_info, va_heap, vl_ptr> work_list = vNULL;
1988 enum tree_code and_or_code = ERROR_MARK;
1989 pred_chain norm_chain = vNULL;
1991 if (!is_neq_zero_form_p (pred))
1993 push_pred (norm_preds, pred);
1994 return;
1997 gimple def_stmt = SSA_NAME_DEF_STMT (pred.pred_lhs);
1998 if (gimple_code (def_stmt) == GIMPLE_ASSIGN)
1999 and_or_code = gimple_assign_rhs_code (def_stmt);
2000 if (and_or_code != BIT_IOR_EXPR
2001 && and_or_code != BIT_AND_EXPR)
2003 if (TREE_CODE_CLASS (and_or_code)
2004 == tcc_comparison)
2006 pred_info n_pred = get_pred_info_from_cmp (def_stmt);
2007 push_pred (norm_preds, n_pred);
2009 else
2010 push_pred (norm_preds, pred);
2011 return;
2014 work_list.safe_push (pred);
2015 hash_set<tree> mark_set;
2017 while (!work_list.is_empty ())
2019 pred_info a_pred = work_list.pop ();
2020 normalize_one_pred_1 (norm_preds, &norm_chain, a_pred,
2021 and_or_code, &work_list, &mark_set);
2023 if (and_or_code == BIT_AND_EXPR)
2024 norm_preds->safe_push (norm_chain);
2026 work_list.release ();
2029 static void
2030 normalize_one_pred_chain (pred_chain_union *norm_preds,
2031 pred_chain one_chain)
2033 vec<pred_info, va_heap, vl_ptr> work_list = vNULL;
2034 hash_set<tree> mark_set;
2035 pred_chain norm_chain = vNULL;
2036 size_t i;
2038 for (i = 0; i < one_chain.length (); i++)
2040 work_list.safe_push (one_chain[i]);
2041 mark_set.add (one_chain[i].pred_lhs);
2044 while (!work_list.is_empty ())
2046 pred_info a_pred = work_list.pop ();
2047 normalize_one_pred_1 (0, &norm_chain, a_pred,
2048 BIT_AND_EXPR, &work_list, &mark_set);
2051 norm_preds->safe_push (norm_chain);
2052 work_list.release ();
2055 /* Normalize predicate chains PREDS and returns the normalized one. */
2057 static pred_chain_union
2058 normalize_preds (pred_chain_union preds, gimple use_or_def, bool is_use)
2060 pred_chain_union norm_preds = vNULL;
2061 size_t n = preds.length ();
2062 size_t i;
2064 if (dump_file && dump_flags & TDF_DETAILS)
2066 fprintf (dump_file, "[BEFORE NORMALIZATION --");
2067 dump_predicates (use_or_def, preds, is_use ? "[USE]:\n" : "[DEF]:\n");
2070 for (i = 0; i < n; i++)
2072 if (preds[i].length () != 1)
2073 normalize_one_pred_chain (&norm_preds, preds[i]);
2074 else
2076 normalize_one_pred (&norm_preds, preds[i][0]);
2077 preds[i].release ();
2081 if (dump_file)
2083 fprintf (dump_file, "[AFTER NORMALIZATION -- ");
2084 dump_predicates (use_or_def, norm_preds, is_use ? "[USE]:\n" : "[DEF]:\n");
2087 preds.release ();
2088 return norm_preds;
2092 /* Computes the predicates that guard the use and checks
2093 if the incoming paths that have empty (or possibly
2094 empty) definition can be pruned/filtered. The function returns
2095 true if it can be determined that the use of PHI's def in
2096 USE_STMT is guarded with a predicate set not overlapping with
2097 predicate sets of all runtime paths that do not have a definition.
2098 Returns false if it is not or it can not be determined. USE_BB is
2099 the bb of the use (for phi operand use, the bb is not the bb of
2100 the phi stmt, but the src bb of the operand edge). UNINIT_OPNDS
2101 is a bit vector. If an operand of PHI is uninitialized, the
2102 corresponding bit in the vector is 1. VISIED_PHIS is a pointer
2103 set of phis being visted. */
2105 static bool
2106 is_use_properly_guarded (gimple use_stmt,
2107 basic_block use_bb,
2108 gimple phi,
2109 unsigned uninit_opnds,
2110 hash_set<gimple> *visited_phis)
2112 basic_block phi_bb;
2113 pred_chain_union preds = vNULL;
2114 pred_chain_union def_preds = vNULL;
2115 bool has_valid_preds = false;
2116 bool is_properly_guarded = false;
2118 if (visited_phis->add (phi))
2119 return false;
2121 phi_bb = gimple_bb (phi);
2123 if (is_non_loop_exit_postdominating (use_bb, phi_bb))
2124 return false;
2126 has_valid_preds = find_predicates (&preds, phi_bb, use_bb);
2128 if (!has_valid_preds)
2130 destroy_predicate_vecs (preds);
2131 return false;
2134 /* Try to prune the dead incoming phi edges. */
2135 is_properly_guarded
2136 = use_pred_not_overlap_with_undef_path_pred (preds, phi, uninit_opnds,
2137 visited_phis);
2139 if (is_properly_guarded)
2141 destroy_predicate_vecs (preds);
2142 return true;
2145 has_valid_preds = find_def_preds (&def_preds, phi);
2147 if (!has_valid_preds)
2149 destroy_predicate_vecs (preds);
2150 destroy_predicate_vecs (def_preds);
2151 return false;
2154 simplify_preds (&preds, use_stmt, true);
2155 preds = normalize_preds (preds, use_stmt, true);
2157 simplify_preds (&def_preds, phi, false);
2158 def_preds = normalize_preds (def_preds, phi, false);
2160 is_properly_guarded = is_superset_of (def_preds, preds);
2162 destroy_predicate_vecs (preds);
2163 destroy_predicate_vecs (def_preds);
2164 return is_properly_guarded;
2167 /* Searches through all uses of a potentially
2168 uninitialized variable defined by PHI and returns a use
2169 statement if the use is not properly guarded. It returns
2170 NULL if all uses are guarded. UNINIT_OPNDS is a bitvector
2171 holding the position(s) of uninit PHI operands. WORKLIST
2172 is the vector of candidate phis that may be updated by this
2173 function. ADDED_TO_WORKLIST is the pointer set tracking
2174 if the new phi is already in the worklist. */
2176 static gimple
2177 find_uninit_use (gimple phi, unsigned uninit_opnds,
2178 vec<gimple> *worklist,
2179 hash_set<gimple> *added_to_worklist)
2181 tree phi_result;
2182 use_operand_p use_p;
2183 gimple use_stmt;
2184 imm_use_iterator iter;
2186 phi_result = gimple_phi_result (phi);
2188 FOR_EACH_IMM_USE_FAST (use_p, iter, phi_result)
2190 basic_block use_bb;
2192 use_stmt = USE_STMT (use_p);
2193 if (is_gimple_debug (use_stmt))
2194 continue;
2196 if (gimple_code (use_stmt) == GIMPLE_PHI)
2197 use_bb = gimple_phi_arg_edge (use_stmt,
2198 PHI_ARG_INDEX_FROM_USE (use_p))->src;
2199 else
2200 use_bb = gimple_bb (use_stmt);
2202 hash_set<gimple> visited_phis;
2203 if (is_use_properly_guarded (use_stmt, use_bb, phi, uninit_opnds,
2204 &visited_phis))
2205 continue;
2207 if (dump_file && (dump_flags & TDF_DETAILS))
2209 fprintf (dump_file, "[CHECK]: Found unguarded use: ");
2210 print_gimple_stmt (dump_file, use_stmt, 0, 0);
2212 /* Found one real use, return. */
2213 if (gimple_code (use_stmt) != GIMPLE_PHI)
2214 return use_stmt;
2216 /* Found a phi use that is not guarded,
2217 add the phi to the worklist. */
2218 if (!added_to_worklist->add (use_stmt))
2220 if (dump_file && (dump_flags & TDF_DETAILS))
2222 fprintf (dump_file, "[WORKLIST]: Update worklist with phi: ");
2223 print_gimple_stmt (dump_file, use_stmt, 0, 0);
2226 worklist->safe_push (use_stmt);
2227 possibly_undefined_names->add (phi_result);
2231 return NULL;
2234 /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions
2235 and gives warning if there exists a runtime path from the entry to a
2236 use of the PHI def that does not contain a definition. In other words,
2237 the warning is on the real use. The more dead paths that can be pruned
2238 by the compiler, the fewer false positives the warning is. WORKLIST
2239 is a vector of candidate phis to be examined. ADDED_TO_WORKLIST is
2240 a pointer set tracking if the new phi is added to the worklist or not. */
2242 static void
2243 warn_uninitialized_phi (gimple phi, vec<gimple> *worklist,
2244 hash_set<gimple> *added_to_worklist)
2246 unsigned uninit_opnds;
2247 gimple uninit_use_stmt = 0;
2248 tree uninit_op;
2249 int phiarg_index;
2250 location_t loc;
2252 /* Don't look at virtual operands. */
2253 if (virtual_operand_p (gimple_phi_result (phi)))
2254 return;
2256 uninit_opnds = compute_uninit_opnds_pos (phi);
2258 if (MASK_EMPTY (uninit_opnds))
2259 return;
2261 if (dump_file && (dump_flags & TDF_DETAILS))
2263 fprintf (dump_file, "[CHECK]: examining phi: ");
2264 print_gimple_stmt (dump_file, phi, 0, 0);
2267 /* Now check if we have any use of the value without proper guard. */
2268 uninit_use_stmt = find_uninit_use (phi, uninit_opnds,
2269 worklist, added_to_worklist);
2271 /* All uses are properly guarded. */
2272 if (!uninit_use_stmt)
2273 return;
2275 phiarg_index = MASK_FIRST_SET_BIT (uninit_opnds);
2276 uninit_op = gimple_phi_arg_def (phi, phiarg_index);
2277 if (SSA_NAME_VAR (uninit_op) == NULL_TREE)
2278 return;
2279 if (gimple_phi_arg_has_location (phi, phiarg_index))
2280 loc = gimple_phi_arg_location (phi, phiarg_index);
2281 else
2282 loc = UNKNOWN_LOCATION;
2283 warn_uninit (OPT_Wmaybe_uninitialized, uninit_op, SSA_NAME_VAR (uninit_op),
2284 SSA_NAME_VAR (uninit_op),
2285 "%qD may be used uninitialized in this function",
2286 uninit_use_stmt, loc);
2290 static bool
2291 gate_warn_uninitialized (void)
2293 return warn_uninitialized || warn_maybe_uninitialized;
2296 namespace {
2298 const pass_data pass_data_late_warn_uninitialized =
2300 GIMPLE_PASS, /* type */
2301 "uninit", /* name */
2302 OPTGROUP_NONE, /* optinfo_flags */
2303 TV_NONE, /* tv_id */
2304 PROP_ssa, /* properties_required */
2305 0, /* properties_provided */
2306 0, /* properties_destroyed */
2307 0, /* todo_flags_start */
2308 0, /* todo_flags_finish */
2311 class pass_late_warn_uninitialized : public gimple_opt_pass
2313 public:
2314 pass_late_warn_uninitialized (gcc::context *ctxt)
2315 : gimple_opt_pass (pass_data_late_warn_uninitialized, ctxt)
2318 /* opt_pass methods: */
2319 opt_pass * clone () { return new pass_late_warn_uninitialized (m_ctxt); }
2320 virtual bool gate (function *) { return gate_warn_uninitialized (); }
2321 virtual unsigned int execute (function *);
2323 }; // class pass_late_warn_uninitialized
2325 unsigned int
2326 pass_late_warn_uninitialized::execute (function *fun)
2328 basic_block bb;
2329 gimple_stmt_iterator gsi;
2330 vec<gimple> worklist = vNULL;
2332 calculate_dominance_info (CDI_DOMINATORS);
2333 calculate_dominance_info (CDI_POST_DOMINATORS);
2334 /* Re-do the plain uninitialized variable check, as optimization may have
2335 straightened control flow. Do this first so that we don't accidentally
2336 get a "may be" warning when we'd have seen an "is" warning later. */
2337 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/1);
2339 timevar_push (TV_TREE_UNINIT);
2341 possibly_undefined_names = new hash_set<tree>;
2342 hash_set<gimple> added_to_worklist;
2344 /* Initialize worklist */
2345 FOR_EACH_BB_FN (bb, fun)
2346 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2348 gimple phi = gsi_stmt (gsi);
2349 size_t n, i;
2351 n = gimple_phi_num_args (phi);
2353 /* Don't look at virtual operands. */
2354 if (virtual_operand_p (gimple_phi_result (phi)))
2355 continue;
2357 for (i = 0; i < n; ++i)
2359 tree op = gimple_phi_arg_def (phi, i);
2360 if (TREE_CODE (op) == SSA_NAME
2361 && uninit_undefined_value_p (op))
2363 worklist.safe_push (phi);
2364 added_to_worklist.add (phi);
2365 if (dump_file && (dump_flags & TDF_DETAILS))
2367 fprintf (dump_file, "[WORKLIST]: add to initial list: ");
2368 print_gimple_stmt (dump_file, phi, 0, 0);
2370 break;
2375 while (worklist.length () != 0)
2377 gimple cur_phi = 0;
2378 cur_phi = worklist.pop ();
2379 warn_uninitialized_phi (cur_phi, &worklist, &added_to_worklist);
2382 worklist.release ();
2383 delete possibly_undefined_names;
2384 possibly_undefined_names = NULL;
2385 free_dominance_info (CDI_POST_DOMINATORS);
2386 timevar_pop (TV_TREE_UNINIT);
2387 return 0;
2390 } // anon namespace
2392 gimple_opt_pass *
2393 make_pass_late_warn_uninitialized (gcc::context *ctxt)
2395 return new pass_late_warn_uninitialized (ctxt);
2399 static unsigned int
2400 execute_early_warn_uninitialized (void)
2402 /* Currently, this pass runs always but
2403 execute_late_warn_uninitialized only runs with optimization. With
2404 optimization we want to warn about possible uninitialized as late
2405 as possible, thus don't do it here. However, without
2406 optimization we need to warn here about "may be uninitialized". */
2407 calculate_dominance_info (CDI_POST_DOMINATORS);
2409 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/!optimize);
2411 /* Post-dominator information can not be reliably updated. Free it
2412 after the use. */
2414 free_dominance_info (CDI_POST_DOMINATORS);
2415 return 0;
2419 namespace {
2421 const pass_data pass_data_early_warn_uninitialized =
2423 GIMPLE_PASS, /* type */
2424 "*early_warn_uninitialized", /* name */
2425 OPTGROUP_NONE, /* optinfo_flags */
2426 TV_TREE_UNINIT, /* tv_id */
2427 PROP_ssa, /* properties_required */
2428 0, /* properties_provided */
2429 0, /* properties_destroyed */
2430 0, /* todo_flags_start */
2431 0, /* todo_flags_finish */
2434 class pass_early_warn_uninitialized : public gimple_opt_pass
2436 public:
2437 pass_early_warn_uninitialized (gcc::context *ctxt)
2438 : gimple_opt_pass (pass_data_early_warn_uninitialized, ctxt)
2441 /* opt_pass methods: */
2442 virtual bool gate (function *) { return gate_warn_uninitialized (); }
2443 virtual unsigned int execute (function *)
2445 return execute_early_warn_uninitialized ();
2448 }; // class pass_early_warn_uninitialized
2450 } // anon namespace
2452 gimple_opt_pass *
2453 make_pass_early_warn_uninitialized (gcc::context *ctxt)
2455 return new pass_early_warn_uninitialized (ctxt);