Rebase.
[official-gcc.git] / gcc / tree-ssa-uninit.c
blobf2578b7e8f84c92ec7e403e1e767505c9f6a2b0d
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 "pointer-set.h"
34 #include "tree-ssa-alias.h"
35 #include "internal-fn.h"
36 #include "gimple-expr.h"
37 #include "is-a.h"
38 #include "gimple.h"
39 #include "gimple-iterator.h"
40 #include "gimple-ssa.h"
41 #include "tree-phinodes.h"
42 #include "ssa-iterators.h"
43 #include "tree-ssa.h"
44 #include "tree-inline.h"
45 #include "hashtab.h"
46 #include "tree-pass.h"
47 #include "diagnostic-core.h"
48 #include "params.h"
50 /* This implements the pass that does predicate aware warning on uses of
51 possibly uninitialized variables. The pass first collects the set of
52 possibly uninitialized SSA names. For each such name, it walks through
53 all its immediate uses. For each immediate use, it rebuilds the condition
54 expression (the predicate) that guards the use. The predicate is then
55 examined to see if the variable is always defined under that same condition.
56 This is done either by pruning the unrealizable paths that lead to the
57 default definitions or by checking if the predicate set that guards the
58 defining paths is a superset of the use predicate. */
61 /* Pointer set of potentially undefined ssa names, i.e.,
62 ssa names that are defined by phi with operands that
63 are not defined or potentially undefined. */
64 static hash_set<tree> *possibly_undefined_names = 0;
66 /* Bit mask handling macros. */
67 #define MASK_SET_BIT(mask, pos) mask |= (1 << pos)
68 #define MASK_TEST_BIT(mask, pos) (mask & (1 << pos))
69 #define MASK_EMPTY(mask) (mask == 0)
71 /* Returns the first bit position (starting from LSB)
72 in mask that is non zero. Returns -1 if the mask is empty. */
73 static int
74 get_mask_first_set_bit (unsigned mask)
76 int pos = 0;
77 if (mask == 0)
78 return -1;
80 while ((mask & (1 << pos)) == 0)
81 pos++;
83 return pos;
85 #define MASK_FIRST_SET_BIT(mask) get_mask_first_set_bit (mask)
87 /* Return true if T, an SSA_NAME, has an undefined value. */
88 static bool
89 has_undefined_value_p (tree t)
91 return (ssa_undefined_value_p (t)
92 || (possibly_undefined_names
93 && possibly_undefined_names->contains (t)));
98 /* Like has_undefined_value_p, but don't return true if TREE_NO_WARNING
99 is set on SSA_NAME_VAR. */
101 static inline bool
102 uninit_undefined_value_p (tree t) {
103 if (!has_undefined_value_p (t))
104 return false;
105 if (SSA_NAME_VAR (t) && TREE_NO_WARNING (SSA_NAME_VAR (t)))
106 return false;
107 return true;
110 /* Emit warnings for uninitialized variables. This is done in two passes.
112 The first pass notices real uses of SSA names with undefined values.
113 Such uses are unconditionally uninitialized, and we can be certain that
114 such a use is a mistake. This pass is run before most optimizations,
115 so that we catch as many as we can.
117 The second pass follows PHI nodes to find uses that are potentially
118 uninitialized. In this case we can't necessarily prove that the use
119 is really uninitialized. This pass is run after most optimizations,
120 so that we thread as many jumps and possible, and delete as much dead
121 code as possible, in order to reduce false positives. We also look
122 again for plain uninitialized variables, since optimization may have
123 changed conditionally uninitialized to unconditionally uninitialized. */
125 /* Emit a warning for EXPR based on variable VAR at the point in the
126 program T, an SSA_NAME, is used being uninitialized. The exact
127 warning text is in MSGID and DATA is the gimple stmt with info about
128 the location in source code. When DATA is a GIMPLE_PHI, PHIARG_IDX
129 gives which argument of the phi node to take the location from. WC
130 is the warning code. */
132 static void
133 warn_uninit (enum opt_code wc, tree t, tree expr, tree var,
134 const char *gmsgid, void *data, location_t phiarg_loc)
136 gimple context = (gimple) data;
137 location_t location, cfun_loc;
138 expanded_location xloc, floc;
140 /* Ignore COMPLEX_EXPR as initializing only a part of a complex
141 turns in a COMPLEX_EXPR with the not initialized part being
142 set to its previous (undefined) value. */
143 if (is_gimple_assign (context)
144 && gimple_assign_rhs_code (context) == COMPLEX_EXPR)
145 return;
146 if (!has_undefined_value_p (t))
147 return;
149 /* TREE_NO_WARNING either means we already warned, or the front end
150 wishes to suppress the warning. */
151 if ((context
152 && (gimple_no_warning_p (context)
153 || (gimple_assign_single_p (context)
154 && TREE_NO_WARNING (gimple_assign_rhs1 (context)))))
155 || TREE_NO_WARNING (expr))
156 return;
158 if (context != NULL && gimple_has_location (context))
159 location = gimple_location (context);
160 else if (phiarg_loc != UNKNOWN_LOCATION)
161 location = phiarg_loc;
162 else
163 location = DECL_SOURCE_LOCATION (var);
164 location = linemap_resolve_location (line_table, location,
165 LRK_SPELLING_LOCATION,
166 NULL);
167 cfun_loc = DECL_SOURCE_LOCATION (cfun->decl);
168 xloc = expand_location (location);
169 floc = expand_location (cfun_loc);
170 if (warning_at (location, wc, gmsgid, expr))
172 TREE_NO_WARNING (expr) = 1;
174 if (location == DECL_SOURCE_LOCATION (var))
175 return;
176 if (xloc.file != floc.file
177 || linemap_location_before_p (line_table,
178 location, cfun_loc)
179 || linemap_location_before_p (line_table,
180 cfun->function_end_locus,
181 location))
182 inform (DECL_SOURCE_LOCATION (var), "%qD was declared here", var);
186 static unsigned int
187 warn_uninitialized_vars (bool warn_possibly_uninitialized)
189 gimple_stmt_iterator gsi;
190 basic_block bb;
192 FOR_EACH_BB_FN (bb, cfun)
194 bool always_executed = dominated_by_p (CDI_POST_DOMINATORS,
195 single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)), bb);
196 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
198 gimple stmt = gsi_stmt (gsi);
199 use_operand_p use_p;
200 ssa_op_iter op_iter;
201 tree use;
203 if (is_gimple_debug (stmt))
204 continue;
206 /* We only do data flow with SSA_NAMEs, so that's all we
207 can warn about. */
208 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, op_iter, SSA_OP_USE)
210 use = USE_FROM_PTR (use_p);
211 if (always_executed)
212 warn_uninit (OPT_Wuninitialized, use,
213 SSA_NAME_VAR (use), SSA_NAME_VAR (use),
214 "%qD is used uninitialized in this function",
215 stmt, UNKNOWN_LOCATION);
216 else if (warn_possibly_uninitialized)
217 warn_uninit (OPT_Wmaybe_uninitialized, use,
218 SSA_NAME_VAR (use), SSA_NAME_VAR (use),
219 "%qD may be used uninitialized in this function",
220 stmt, UNKNOWN_LOCATION);
223 /* For memory the only cheap thing we can do is see if we
224 have a use of the default def of the virtual operand.
225 ??? Not so cheap would be to use the alias oracle via
226 walk_aliased_vdefs, if we don't find any aliasing vdef
227 warn as is-used-uninitialized, if we don't find an aliasing
228 vdef that kills our use (stmt_kills_ref_p), warn as
229 may-be-used-uninitialized. But this walk is quadratic and
230 so must be limited which means we would miss warning
231 opportunities. */
232 use = gimple_vuse (stmt);
233 if (use
234 && gimple_assign_single_p (stmt)
235 && !gimple_vdef (stmt)
236 && SSA_NAME_IS_DEFAULT_DEF (use))
238 tree rhs = gimple_assign_rhs1 (stmt);
239 tree base = get_base_address (rhs);
241 /* Do not warn if it can be initialized outside this function. */
242 if (TREE_CODE (base) != VAR_DECL
243 || DECL_HARD_REGISTER (base)
244 || is_global_var (base))
245 continue;
247 if (always_executed)
248 warn_uninit (OPT_Wuninitialized, use,
249 gimple_assign_rhs1 (stmt), base,
250 "%qE is used uninitialized in this function",
251 stmt, UNKNOWN_LOCATION);
252 else if (warn_possibly_uninitialized)
253 warn_uninit (OPT_Wmaybe_uninitialized, use,
254 gimple_assign_rhs1 (stmt), base,
255 "%qE may be used uninitialized in this function",
256 stmt, UNKNOWN_LOCATION);
261 return 0;
264 /* Checks if the operand OPND of PHI is defined by
265 another phi with one operand defined by this PHI,
266 but the rest operands are all defined. If yes,
267 returns true to skip this this operand as being
268 redundant. Can be enhanced to be more general. */
270 static bool
271 can_skip_redundant_opnd (tree opnd, gimple phi)
273 gimple op_def;
274 tree phi_def;
275 int i, n;
277 phi_def = gimple_phi_result (phi);
278 op_def = SSA_NAME_DEF_STMT (opnd);
279 if (gimple_code (op_def) != GIMPLE_PHI)
280 return false;
281 n = gimple_phi_num_args (op_def);
282 for (i = 0; i < n; ++i)
284 tree op = gimple_phi_arg_def (op_def, i);
285 if (TREE_CODE (op) != SSA_NAME)
286 continue;
287 if (op != phi_def && uninit_undefined_value_p (op))
288 return false;
291 return true;
294 /* Returns a bit mask holding the positions of arguments in PHI
295 that have empty (or possibly empty) definitions. */
297 static unsigned
298 compute_uninit_opnds_pos (gimple phi)
300 size_t i, n;
301 unsigned uninit_opnds = 0;
303 n = gimple_phi_num_args (phi);
304 /* Bail out for phi with too many args. */
305 if (n > 32)
306 return 0;
308 for (i = 0; i < n; ++i)
310 tree op = gimple_phi_arg_def (phi, i);
311 if (TREE_CODE (op) == SSA_NAME
312 && uninit_undefined_value_p (op)
313 && !can_skip_redundant_opnd (op, phi))
315 if (cfun->has_nonlocal_label || cfun->calls_setjmp)
317 /* Ignore SSA_NAMEs that appear on abnormal edges
318 somewhere. */
319 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
320 continue;
322 MASK_SET_BIT (uninit_opnds, i);
325 return uninit_opnds;
328 /* Find the immediate postdominator PDOM of the specified
329 basic block BLOCK. */
331 static inline basic_block
332 find_pdom (basic_block block)
334 if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
335 return EXIT_BLOCK_PTR_FOR_FN (cfun);
336 else
338 basic_block bb
339 = get_immediate_dominator (CDI_POST_DOMINATORS, block);
340 if (! bb)
341 return EXIT_BLOCK_PTR_FOR_FN (cfun);
342 return bb;
346 /* Find the immediate DOM of the specified
347 basic block BLOCK. */
349 static inline basic_block
350 find_dom (basic_block block)
352 if (block == ENTRY_BLOCK_PTR_FOR_FN (cfun))
353 return ENTRY_BLOCK_PTR_FOR_FN (cfun);
354 else
356 basic_block bb = get_immediate_dominator (CDI_DOMINATORS, block);
357 if (! bb)
358 return ENTRY_BLOCK_PTR_FOR_FN (cfun);
359 return bb;
363 /* Returns true if BB1 is postdominating BB2 and BB1 is
364 not a loop exit bb. The loop exit bb check is simple and does
365 not cover all cases. */
367 static bool
368 is_non_loop_exit_postdominating (basic_block bb1, basic_block bb2)
370 if (!dominated_by_p (CDI_POST_DOMINATORS, bb2, bb1))
371 return false;
373 if (single_pred_p (bb1) && !single_succ_p (bb2))
374 return false;
376 return true;
379 /* Find the closest postdominator of a specified BB, which is control
380 equivalent to BB. */
382 static inline basic_block
383 find_control_equiv_block (basic_block bb)
385 basic_block pdom;
387 pdom = find_pdom (bb);
389 /* Skip the postdominating bb that is also loop exit. */
390 if (!is_non_loop_exit_postdominating (pdom, bb))
391 return NULL;
393 if (dominated_by_p (CDI_DOMINATORS, pdom, bb))
394 return pdom;
396 return NULL;
399 #define MAX_NUM_CHAINS 8
400 #define MAX_CHAIN_LEN 5
401 #define MAX_POSTDOM_CHECK 8
403 /* Computes the control dependence chains (paths of edges)
404 for DEP_BB up to the dominating basic block BB (the head node of a
405 chain should be dominated by it). CD_CHAINS is pointer to an
406 array holding the result chains. CUR_CD_CHAIN is the current
407 chain being computed. *NUM_CHAINS is total number of chains. The
408 function returns true if the information is successfully computed,
409 return false if there is no control dependence or not computed. */
411 static bool
412 compute_control_dep_chain (basic_block bb, basic_block dep_bb,
413 vec<edge> *cd_chains,
414 size_t *num_chains,
415 vec<edge> *cur_cd_chain,
416 int *num_calls)
418 edge_iterator ei;
419 edge e;
420 size_t i;
421 bool found_cd_chain = false;
422 size_t cur_chain_len = 0;
424 if (EDGE_COUNT (bb->succs) < 2)
425 return false;
427 if (*num_calls > PARAM_VALUE (PARAM_UNINIT_CONTROL_DEP_ATTEMPTS))
428 return false;
429 ++*num_calls;
431 /* Could use a set instead. */
432 cur_chain_len = cur_cd_chain->length ();
433 if (cur_chain_len > MAX_CHAIN_LEN)
434 return false;
436 for (i = 0; i < cur_chain_len; i++)
438 edge e = (*cur_cd_chain)[i];
439 /* Cycle detected. */
440 if (e->src == bb)
441 return false;
444 FOR_EACH_EDGE (e, ei, bb->succs)
446 basic_block cd_bb;
447 int post_dom_check = 0;
448 if (e->flags & (EDGE_FAKE | EDGE_ABNORMAL))
449 continue;
451 cd_bb = e->dest;
452 cur_cd_chain->safe_push (e);
453 while (!is_non_loop_exit_postdominating (cd_bb, bb))
455 if (cd_bb == dep_bb)
457 /* Found a direct control dependence. */
458 if (*num_chains < MAX_NUM_CHAINS)
460 cd_chains[*num_chains] = cur_cd_chain->copy ();
461 (*num_chains)++;
463 found_cd_chain = true;
464 /* Check path from next edge. */
465 break;
468 /* Now check if DEP_BB is indirectly control dependent on BB. */
469 if (compute_control_dep_chain (cd_bb, dep_bb, cd_chains,
470 num_chains, cur_cd_chain, num_calls))
472 found_cd_chain = true;
473 break;
476 cd_bb = find_pdom (cd_bb);
477 post_dom_check++;
478 if (cd_bb == EXIT_BLOCK_PTR_FOR_FN (cfun) || post_dom_check >
479 MAX_POSTDOM_CHECK)
480 break;
482 cur_cd_chain->pop ();
483 gcc_assert (cur_cd_chain->length () == cur_chain_len);
485 gcc_assert (cur_cd_chain->length () == cur_chain_len);
487 return found_cd_chain;
490 /* The type to represent a simple predicate */
492 typedef struct use_def_pred_info
494 tree pred_lhs;
495 tree pred_rhs;
496 enum tree_code cond_code;
497 bool invert;
498 } pred_info;
500 /* The type to represent a sequence of predicates grouped
501 with .AND. operation. */
503 typedef vec<pred_info, va_heap, vl_ptr> pred_chain;
505 /* The type to represent a sequence of pred_chains grouped
506 with .OR. operation. */
508 typedef vec<pred_chain, va_heap, vl_ptr> pred_chain_union;
510 /* Converts the chains of control dependence edges into a set of
511 predicates. A control dependence chain is represented by a vector
512 edges. DEP_CHAINS points to an array of dependence chains.
513 NUM_CHAINS is the size of the chain array. One edge in a dependence
514 chain is mapped to predicate expression represented by pred_info
515 type. One dependence chain is converted to a composite predicate that
516 is the result of AND operation of pred_info mapped to each edge.
517 A composite predicate is presented by a vector of pred_info. On
518 return, *PREDS points to the resulting array of composite predicates.
519 *NUM_PREDS is the number of composite predictes. */
521 static bool
522 convert_control_dep_chain_into_preds (vec<edge> *dep_chains,
523 size_t num_chains,
524 pred_chain_union *preds)
526 bool has_valid_pred = false;
527 size_t i, j;
528 if (num_chains == 0 || num_chains >= MAX_NUM_CHAINS)
529 return false;
531 /* Now convert the control dep chain into a set
532 of predicates. */
533 preds->reserve (num_chains);
535 for (i = 0; i < num_chains; i++)
537 vec<edge> one_cd_chain = dep_chains[i];
539 has_valid_pred = false;
540 pred_chain t_chain = vNULL;
541 for (j = 0; j < one_cd_chain.length (); j++)
543 gimple cond_stmt;
544 gimple_stmt_iterator gsi;
545 basic_block guard_bb;
546 pred_info one_pred;
547 edge e;
549 e = one_cd_chain[j];
550 guard_bb = e->src;
551 gsi = gsi_last_bb (guard_bb);
552 if (gsi_end_p (gsi))
554 has_valid_pred = false;
555 break;
557 cond_stmt = gsi_stmt (gsi);
558 if (is_gimple_call (cond_stmt)
559 && EDGE_COUNT (e->src->succs) >= 2)
561 /* Ignore EH edge. Can add assertion
562 on the other edge's flag. */
563 continue;
565 /* Skip if there is essentially one succesor. */
566 if (EDGE_COUNT (e->src->succs) == 2)
568 edge e1;
569 edge_iterator ei1;
570 bool skip = false;
572 FOR_EACH_EDGE (e1, ei1, e->src->succs)
574 if (EDGE_COUNT (e1->dest->succs) == 0)
576 skip = true;
577 break;
580 if (skip)
581 continue;
583 if (gimple_code (cond_stmt) != GIMPLE_COND)
585 has_valid_pred = false;
586 break;
588 one_pred.pred_lhs = gimple_cond_lhs (cond_stmt);
589 one_pred.pred_rhs = gimple_cond_rhs (cond_stmt);
590 one_pred.cond_code = gimple_cond_code (cond_stmt);
591 one_pred.invert = !!(e->flags & EDGE_FALSE_VALUE);
592 t_chain.safe_push (one_pred);
593 has_valid_pred = true;
596 if (!has_valid_pred)
597 break;
598 else
599 preds->safe_push (t_chain);
601 return has_valid_pred;
604 /* Computes all control dependence chains for USE_BB. The control
605 dependence chains are then converted to an array of composite
606 predicates pointed to by PREDS. PHI_BB is the basic block of
607 the phi whose result is used in USE_BB. */
609 static bool
610 find_predicates (pred_chain_union *preds,
611 basic_block phi_bb,
612 basic_block use_bb)
614 size_t num_chains = 0, i;
615 int num_calls = 0;
616 vec<edge> dep_chains[MAX_NUM_CHAINS];
617 auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
618 bool has_valid_pred = false;
619 basic_block cd_root = 0;
621 /* First find the closest bb that is control equivalent to PHI_BB
622 that also dominates USE_BB. */
623 cd_root = phi_bb;
624 while (dominated_by_p (CDI_DOMINATORS, use_bb, cd_root))
626 basic_block ctrl_eq_bb = find_control_equiv_block (cd_root);
627 if (ctrl_eq_bb && dominated_by_p (CDI_DOMINATORS, use_bb, ctrl_eq_bb))
628 cd_root = ctrl_eq_bb;
629 else
630 break;
633 compute_control_dep_chain (cd_root, use_bb, dep_chains, &num_chains,
634 &cur_chain, &num_calls);
636 has_valid_pred
637 = convert_control_dep_chain_into_preds (dep_chains, num_chains, preds);
638 for (i = 0; i < num_chains; i++)
639 dep_chains[i].release ();
640 return has_valid_pred;
643 /* Computes the set of incoming edges of PHI that have non empty
644 definitions of a phi chain. The collection will be done
645 recursively on operands that are defined by phis. CD_ROOT
646 is the control dependence root. *EDGES holds the result, and
647 VISITED_PHIS is a pointer set for detecting cycles. */
649 static void
650 collect_phi_def_edges (gimple phi, basic_block cd_root,
651 vec<edge> *edges,
652 hash_set<gimple> *visited_phis)
654 size_t i, n;
655 edge opnd_edge;
656 tree opnd;
658 if (visited_phis->add (phi))
659 return;
661 n = gimple_phi_num_args (phi);
662 for (i = 0; i < n; i++)
664 opnd_edge = gimple_phi_arg_edge (phi, i);
665 opnd = gimple_phi_arg_def (phi, i);
667 if (TREE_CODE (opnd) != SSA_NAME)
669 if (dump_file && (dump_flags & TDF_DETAILS))
671 fprintf (dump_file, "\n[CHECK] Found def edge %d in ", (int)i);
672 print_gimple_stmt (dump_file, phi, 0, 0);
674 edges->safe_push (opnd_edge);
676 else
678 gimple def = SSA_NAME_DEF_STMT (opnd);
680 if (gimple_code (def) == GIMPLE_PHI
681 && dominated_by_p (CDI_DOMINATORS,
682 gimple_bb (def), cd_root))
683 collect_phi_def_edges (def, cd_root, edges,
684 visited_phis);
685 else if (!uninit_undefined_value_p (opnd))
687 if (dump_file && (dump_flags & TDF_DETAILS))
689 fprintf (dump_file, "\n[CHECK] Found def edge %d in ", (int)i);
690 print_gimple_stmt (dump_file, phi, 0, 0);
692 edges->safe_push (opnd_edge);
698 /* For each use edge of PHI, computes all control dependence chains.
699 The control dependence chains are then converted to an array of
700 composite predicates pointed to by PREDS. */
702 static bool
703 find_def_preds (pred_chain_union *preds, gimple phi)
705 size_t num_chains = 0, i, n;
706 vec<edge> dep_chains[MAX_NUM_CHAINS];
707 auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
708 vec<edge> def_edges = vNULL;
709 bool has_valid_pred = false;
710 basic_block phi_bb, cd_root = 0;
712 phi_bb = gimple_bb (phi);
713 /* First find the closest dominating bb to be
714 the control dependence root */
715 cd_root = find_dom (phi_bb);
716 if (!cd_root)
717 return false;
719 hash_set<gimple> visited_phis;
720 collect_phi_def_edges (phi, cd_root, &def_edges, &visited_phis);
722 n = def_edges.length ();
723 if (n == 0)
724 return false;
726 for (i = 0; i < n; i++)
728 size_t prev_nc, j;
729 int num_calls = 0;
730 edge opnd_edge;
732 opnd_edge = def_edges[i];
733 prev_nc = num_chains;
734 compute_control_dep_chain (cd_root, opnd_edge->src, dep_chains,
735 &num_chains, &cur_chain, &num_calls);
737 /* Now update the newly added chains with
738 the phi operand edge: */
739 if (EDGE_COUNT (opnd_edge->src->succs) > 1)
741 if (prev_nc == num_chains && num_chains < MAX_NUM_CHAINS)
742 dep_chains[num_chains++] = vNULL;
743 for (j = prev_nc; j < num_chains; j++)
744 dep_chains[j].safe_push (opnd_edge);
748 has_valid_pred
749 = convert_control_dep_chain_into_preds (dep_chains, num_chains, preds);
750 for (i = 0; i < num_chains; i++)
751 dep_chains[i].release ();
752 return has_valid_pred;
755 /* Dumps the predicates (PREDS) for USESTMT. */
757 static void
758 dump_predicates (gimple usestmt, pred_chain_union preds,
759 const char* msg)
761 size_t i, j;
762 pred_chain one_pred_chain = vNULL;
763 fprintf (dump_file, msg);
764 print_gimple_stmt (dump_file, usestmt, 0, 0);
765 fprintf (dump_file, "is guarded by :\n\n");
766 size_t num_preds = preds.length ();
767 /* Do some dumping here: */
768 for (i = 0; i < num_preds; i++)
770 size_t np;
772 one_pred_chain = preds[i];
773 np = one_pred_chain.length ();
775 for (j = 0; j < np; j++)
777 pred_info one_pred = one_pred_chain[j];
778 if (one_pred.invert)
779 fprintf (dump_file, " (.NOT.) ");
780 print_generic_expr (dump_file, one_pred.pred_lhs, 0);
781 fprintf (dump_file, " %s ", op_symbol_code (one_pred.cond_code));
782 print_generic_expr (dump_file, one_pred.pred_rhs, 0);
783 if (j < np - 1)
784 fprintf (dump_file, " (.AND.) ");
785 else
786 fprintf (dump_file, "\n");
788 if (i < num_preds - 1)
789 fprintf (dump_file, "(.OR.)\n");
790 else
791 fprintf (dump_file, "\n\n");
795 /* Destroys the predicate set *PREDS. */
797 static void
798 destroy_predicate_vecs (pred_chain_union preds)
800 size_t i;
802 size_t n = preds.length ();
803 for (i = 0; i < n; i++)
804 preds[i].release ();
805 preds.release ();
809 /* Computes the 'normalized' conditional code with operand
810 swapping and condition inversion. */
812 static enum tree_code
813 get_cmp_code (enum tree_code orig_cmp_code,
814 bool swap_cond, bool invert)
816 enum tree_code tc = orig_cmp_code;
818 if (swap_cond)
819 tc = swap_tree_comparison (orig_cmp_code);
820 if (invert)
821 tc = invert_tree_comparison (tc, false);
823 switch (tc)
825 case LT_EXPR:
826 case LE_EXPR:
827 case GT_EXPR:
828 case GE_EXPR:
829 case EQ_EXPR:
830 case NE_EXPR:
831 break;
832 default:
833 return ERROR_MARK;
835 return tc;
838 /* Returns true if VAL falls in the range defined by BOUNDARY and CMPC, i.e.
839 all values in the range satisfies (x CMPC BOUNDARY) == true. */
841 static bool
842 is_value_included_in (tree val, tree boundary, enum tree_code cmpc)
844 bool inverted = false;
845 bool is_unsigned;
846 bool result;
848 /* Only handle integer constant here. */
849 if (TREE_CODE (val) != INTEGER_CST
850 || TREE_CODE (boundary) != INTEGER_CST)
851 return true;
853 is_unsigned = TYPE_UNSIGNED (TREE_TYPE (val));
855 if (cmpc == GE_EXPR || cmpc == GT_EXPR
856 || cmpc == NE_EXPR)
858 cmpc = invert_tree_comparison (cmpc, false);
859 inverted = true;
862 if (is_unsigned)
864 if (cmpc == EQ_EXPR)
865 result = tree_int_cst_equal (val, boundary);
866 else if (cmpc == LT_EXPR)
867 result = tree_int_cst_lt (val, boundary);
868 else
870 gcc_assert (cmpc == LE_EXPR);
871 result = tree_int_cst_le (val, boundary);
874 else
876 if (cmpc == EQ_EXPR)
877 result = tree_int_cst_equal (val, boundary);
878 else if (cmpc == LT_EXPR)
879 result = tree_int_cst_lt (val, boundary);
880 else
882 gcc_assert (cmpc == LE_EXPR);
883 result = (tree_int_cst_equal (val, boundary)
884 || tree_int_cst_lt (val, boundary));
888 if (inverted)
889 result ^= 1;
891 return result;
894 /* Returns true if PRED is common among all the predicate
895 chains (PREDS) (and therefore can be factored out).
896 NUM_PRED_CHAIN is the size of array PREDS. */
898 static bool
899 find_matching_predicate_in_rest_chains (pred_info pred,
900 pred_chain_union preds,
901 size_t num_pred_chains)
903 size_t i, j, n;
905 /* Trival case. */
906 if (num_pred_chains == 1)
907 return true;
909 for (i = 1; i < num_pred_chains; i++)
911 bool found = false;
912 pred_chain one_chain = preds[i];
913 n = one_chain.length ();
914 for (j = 0; j < n; j++)
916 pred_info pred2 = one_chain[j];
917 /* Can relax the condition comparison to not
918 use address comparison. However, the most common
919 case is that multiple control dependent paths share
920 a common path prefix, so address comparison should
921 be ok. */
923 if (operand_equal_p (pred2.pred_lhs, pred.pred_lhs, 0)
924 && operand_equal_p (pred2.pred_rhs, pred.pred_rhs, 0)
925 && pred2.invert == pred.invert)
927 found = true;
928 break;
931 if (!found)
932 return false;
934 return true;
937 /* Forward declaration. */
938 static bool
939 is_use_properly_guarded (gimple use_stmt,
940 basic_block use_bb,
941 gimple phi,
942 unsigned uninit_opnds,
943 hash_set<gimple> *visited_phis);
945 /* Returns true if all uninitialized opnds are pruned. Returns false
946 otherwise. PHI is the phi node with uninitialized operands,
947 UNINIT_OPNDS is the bitmap of the uninitialize operand positions,
948 FLAG_DEF is the statement defining the flag guarding the use of the
949 PHI output, BOUNDARY_CST is the const value used in the predicate
950 associated with the flag, CMP_CODE is the comparison code used in
951 the predicate, VISITED_PHIS is the pointer set of phis visited, and
952 VISITED_FLAG_PHIS is the pointer to the pointer set of flag definitions
953 that are also phis.
955 Example scenario:
957 BB1:
958 flag_1 = phi <0, 1> // (1)
959 var_1 = phi <undef, some_val>
962 BB2:
963 flag_2 = phi <0, flag_1, flag_1> // (2)
964 var_2 = phi <undef, var_1, var_1>
965 if (flag_2 == 1)
966 goto BB3;
968 BB3:
969 use of var_2 // (3)
971 Because some flag arg in (1) is not constant, if we do not look into the
972 flag phis recursively, it is conservatively treated as unknown and var_1
973 is thought to be flowed into use at (3). Since var_1 is potentially uninitialized
974 a false warning will be emitted. Checking recursively into (1), the compiler can
975 find out that only some_val (which is defined) can flow into (3) which is OK.
979 static bool
980 prune_uninit_phi_opnds_in_unrealizable_paths (gimple phi,
981 unsigned uninit_opnds,
982 gimple flag_def,
983 tree boundary_cst,
984 enum tree_code cmp_code,
985 hash_set<gimple> *visited_phis,
986 bitmap *visited_flag_phis)
988 unsigned i;
990 for (i = 0; i < MIN (32, gimple_phi_num_args (flag_def)); i++)
992 tree flag_arg;
994 if (!MASK_TEST_BIT (uninit_opnds, i))
995 continue;
997 flag_arg = gimple_phi_arg_def (flag_def, i);
998 if (!is_gimple_constant (flag_arg))
1000 gimple flag_arg_def, phi_arg_def;
1001 tree phi_arg;
1002 unsigned uninit_opnds_arg_phi;
1004 if (TREE_CODE (flag_arg) != SSA_NAME)
1005 return false;
1006 flag_arg_def = SSA_NAME_DEF_STMT (flag_arg);
1007 if (gimple_code (flag_arg_def) != GIMPLE_PHI)
1008 return false;
1010 phi_arg = gimple_phi_arg_def (phi, i);
1011 if (TREE_CODE (phi_arg) != SSA_NAME)
1012 return false;
1014 phi_arg_def = SSA_NAME_DEF_STMT (phi_arg);
1015 if (gimple_code (phi_arg_def) != GIMPLE_PHI)
1016 return false;
1018 if (gimple_bb (phi_arg_def) != gimple_bb (flag_arg_def))
1019 return false;
1021 if (!*visited_flag_phis)
1022 *visited_flag_phis = BITMAP_ALLOC (NULL);
1024 if (bitmap_bit_p (*visited_flag_phis,
1025 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def))))
1026 return false;
1028 bitmap_set_bit (*visited_flag_phis,
1029 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def)));
1031 /* Now recursively prune the uninitialized phi args. */
1032 uninit_opnds_arg_phi = compute_uninit_opnds_pos (phi_arg_def);
1033 if (!prune_uninit_phi_opnds_in_unrealizable_paths
1034 (phi_arg_def, uninit_opnds_arg_phi, flag_arg_def,
1035 boundary_cst, cmp_code, visited_phis, visited_flag_phis))
1036 return false;
1038 bitmap_clear_bit (*visited_flag_phis,
1039 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def)));
1040 continue;
1043 /* Now check if the constant is in the guarded range. */
1044 if (is_value_included_in (flag_arg, boundary_cst, cmp_code))
1046 tree opnd;
1047 gimple opnd_def;
1049 /* Now that we know that this undefined edge is not
1050 pruned. If the operand is defined by another phi,
1051 we can further prune the incoming edges of that
1052 phi by checking the predicates of this operands. */
1054 opnd = gimple_phi_arg_def (phi, i);
1055 opnd_def = SSA_NAME_DEF_STMT (opnd);
1056 if (gimple_code (opnd_def) == GIMPLE_PHI)
1058 edge opnd_edge;
1059 unsigned uninit_opnds2
1060 = compute_uninit_opnds_pos (opnd_def);
1061 gcc_assert (!MASK_EMPTY (uninit_opnds2));
1062 opnd_edge = gimple_phi_arg_edge (phi, i);
1063 if (!is_use_properly_guarded (phi,
1064 opnd_edge->src,
1065 opnd_def,
1066 uninit_opnds2,
1067 visited_phis))
1068 return false;
1070 else
1071 return false;
1075 return true;
1078 /* A helper function that determines if the predicate set
1079 of the use is not overlapping with that of the uninit paths.
1080 The most common senario of guarded use is in Example 1:
1081 Example 1:
1082 if (some_cond)
1084 x = ...;
1085 flag = true;
1088 ... some code ...
1090 if (flag)
1091 use (x);
1093 The real world examples are usually more complicated, but similar
1094 and usually result from inlining:
1096 bool init_func (int * x)
1098 if (some_cond)
1099 return false;
1100 *x = ..
1101 return true;
1104 void foo(..)
1106 int x;
1108 if (!init_func(&x))
1109 return;
1111 .. some_code ...
1112 use (x);
1115 Another possible use scenario is in the following trivial example:
1117 Example 2:
1118 if (n > 0)
1119 x = 1;
1121 if (n > 0)
1123 if (m < 2)
1124 .. = x;
1127 Predicate analysis needs to compute the composite predicate:
1129 1) 'x' use predicate: (n > 0) .AND. (m < 2)
1130 2) 'x' default value (non-def) predicate: .NOT. (n > 0)
1131 (the predicate chain for phi operand defs can be computed
1132 starting from a bb that is control equivalent to the phi's
1133 bb and is dominating the operand def.)
1135 and check overlapping:
1136 (n > 0) .AND. (m < 2) .AND. (.NOT. (n > 0))
1137 <==> false
1139 This implementation provides framework that can handle
1140 scenarios. (Note that many simple cases are handled properly
1141 without the predicate analysis -- this is due to jump threading
1142 transformation which eliminates the merge point thus makes
1143 path sensitive analysis unnecessary.)
1145 NUM_PREDS is the number is the number predicate chains, PREDS is
1146 the array of chains, PHI is the phi node whose incoming (undefined)
1147 paths need to be pruned, and UNINIT_OPNDS is the bitmap holding
1148 uninit operand positions. VISITED_PHIS is the pointer set of phi
1149 stmts being checked. */
1152 static bool
1153 use_pred_not_overlap_with_undef_path_pred (pred_chain_union preds,
1154 gimple phi, unsigned uninit_opnds,
1155 hash_set<gimple> *visited_phis)
1157 unsigned int i, n;
1158 gimple flag_def = 0;
1159 tree boundary_cst = 0;
1160 enum tree_code cmp_code;
1161 bool swap_cond = false;
1162 bool invert = false;
1163 pred_chain the_pred_chain = vNULL;
1164 bitmap visited_flag_phis = NULL;
1165 bool all_pruned = false;
1166 size_t num_preds = preds.length ();
1168 gcc_assert (num_preds > 0);
1169 /* Find within the common prefix of multiple predicate chains
1170 a predicate that is a comparison of a flag variable against
1171 a constant. */
1172 the_pred_chain = preds[0];
1173 n = the_pred_chain.length ();
1174 for (i = 0; i < n; i++)
1176 tree cond_lhs, cond_rhs, flag = 0;
1178 pred_info the_pred = the_pred_chain[i];
1180 invert = the_pred.invert;
1181 cond_lhs = the_pred.pred_lhs;
1182 cond_rhs = the_pred.pred_rhs;
1183 cmp_code = the_pred.cond_code;
1185 if (cond_lhs != NULL_TREE && TREE_CODE (cond_lhs) == SSA_NAME
1186 && cond_rhs != NULL_TREE && is_gimple_constant (cond_rhs))
1188 boundary_cst = cond_rhs;
1189 flag = cond_lhs;
1191 else if (cond_rhs != NULL_TREE && TREE_CODE (cond_rhs) == SSA_NAME
1192 && cond_lhs != NULL_TREE && is_gimple_constant (cond_lhs))
1194 boundary_cst = cond_lhs;
1195 flag = cond_rhs;
1196 swap_cond = true;
1199 if (!flag)
1200 continue;
1202 flag_def = SSA_NAME_DEF_STMT (flag);
1204 if (!flag_def)
1205 continue;
1207 if ((gimple_code (flag_def) == GIMPLE_PHI)
1208 && (gimple_bb (flag_def) == gimple_bb (phi))
1209 && find_matching_predicate_in_rest_chains (the_pred, preds,
1210 num_preds))
1211 break;
1213 flag_def = 0;
1216 if (!flag_def)
1217 return false;
1219 /* Now check all the uninit incoming edge has a constant flag value
1220 that is in conflict with the use guard/predicate. */
1221 cmp_code = get_cmp_code (cmp_code, swap_cond, invert);
1223 if (cmp_code == ERROR_MARK)
1224 return false;
1226 all_pruned = prune_uninit_phi_opnds_in_unrealizable_paths (phi,
1227 uninit_opnds,
1228 flag_def,
1229 boundary_cst,
1230 cmp_code,
1231 visited_phis,
1232 &visited_flag_phis);
1234 if (visited_flag_phis)
1235 BITMAP_FREE (visited_flag_phis);
1237 return all_pruned;
1240 /* The helper function returns true if two predicates X1 and X2
1241 are equivalent. It assumes the expressions have already
1242 properly re-associated. */
1244 static inline bool
1245 pred_equal_p (pred_info x1, pred_info x2)
1247 enum tree_code c1, c2;
1248 if (!operand_equal_p (x1.pred_lhs, x2.pred_lhs, 0)
1249 || !operand_equal_p (x1.pred_rhs, x2.pred_rhs, 0))
1250 return false;
1252 c1 = x1.cond_code;
1253 if (x1.invert != x2.invert)
1254 c2 = invert_tree_comparison (x2.cond_code, false);
1255 else
1256 c2 = x2.cond_code;
1258 return c1 == c2;
1261 /* Returns true if the predication is testing !=. */
1263 static inline bool
1264 is_neq_relop_p (pred_info pred)
1267 return (pred.cond_code == NE_EXPR && !pred.invert)
1268 || (pred.cond_code == EQ_EXPR && pred.invert);
1271 /* Returns true if pred is of the form X != 0. */
1273 static inline bool
1274 is_neq_zero_form_p (pred_info pred)
1276 if (!is_neq_relop_p (pred) || !integer_zerop (pred.pred_rhs)
1277 || TREE_CODE (pred.pred_lhs) != SSA_NAME)
1278 return false;
1279 return true;
1282 /* The helper function returns true if two predicates X1
1283 is equivalent to X2 != 0. */
1285 static inline bool
1286 pred_expr_equal_p (pred_info x1, tree x2)
1288 if (!is_neq_zero_form_p (x1))
1289 return false;
1291 return operand_equal_p (x1.pred_lhs, x2, 0);
1294 /* Returns true of the domain of single predicate expression
1295 EXPR1 is a subset of that of EXPR2. Returns false if it
1296 can not be proved. */
1298 static bool
1299 is_pred_expr_subset_of (pred_info expr1, pred_info expr2)
1301 enum tree_code code1, code2;
1303 if (pred_equal_p (expr1, expr2))
1304 return true;
1306 if ((TREE_CODE (expr1.pred_rhs) != INTEGER_CST)
1307 || (TREE_CODE (expr2.pred_rhs) != INTEGER_CST))
1308 return false;
1310 if (!operand_equal_p (expr1.pred_lhs, expr2.pred_lhs, 0))
1311 return false;
1313 code1 = expr1.cond_code;
1314 if (expr1.invert)
1315 code1 = invert_tree_comparison (code1, false);
1316 code2 = expr2.cond_code;
1317 if (expr2.invert)
1318 code2 = invert_tree_comparison (code2, false);
1320 if (code1 != code2 && code2 != NE_EXPR)
1321 return false;
1323 if (is_value_included_in (expr1.pred_rhs, expr2.pred_rhs, code2))
1324 return true;
1326 return false;
1329 /* Returns true if the domain of PRED1 is a subset
1330 of that of PRED2. Returns false if it can not be proved so. */
1332 static bool
1333 is_pred_chain_subset_of (pred_chain pred1,
1334 pred_chain pred2)
1336 size_t np1, np2, i1, i2;
1338 np1 = pred1.length ();
1339 np2 = pred2.length ();
1341 for (i2 = 0; i2 < np2; i2++)
1343 bool found = false;
1344 pred_info info2 = pred2[i2];
1345 for (i1 = 0; i1 < np1; i1++)
1347 pred_info info1 = pred1[i1];
1348 if (is_pred_expr_subset_of (info1, info2))
1350 found = true;
1351 break;
1354 if (!found)
1355 return false;
1357 return true;
1360 /* Returns true if the domain defined by
1361 one pred chain ONE_PRED is a subset of the domain
1362 of *PREDS. It returns false if ONE_PRED's domain is
1363 not a subset of any of the sub-domains of PREDS
1364 (corresponding to each individual chains in it), even
1365 though it may be still be a subset of whole domain
1366 of PREDS which is the union (ORed) of all its subdomains.
1367 In other words, the result is conservative. */
1369 static bool
1370 is_included_in (pred_chain one_pred, pred_chain_union preds)
1372 size_t i;
1373 size_t n = preds.length ();
1375 for (i = 0; i < n; i++)
1377 if (is_pred_chain_subset_of (one_pred, preds[i]))
1378 return true;
1381 return false;
1384 /* Compares two predicate sets PREDS1 and PREDS2 and returns
1385 true if the domain defined by PREDS1 is a superset
1386 of PREDS2's domain. N1 and N2 are array sizes of PREDS1 and
1387 PREDS2 respectively. The implementation chooses not to build
1388 generic trees (and relying on the folding capability of the
1389 compiler), but instead performs brute force comparison of
1390 individual predicate chains (won't be a compile time problem
1391 as the chains are pretty short). When the function returns
1392 false, it does not necessarily mean *PREDS1 is not a superset
1393 of *PREDS2, but mean it may not be so since the analysis can
1394 not prove it. In such cases, false warnings may still be
1395 emitted. */
1397 static bool
1398 is_superset_of (pred_chain_union preds1, pred_chain_union preds2)
1400 size_t i, n2;
1401 pred_chain one_pred_chain = vNULL;
1403 n2 = preds2.length ();
1405 for (i = 0; i < n2; i++)
1407 one_pred_chain = preds2[i];
1408 if (!is_included_in (one_pred_chain, preds1))
1409 return false;
1412 return true;
1415 /* Returns true if TC is AND or OR. */
1417 static inline bool
1418 is_and_or_or_p (enum tree_code tc, tree type)
1420 return (tc == BIT_IOR_EXPR
1421 || (tc == BIT_AND_EXPR
1422 && (type == 0 || TREE_CODE (type) == BOOLEAN_TYPE)));
1425 /* Returns true if X1 is the negate of X2. */
1427 static inline bool
1428 pred_neg_p (pred_info x1, pred_info x2)
1430 enum tree_code c1, c2;
1431 if (!operand_equal_p (x1.pred_lhs, x2.pred_lhs, 0)
1432 || !operand_equal_p (x1.pred_rhs, x2.pred_rhs, 0))
1433 return false;
1435 c1 = x1.cond_code;
1436 if (x1.invert == x2.invert)
1437 c2 = invert_tree_comparison (x2.cond_code, false);
1438 else
1439 c2 = x2.cond_code;
1441 return c1 == c2;
1444 /* 1) ((x IOR y) != 0) AND (x != 0) is equivalent to (x != 0);
1445 2) (X AND Y) OR (!X AND Y) is equivalent to Y;
1446 3) X OR (!X AND Y) is equivalent to (X OR Y);
1447 4) ((x IAND y) != 0) || (x != 0 AND y != 0)) is equivalent to
1448 (x != 0 AND y != 0)
1449 5) (X AND Y) OR (!X AND Z) OR (!Y AND Z) is equivalent to
1450 (X AND Y) OR Z
1452 PREDS is the predicate chains, and N is the number of chains. */
1454 /* Helper function to implement rule 1 above. ONE_CHAIN is
1455 the AND predication to be simplified. */
1457 static void
1458 simplify_pred (pred_chain *one_chain)
1460 size_t i, j, n;
1461 bool simplified = false;
1462 pred_chain s_chain = vNULL;
1464 n = one_chain->length ();
1466 for (i = 0; i < n; i++)
1468 pred_info *a_pred = &(*one_chain)[i];
1470 if (!a_pred->pred_lhs)
1471 continue;
1472 if (!is_neq_zero_form_p (*a_pred))
1473 continue;
1475 gimple def_stmt = SSA_NAME_DEF_STMT (a_pred->pred_lhs);
1476 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1477 continue;
1478 if (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR)
1480 for (j = 0; j < n; j++)
1482 pred_info *b_pred = &(*one_chain)[j];
1484 if (!b_pred->pred_lhs)
1485 continue;
1486 if (!is_neq_zero_form_p (*b_pred))
1487 continue;
1489 if (pred_expr_equal_p (*b_pred, gimple_assign_rhs1 (def_stmt))
1490 || pred_expr_equal_p (*b_pred, gimple_assign_rhs2 (def_stmt)))
1492 /* Mark a_pred for removal. */
1493 a_pred->pred_lhs = NULL;
1494 a_pred->pred_rhs = NULL;
1495 simplified = true;
1496 break;
1502 if (!simplified)
1503 return;
1505 for (i = 0; i < n; i++)
1507 pred_info *a_pred = &(*one_chain)[i];
1508 if (!a_pred->pred_lhs)
1509 continue;
1510 s_chain.safe_push (*a_pred);
1513 one_chain->release ();
1514 *one_chain = s_chain;
1517 /* The helper function implements the rule 2 for the
1518 OR predicate PREDS.
1520 2) (X AND Y) OR (!X AND Y) is equivalent to Y. */
1522 static bool
1523 simplify_preds_2 (pred_chain_union *preds)
1525 size_t i, j, n;
1526 bool simplified = false;
1527 pred_chain_union s_preds = vNULL;
1529 /* (X AND Y) OR (!X AND Y) is equivalent to Y.
1530 (X AND Y) OR (X AND !Y) is equivalent to X. */
1532 n = preds->length ();
1533 for (i = 0; i < n; i++)
1535 pred_info x, y;
1536 pred_chain *a_chain = &(*preds)[i];
1538 if (a_chain->length () != 2)
1539 continue;
1541 x = (*a_chain)[0];
1542 y = (*a_chain)[1];
1544 for (j = 0; j < n; j++)
1546 pred_chain *b_chain;
1547 pred_info x2, y2;
1549 if (j == i)
1550 continue;
1552 b_chain = &(*preds)[j];
1553 if (b_chain->length () != 2)
1554 continue;
1556 x2 = (*b_chain)[0];
1557 y2 = (*b_chain)[1];
1559 if (pred_equal_p (x, x2) && pred_neg_p (y, y2))
1561 /* Kill a_chain. */
1562 a_chain->release ();
1563 b_chain->release ();
1564 b_chain->safe_push (x);
1565 simplified = true;
1566 break;
1568 if (pred_neg_p (x, x2) && pred_equal_p (y, y2))
1570 /* Kill a_chain. */
1571 a_chain->release ();
1572 b_chain->release ();
1573 b_chain->safe_push (y);
1574 simplified = true;
1575 break;
1579 /* Now clean up the chain. */
1580 if (simplified)
1582 for (i = 0; i < n; i++)
1584 if ((*preds)[i].is_empty ())
1585 continue;
1586 s_preds.safe_push ((*preds)[i]);
1588 preds->release ();
1589 (*preds) = s_preds;
1590 s_preds = vNULL;
1593 return simplified;
1596 /* The helper function implements the rule 2 for the
1597 OR predicate PREDS.
1599 3) x OR (!x AND y) is equivalent to x OR y. */
1601 static bool
1602 simplify_preds_3 (pred_chain_union *preds)
1604 size_t i, j, n;
1605 bool simplified = false;
1607 /* Now iteratively simplify X OR (!X AND Z ..)
1608 into X OR (Z ...). */
1610 n = preds->length ();
1611 if (n < 2)
1612 return false;
1614 for (i = 0; i < n; i++)
1616 pred_info x;
1617 pred_chain *a_chain = &(*preds)[i];
1619 if (a_chain->length () != 1)
1620 continue;
1622 x = (*a_chain)[0];
1624 for (j = 0; j < n; j++)
1626 pred_chain *b_chain;
1627 pred_info x2;
1628 size_t k;
1630 if (j == i)
1631 continue;
1633 b_chain = &(*preds)[j];
1634 if (b_chain->length () < 2)
1635 continue;
1637 for (k = 0; k < b_chain->length (); k++)
1639 x2 = (*b_chain)[k];
1640 if (pred_neg_p (x, x2))
1642 b_chain->unordered_remove (k);
1643 simplified = true;
1644 break;
1649 return simplified;
1652 /* The helper function implements the rule 4 for the
1653 OR predicate PREDS.
1655 2) ((x AND y) != 0) OR (x != 0 AND y != 0) is equivalent to
1656 (x != 0 ANd y != 0). */
1658 static bool
1659 simplify_preds_4 (pred_chain_union *preds)
1661 size_t i, j, n;
1662 bool simplified = false;
1663 pred_chain_union s_preds = vNULL;
1664 gimple def_stmt;
1666 n = preds->length ();
1667 for (i = 0; i < n; i++)
1669 pred_info z;
1670 pred_chain *a_chain = &(*preds)[i];
1672 if (a_chain->length () != 1)
1673 continue;
1675 z = (*a_chain)[0];
1677 if (!is_neq_zero_form_p (z))
1678 continue;
1680 def_stmt = SSA_NAME_DEF_STMT (z.pred_lhs);
1681 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1682 continue;
1684 if (gimple_assign_rhs_code (def_stmt) != BIT_AND_EXPR)
1685 continue;
1687 for (j = 0; j < n; j++)
1689 pred_chain *b_chain;
1690 pred_info x2, y2;
1692 if (j == i)
1693 continue;
1695 b_chain = &(*preds)[j];
1696 if (b_chain->length () != 2)
1697 continue;
1699 x2 = (*b_chain)[0];
1700 y2 = (*b_chain)[1];
1701 if (!is_neq_zero_form_p (x2)
1702 || !is_neq_zero_form_p (y2))
1703 continue;
1705 if ((pred_expr_equal_p (x2, gimple_assign_rhs1 (def_stmt))
1706 && pred_expr_equal_p (y2, gimple_assign_rhs2 (def_stmt)))
1707 || (pred_expr_equal_p (x2, gimple_assign_rhs2 (def_stmt))
1708 && pred_expr_equal_p (y2, gimple_assign_rhs1 (def_stmt))))
1710 /* Kill a_chain. */
1711 a_chain->release ();
1712 simplified = true;
1713 break;
1717 /* Now clean up the chain. */
1718 if (simplified)
1720 for (i = 0; i < n; i++)
1722 if ((*preds)[i].is_empty ())
1723 continue;
1724 s_preds.safe_push ((*preds)[i]);
1726 preds->release ();
1727 (*preds) = s_preds;
1728 s_preds = vNULL;
1731 return simplified;
1735 /* This function simplifies predicates in PREDS. */
1737 static void
1738 simplify_preds (pred_chain_union *preds, gimple use_or_def, bool is_use)
1740 size_t i, n;
1741 bool changed = false;
1743 if (dump_file && dump_flags & TDF_DETAILS)
1745 fprintf (dump_file, "[BEFORE SIMPLICATION -- ");
1746 dump_predicates (use_or_def, *preds, is_use ? "[USE]:\n" : "[DEF]:\n");
1749 for (i = 0; i < preds->length (); i++)
1750 simplify_pred (&(*preds)[i]);
1752 n = preds->length ();
1753 if (n < 2)
1754 return;
1758 changed = false;
1759 if (simplify_preds_2 (preds))
1760 changed = true;
1762 /* Now iteratively simplify X OR (!X AND Z ..)
1763 into X OR (Z ...). */
1764 if (simplify_preds_3 (preds))
1765 changed = true;
1767 if (simplify_preds_4 (preds))
1768 changed = true;
1770 } while (changed);
1772 return;
1775 /* This is a helper function which attempts to normalize predicate chains
1776 by following UD chains. It basically builds up a big tree of either IOR
1777 operations or AND operations, and convert the IOR tree into a
1778 pred_chain_union or BIT_AND tree into a pred_chain.
1779 Example:
1781 _3 = _2 RELOP1 _1;
1782 _6 = _5 RELOP2 _4;
1783 _9 = _8 RELOP3 _7;
1784 _10 = _3 | _6;
1785 _12 = _9 | _0;
1786 _t = _10 | _12;
1788 then _t != 0 will be normalized into a pred_chain_union
1790 (_2 RELOP1 _1) OR (_5 RELOP2 _4) OR (_8 RELOP3 _7) OR (_0 != 0)
1792 Similarly given,
1794 _3 = _2 RELOP1 _1;
1795 _6 = _5 RELOP2 _4;
1796 _9 = _8 RELOP3 _7;
1797 _10 = _3 & _6;
1798 _12 = _9 & _0;
1800 then _t != 0 will be normalized into a pred_chain:
1801 (_2 RELOP1 _1) AND (_5 RELOP2 _4) AND (_8 RELOP3 _7) AND (_0 != 0)
1805 /* This is a helper function that stores a PRED into NORM_PREDS. */
1807 inline static void
1808 push_pred (pred_chain_union *norm_preds, pred_info pred)
1810 pred_chain pred_chain = vNULL;
1811 pred_chain.safe_push (pred);
1812 norm_preds->safe_push (pred_chain);
1815 /* A helper function that creates a predicate of the form
1816 OP != 0 and push it WORK_LIST. */
1818 inline static void
1819 push_to_worklist (tree op, vec<pred_info, va_heap, vl_ptr> *work_list,
1820 hash_set<tree> *mark_set)
1822 if (mark_set->contains (op))
1823 return;
1824 mark_set->add (op);
1826 pred_info arg_pred;
1827 arg_pred.pred_lhs = op;
1828 arg_pred.pred_rhs = integer_zero_node;
1829 arg_pred.cond_code = NE_EXPR;
1830 arg_pred.invert = false;
1831 work_list->safe_push (arg_pred);
1834 /* A helper that generates a pred_info from a gimple assignment
1835 CMP_ASSIGN with comparison rhs. */
1837 static pred_info
1838 get_pred_info_from_cmp (gimple cmp_assign)
1840 pred_info n_pred;
1841 n_pred.pred_lhs = gimple_assign_rhs1 (cmp_assign);
1842 n_pred.pred_rhs = gimple_assign_rhs2 (cmp_assign);
1843 n_pred.cond_code = gimple_assign_rhs_code (cmp_assign);
1844 n_pred.invert = false;
1845 return n_pred;
1848 /* Returns true if the PHI is a degenerated phi with
1849 all args with the same value (relop). In that case, *PRED
1850 will be updated to that value. */
1852 static bool
1853 is_degenerated_phi (gimple phi, pred_info *pred_p)
1855 int i, n;
1856 tree op0;
1857 gimple def0;
1858 pred_info pred0;
1860 n = gimple_phi_num_args (phi);
1861 op0 = gimple_phi_arg_def (phi, 0);
1863 if (TREE_CODE (op0) != SSA_NAME)
1864 return false;
1866 def0 = SSA_NAME_DEF_STMT (op0);
1867 if (gimple_code (def0) != GIMPLE_ASSIGN)
1868 return false;
1869 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def0))
1870 != tcc_comparison)
1871 return false;
1872 pred0 = get_pred_info_from_cmp (def0);
1874 for (i = 1; i < n; ++i)
1876 gimple def;
1877 pred_info pred;
1878 tree op = gimple_phi_arg_def (phi, i);
1880 if (TREE_CODE (op) != SSA_NAME)
1881 return false;
1883 def = SSA_NAME_DEF_STMT (op);
1884 if (gimple_code (def) != GIMPLE_ASSIGN)
1885 return false;
1886 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def))
1887 != tcc_comparison)
1888 return false;
1889 pred = get_pred_info_from_cmp (def);
1890 if (!pred_equal_p (pred, pred0))
1891 return false;
1894 *pred_p = pred0;
1895 return true;
1898 /* Normalize one predicate PRED
1899 1) if PRED can no longer be normlized, put it into NORM_PREDS.
1900 2) otherwise if PRED is of the form x != 0, follow x's definition
1901 and put normalized predicates into WORK_LIST. */
1903 static void
1904 normalize_one_pred_1 (pred_chain_union *norm_preds,
1905 pred_chain *norm_chain,
1906 pred_info pred,
1907 enum tree_code and_or_code,
1908 vec<pred_info, va_heap, vl_ptr> *work_list,
1909 hash_set<tree> *mark_set)
1911 if (!is_neq_zero_form_p (pred))
1913 if (and_or_code == BIT_IOR_EXPR)
1914 push_pred (norm_preds, pred);
1915 else
1916 norm_chain->safe_push (pred);
1917 return;
1920 gimple def_stmt = SSA_NAME_DEF_STMT (pred.pred_lhs);
1922 if (gimple_code (def_stmt) == GIMPLE_PHI
1923 && is_degenerated_phi (def_stmt, &pred))
1924 work_list->safe_push (pred);
1925 else if (gimple_code (def_stmt) == GIMPLE_PHI
1926 && and_or_code == BIT_IOR_EXPR)
1928 int i, n;
1929 n = gimple_phi_num_args (def_stmt);
1931 /* If we see non zero constant, we should punt. The predicate
1932 * should be one guarding the phi edge. */
1933 for (i = 0; i < n; ++i)
1935 tree op = gimple_phi_arg_def (def_stmt, i);
1936 if (TREE_CODE (op) == INTEGER_CST && !integer_zerop (op))
1938 push_pred (norm_preds, pred);
1939 return;
1943 for (i = 0; i < n; ++i)
1945 tree op = gimple_phi_arg_def (def_stmt, i);
1946 if (integer_zerop (op))
1947 continue;
1949 push_to_worklist (op, work_list, mark_set);
1952 else if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1954 if (and_or_code == BIT_IOR_EXPR)
1955 push_pred (norm_preds, pred);
1956 else
1957 norm_chain->safe_push (pred);
1959 else if (gimple_assign_rhs_code (def_stmt) == and_or_code)
1961 push_to_worklist (gimple_assign_rhs1 (def_stmt), work_list, mark_set);
1962 push_to_worklist (gimple_assign_rhs2 (def_stmt), work_list, mark_set);
1964 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt))
1965 == tcc_comparison)
1967 pred_info n_pred = get_pred_info_from_cmp (def_stmt);
1968 if (and_or_code == BIT_IOR_EXPR)
1969 push_pred (norm_preds, n_pred);
1970 else
1971 norm_chain->safe_push (n_pred);
1973 else
1975 if (and_or_code == BIT_IOR_EXPR)
1976 push_pred (norm_preds, pred);
1977 else
1978 norm_chain->safe_push (pred);
1982 /* Normalize PRED and store the normalized predicates into NORM_PREDS. */
1984 static void
1985 normalize_one_pred (pred_chain_union *norm_preds,
1986 pred_info pred)
1988 vec<pred_info, va_heap, vl_ptr> work_list = vNULL;
1989 enum tree_code and_or_code = ERROR_MARK;
1990 pred_chain norm_chain = vNULL;
1992 if (!is_neq_zero_form_p (pred))
1994 push_pred (norm_preds, pred);
1995 return;
1998 gimple def_stmt = SSA_NAME_DEF_STMT (pred.pred_lhs);
1999 if (gimple_code (def_stmt) == GIMPLE_ASSIGN)
2000 and_or_code = gimple_assign_rhs_code (def_stmt);
2001 if (and_or_code != BIT_IOR_EXPR
2002 && and_or_code != BIT_AND_EXPR)
2004 if (TREE_CODE_CLASS (and_or_code)
2005 == tcc_comparison)
2007 pred_info n_pred = get_pred_info_from_cmp (def_stmt);
2008 push_pred (norm_preds, n_pred);
2010 else
2011 push_pred (norm_preds, pred);
2012 return;
2015 work_list.safe_push (pred);
2016 hash_set<tree> mark_set;
2018 while (!work_list.is_empty ())
2020 pred_info a_pred = work_list.pop ();
2021 normalize_one_pred_1 (norm_preds, &norm_chain, a_pred,
2022 and_or_code, &work_list, &mark_set);
2024 if (and_or_code == BIT_AND_EXPR)
2025 norm_preds->safe_push (norm_chain);
2027 work_list.release ();
2030 static void
2031 normalize_one_pred_chain (pred_chain_union *norm_preds,
2032 pred_chain one_chain)
2034 vec<pred_info, va_heap, vl_ptr> work_list = vNULL;
2035 hash_set<tree> mark_set;
2036 pred_chain norm_chain = vNULL;
2037 size_t i;
2039 for (i = 0; i < one_chain.length (); i++)
2041 work_list.safe_push (one_chain[i]);
2042 mark_set.add (one_chain[i].pred_lhs);
2045 while (!work_list.is_empty ())
2047 pred_info a_pred = work_list.pop ();
2048 normalize_one_pred_1 (0, &norm_chain, a_pred,
2049 BIT_AND_EXPR, &work_list, &mark_set);
2052 norm_preds->safe_push (norm_chain);
2053 work_list.release ();
2056 /* Normalize predicate chains PREDS and returns the normalized one. */
2058 static pred_chain_union
2059 normalize_preds (pred_chain_union preds, gimple use_or_def, bool is_use)
2061 pred_chain_union norm_preds = vNULL;
2062 size_t n = preds.length ();
2063 size_t i;
2065 if (dump_file && dump_flags & TDF_DETAILS)
2067 fprintf (dump_file, "[BEFORE NORMALIZATION --");
2068 dump_predicates (use_or_def, preds, is_use ? "[USE]:\n" : "[DEF]:\n");
2071 for (i = 0; i < n; i++)
2073 if (preds[i].length () != 1)
2074 normalize_one_pred_chain (&norm_preds, preds[i]);
2075 else
2077 normalize_one_pred (&norm_preds, preds[i][0]);
2078 preds[i].release ();
2082 if (dump_file)
2084 fprintf (dump_file, "[AFTER NORMALIZATION -- ");
2085 dump_predicates (use_or_def, norm_preds, is_use ? "[USE]:\n" : "[DEF]:\n");
2088 preds.release ();
2089 return norm_preds;
2093 /* Computes the predicates that guard the use and checks
2094 if the incoming paths that have empty (or possibly
2095 empty) definition can be pruned/filtered. The function returns
2096 true if it can be determined that the use of PHI's def in
2097 USE_STMT is guarded with a predicate set not overlapping with
2098 predicate sets of all runtime paths that do not have a definition.
2099 Returns false if it is not or it can not be determined. USE_BB is
2100 the bb of the use (for phi operand use, the bb is not the bb of
2101 the phi stmt, but the src bb of the operand edge). UNINIT_OPNDS
2102 is a bit vector. If an operand of PHI is uninitialized, the
2103 corresponding bit in the vector is 1. VISIED_PHIS is a pointer
2104 set of phis being visted. */
2106 static bool
2107 is_use_properly_guarded (gimple use_stmt,
2108 basic_block use_bb,
2109 gimple phi,
2110 unsigned uninit_opnds,
2111 hash_set<gimple> *visited_phis)
2113 basic_block phi_bb;
2114 pred_chain_union preds = vNULL;
2115 pred_chain_union def_preds = vNULL;
2116 bool has_valid_preds = false;
2117 bool is_properly_guarded = false;
2119 if (visited_phis->add (phi))
2120 return false;
2122 phi_bb = gimple_bb (phi);
2124 if (is_non_loop_exit_postdominating (use_bb, phi_bb))
2125 return false;
2127 has_valid_preds = find_predicates (&preds, phi_bb, use_bb);
2129 if (!has_valid_preds)
2131 destroy_predicate_vecs (preds);
2132 return false;
2135 /* Try to prune the dead incoming phi edges. */
2136 is_properly_guarded
2137 = use_pred_not_overlap_with_undef_path_pred (preds, phi, uninit_opnds,
2138 visited_phis);
2140 if (is_properly_guarded)
2142 destroy_predicate_vecs (preds);
2143 return true;
2146 has_valid_preds = find_def_preds (&def_preds, phi);
2148 if (!has_valid_preds)
2150 destroy_predicate_vecs (preds);
2151 destroy_predicate_vecs (def_preds);
2152 return false;
2155 simplify_preds (&preds, use_stmt, true);
2156 preds = normalize_preds (preds, use_stmt, true);
2158 simplify_preds (&def_preds, phi, false);
2159 def_preds = normalize_preds (def_preds, phi, false);
2161 is_properly_guarded = is_superset_of (def_preds, preds);
2163 destroy_predicate_vecs (preds);
2164 destroy_predicate_vecs (def_preds);
2165 return is_properly_guarded;
2168 /* Searches through all uses of a potentially
2169 uninitialized variable defined by PHI and returns a use
2170 statement if the use is not properly guarded. It returns
2171 NULL if all uses are guarded. UNINIT_OPNDS is a bitvector
2172 holding the position(s) of uninit PHI operands. WORKLIST
2173 is the vector of candidate phis that may be updated by this
2174 function. ADDED_TO_WORKLIST is the pointer set tracking
2175 if the new phi is already in the worklist. */
2177 static gimple
2178 find_uninit_use (gimple phi, unsigned uninit_opnds,
2179 vec<gimple> *worklist,
2180 hash_set<gimple> *added_to_worklist)
2182 tree phi_result;
2183 use_operand_p use_p;
2184 gimple use_stmt;
2185 imm_use_iterator iter;
2187 phi_result = gimple_phi_result (phi);
2189 FOR_EACH_IMM_USE_FAST (use_p, iter, phi_result)
2191 basic_block use_bb;
2193 use_stmt = USE_STMT (use_p);
2194 if (is_gimple_debug (use_stmt))
2195 continue;
2197 if (gimple_code (use_stmt) == GIMPLE_PHI)
2198 use_bb = gimple_phi_arg_edge (use_stmt,
2199 PHI_ARG_INDEX_FROM_USE (use_p))->src;
2200 else
2201 use_bb = gimple_bb (use_stmt);
2203 hash_set<gimple> visited_phis;
2204 if (is_use_properly_guarded (use_stmt, use_bb, phi, uninit_opnds,
2205 &visited_phis))
2206 continue;
2208 if (dump_file && (dump_flags & TDF_DETAILS))
2210 fprintf (dump_file, "[CHECK]: Found unguarded use: ");
2211 print_gimple_stmt (dump_file, use_stmt, 0, 0);
2213 /* Found one real use, return. */
2214 if (gimple_code (use_stmt) != GIMPLE_PHI)
2215 return use_stmt;
2217 /* Found a phi use that is not guarded,
2218 add the phi to the worklist. */
2219 if (!added_to_worklist->add (use_stmt))
2221 if (dump_file && (dump_flags & TDF_DETAILS))
2223 fprintf (dump_file, "[WORKLIST]: Update worklist with phi: ");
2224 print_gimple_stmt (dump_file, use_stmt, 0, 0);
2227 worklist->safe_push (use_stmt);
2228 possibly_undefined_names->add (phi_result);
2232 return NULL;
2235 /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions
2236 and gives warning if there exists a runtime path from the entry to a
2237 use of the PHI def that does not contain a definition. In other words,
2238 the warning is on the real use. The more dead paths that can be pruned
2239 by the compiler, the fewer false positives the warning is. WORKLIST
2240 is a vector of candidate phis to be examined. ADDED_TO_WORKLIST is
2241 a pointer set tracking if the new phi is added to the worklist or not. */
2243 static void
2244 warn_uninitialized_phi (gimple phi, vec<gimple> *worklist,
2245 hash_set<gimple> *added_to_worklist)
2247 unsigned uninit_opnds;
2248 gimple uninit_use_stmt = 0;
2249 tree uninit_op;
2250 int phiarg_index;
2251 location_t loc;
2253 /* Don't look at virtual operands. */
2254 if (virtual_operand_p (gimple_phi_result (phi)))
2255 return;
2257 uninit_opnds = compute_uninit_opnds_pos (phi);
2259 if (MASK_EMPTY (uninit_opnds))
2260 return;
2262 if (dump_file && (dump_flags & TDF_DETAILS))
2264 fprintf (dump_file, "[CHECK]: examining phi: ");
2265 print_gimple_stmt (dump_file, phi, 0, 0);
2268 /* Now check if we have any use of the value without proper guard. */
2269 uninit_use_stmt = find_uninit_use (phi, uninit_opnds,
2270 worklist, added_to_worklist);
2272 /* All uses are properly guarded. */
2273 if (!uninit_use_stmt)
2274 return;
2276 phiarg_index = MASK_FIRST_SET_BIT (uninit_opnds);
2277 uninit_op = gimple_phi_arg_def (phi, phiarg_index);
2278 if (SSA_NAME_VAR (uninit_op) == NULL_TREE)
2279 return;
2280 if (gimple_phi_arg_has_location (phi, phiarg_index))
2281 loc = gimple_phi_arg_location (phi, phiarg_index);
2282 else
2283 loc = UNKNOWN_LOCATION;
2284 warn_uninit (OPT_Wmaybe_uninitialized, uninit_op, SSA_NAME_VAR (uninit_op),
2285 SSA_NAME_VAR (uninit_op),
2286 "%qD may be used uninitialized in this function",
2287 uninit_use_stmt, loc);
2291 static bool
2292 gate_warn_uninitialized (void)
2294 return warn_uninitialized || warn_maybe_uninitialized;
2297 namespace {
2299 const pass_data pass_data_late_warn_uninitialized =
2301 GIMPLE_PASS, /* type */
2302 "uninit", /* name */
2303 OPTGROUP_NONE, /* optinfo_flags */
2304 TV_NONE, /* tv_id */
2305 PROP_ssa, /* properties_required */
2306 0, /* properties_provided */
2307 0, /* properties_destroyed */
2308 0, /* todo_flags_start */
2309 0, /* todo_flags_finish */
2312 class pass_late_warn_uninitialized : public gimple_opt_pass
2314 public:
2315 pass_late_warn_uninitialized (gcc::context *ctxt)
2316 : gimple_opt_pass (pass_data_late_warn_uninitialized, ctxt)
2319 /* opt_pass methods: */
2320 opt_pass * clone () { return new pass_late_warn_uninitialized (m_ctxt); }
2321 virtual bool gate (function *) { return gate_warn_uninitialized (); }
2322 virtual unsigned int execute (function *);
2324 }; // class pass_late_warn_uninitialized
2326 unsigned int
2327 pass_late_warn_uninitialized::execute (function *fun)
2329 basic_block bb;
2330 gimple_stmt_iterator gsi;
2331 vec<gimple> worklist = vNULL;
2333 calculate_dominance_info (CDI_DOMINATORS);
2334 calculate_dominance_info (CDI_POST_DOMINATORS);
2335 /* Re-do the plain uninitialized variable check, as optimization may have
2336 straightened control flow. Do this first so that we don't accidentally
2337 get a "may be" warning when we'd have seen an "is" warning later. */
2338 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/1);
2340 timevar_push (TV_TREE_UNINIT);
2342 possibly_undefined_names = new hash_set<tree>;
2343 hash_set<gimple> added_to_worklist;
2345 /* Initialize worklist */
2346 FOR_EACH_BB_FN (bb, fun)
2347 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2349 gimple phi = gsi_stmt (gsi);
2350 size_t n, i;
2352 n = gimple_phi_num_args (phi);
2354 /* Don't look at virtual operands. */
2355 if (virtual_operand_p (gimple_phi_result (phi)))
2356 continue;
2358 for (i = 0; i < n; ++i)
2360 tree op = gimple_phi_arg_def (phi, i);
2361 if (TREE_CODE (op) == SSA_NAME
2362 && uninit_undefined_value_p (op))
2364 worklist.safe_push (phi);
2365 added_to_worklist.add (phi);
2366 if (dump_file && (dump_flags & TDF_DETAILS))
2368 fprintf (dump_file, "[WORKLIST]: add to initial list: ");
2369 print_gimple_stmt (dump_file, phi, 0, 0);
2371 break;
2376 while (worklist.length () != 0)
2378 gimple cur_phi = 0;
2379 cur_phi = worklist.pop ();
2380 warn_uninitialized_phi (cur_phi, &worklist, &added_to_worklist);
2383 worklist.release ();
2384 delete possibly_undefined_names;
2385 possibly_undefined_names = NULL;
2386 free_dominance_info (CDI_POST_DOMINATORS);
2387 timevar_pop (TV_TREE_UNINIT);
2388 return 0;
2391 } // anon namespace
2393 gimple_opt_pass *
2394 make_pass_late_warn_uninitialized (gcc::context *ctxt)
2396 return new pass_late_warn_uninitialized (ctxt);
2400 static unsigned int
2401 execute_early_warn_uninitialized (void)
2403 /* Currently, this pass runs always but
2404 execute_late_warn_uninitialized only runs with optimization. With
2405 optimization we want to warn about possible uninitialized as late
2406 as possible, thus don't do it here. However, without
2407 optimization we need to warn here about "may be uninitialized". */
2408 calculate_dominance_info (CDI_POST_DOMINATORS);
2410 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/!optimize);
2412 /* Post-dominator information can not be reliably updated. Free it
2413 after the use. */
2415 free_dominance_info (CDI_POST_DOMINATORS);
2416 return 0;
2420 namespace {
2422 const pass_data pass_data_early_warn_uninitialized =
2424 GIMPLE_PASS, /* type */
2425 "*early_warn_uninitialized", /* name */
2426 OPTGROUP_NONE, /* optinfo_flags */
2427 TV_TREE_UNINIT, /* tv_id */
2428 PROP_ssa, /* properties_required */
2429 0, /* properties_provided */
2430 0, /* properties_destroyed */
2431 0, /* todo_flags_start */
2432 0, /* todo_flags_finish */
2435 class pass_early_warn_uninitialized : public gimple_opt_pass
2437 public:
2438 pass_early_warn_uninitialized (gcc::context *ctxt)
2439 : gimple_opt_pass (pass_data_early_warn_uninitialized, ctxt)
2442 /* opt_pass methods: */
2443 virtual bool gate (function *) { return gate_warn_uninitialized (); }
2444 virtual unsigned int execute (function *)
2446 return execute_early_warn_uninitialized ();
2449 }; // class pass_early_warn_uninitialized
2451 } // anon namespace
2453 gimple_opt_pass *
2454 make_pass_early_warn_uninitialized (gcc::context *ctxt)
2456 return new pass_early_warn_uninitialized (ctxt);