trans-decl.c (create_function_arglist): Add hidden coarray
[official-gcc.git] / gcc / tree-ssa-uninit.c
blobae251ccd6ba8371c7a65326b11ce3431f35a8fb8
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 "pointer-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 pointer_set_t *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 && pointer_set_contains (possibly_undefined_names, 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 LOCUS may contain a location or be null.
127 WC is the warning code. */
129 static void
130 warn_uninit (enum opt_code wc, tree t,
131 tree expr, tree var, const char *gmsgid, void *data)
133 gimple context = (gimple) data;
134 location_t location, cfun_loc;
135 expanded_location xloc, floc;
137 if (!has_undefined_value_p (t))
138 return;
140 /* TREE_NO_WARNING either means we already warned, or the front end
141 wishes to suppress the warning. */
142 if ((context
143 && (gimple_no_warning_p (context)
144 || (gimple_assign_single_p (context)
145 && TREE_NO_WARNING (gimple_assign_rhs1 (context)))))
146 || TREE_NO_WARNING (expr))
147 return;
149 location = (context != NULL && gimple_has_location (context))
150 ? gimple_location (context)
151 : DECL_SOURCE_LOCATION (var);
152 location = linemap_resolve_location (line_table, location,
153 LRK_SPELLING_LOCATION,
154 NULL);
155 cfun_loc = DECL_SOURCE_LOCATION (cfun->decl);
156 xloc = expand_location (location);
157 floc = expand_location (cfun_loc);
158 if (warning_at (location, wc, gmsgid, expr))
160 TREE_NO_WARNING (expr) = 1;
162 if (location == DECL_SOURCE_LOCATION (var))
163 return;
164 if (xloc.file != floc.file
165 || linemap_location_before_p (line_table,
166 location, cfun_loc)
167 || linemap_location_before_p (line_table,
168 cfun->function_end_locus,
169 location))
170 inform (DECL_SOURCE_LOCATION (var), "%qD was declared here", var);
174 static unsigned int
175 warn_uninitialized_vars (bool warn_possibly_uninitialized)
177 gimple_stmt_iterator gsi;
178 basic_block bb;
180 FOR_EACH_BB_FN (bb, cfun)
182 bool always_executed = dominated_by_p (CDI_POST_DOMINATORS,
183 single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)), bb);
184 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
186 gimple stmt = gsi_stmt (gsi);
187 use_operand_p use_p;
188 ssa_op_iter op_iter;
189 tree use;
191 if (is_gimple_debug (stmt))
192 continue;
194 /* We only do data flow with SSA_NAMEs, so that's all we
195 can warn about. */
196 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, op_iter, SSA_OP_USE)
198 use = USE_FROM_PTR (use_p);
199 if (always_executed)
200 warn_uninit (OPT_Wuninitialized, use,
201 SSA_NAME_VAR (use), SSA_NAME_VAR (use),
202 "%qD is used uninitialized in this function",
203 stmt);
204 else if (warn_possibly_uninitialized)
205 warn_uninit (OPT_Wmaybe_uninitialized, use,
206 SSA_NAME_VAR (use), SSA_NAME_VAR (use),
207 "%qD may be used uninitialized in this function",
208 stmt);
211 /* For memory the only cheap thing we can do is see if we
212 have a use of the default def of the virtual operand.
213 ??? Not so cheap would be to use the alias oracle via
214 walk_aliased_vdefs, if we don't find any aliasing vdef
215 warn as is-used-uninitialized, if we don't find an aliasing
216 vdef that kills our use (stmt_kills_ref_p), warn as
217 may-be-used-uninitialized. But this walk is quadratic and
218 so must be limited which means we would miss warning
219 opportunities. */
220 use = gimple_vuse (stmt);
221 if (use
222 && gimple_assign_single_p (stmt)
223 && !gimple_vdef (stmt)
224 && SSA_NAME_IS_DEFAULT_DEF (use))
226 tree rhs = gimple_assign_rhs1 (stmt);
227 tree base = get_base_address (rhs);
229 /* Do not warn if it can be initialized outside this function. */
230 if (TREE_CODE (base) != VAR_DECL
231 || DECL_HARD_REGISTER (base)
232 || is_global_var (base))
233 continue;
235 if (always_executed)
236 warn_uninit (OPT_Wuninitialized, use,
237 gimple_assign_rhs1 (stmt), base,
238 "%qE is used uninitialized in this function",
239 stmt);
240 else if (warn_possibly_uninitialized)
241 warn_uninit (OPT_Wmaybe_uninitialized, use,
242 gimple_assign_rhs1 (stmt), base,
243 "%qE may be used uninitialized in this function",
244 stmt);
249 return 0;
252 /* Checks if the operand OPND of PHI is defined by
253 another phi with one operand defined by this PHI,
254 but the rest operands are all defined. If yes,
255 returns true to skip this this operand as being
256 redundant. Can be enhanced to be more general. */
258 static bool
259 can_skip_redundant_opnd (tree opnd, gimple phi)
261 gimple op_def;
262 tree phi_def;
263 int i, n;
265 phi_def = gimple_phi_result (phi);
266 op_def = SSA_NAME_DEF_STMT (opnd);
267 if (gimple_code (op_def) != GIMPLE_PHI)
268 return false;
269 n = gimple_phi_num_args (op_def);
270 for (i = 0; i < n; ++i)
272 tree op = gimple_phi_arg_def (op_def, i);
273 if (TREE_CODE (op) != SSA_NAME)
274 continue;
275 if (op != phi_def && uninit_undefined_value_p (op))
276 return false;
279 return true;
282 /* Returns a bit mask holding the positions of arguments in PHI
283 that have empty (or possibly empty) definitions. */
285 static unsigned
286 compute_uninit_opnds_pos (gimple phi)
288 size_t i, n;
289 unsigned uninit_opnds = 0;
291 n = gimple_phi_num_args (phi);
292 /* Bail out for phi with too many args. */
293 if (n > 32)
294 return 0;
296 for (i = 0; i < n; ++i)
298 tree op = gimple_phi_arg_def (phi, i);
299 if (TREE_CODE (op) == SSA_NAME
300 && uninit_undefined_value_p (op)
301 && !can_skip_redundant_opnd (op, phi))
303 if (cfun->has_nonlocal_label || cfun->calls_setjmp)
305 /* Ignore SSA_NAMEs that appear on abnormal edges
306 somewhere. */
307 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
308 continue;
310 MASK_SET_BIT (uninit_opnds, i);
313 return uninit_opnds;
316 /* Find the immediate postdominator PDOM of the specified
317 basic block BLOCK. */
319 static inline basic_block
320 find_pdom (basic_block block)
322 if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
323 return EXIT_BLOCK_PTR_FOR_FN (cfun);
324 else
326 basic_block bb
327 = get_immediate_dominator (CDI_POST_DOMINATORS, block);
328 if (! bb)
329 return EXIT_BLOCK_PTR_FOR_FN (cfun);
330 return bb;
334 /* Find the immediate DOM of the specified
335 basic block BLOCK. */
337 static inline basic_block
338 find_dom (basic_block block)
340 if (block == ENTRY_BLOCK_PTR_FOR_FN (cfun))
341 return ENTRY_BLOCK_PTR_FOR_FN (cfun);
342 else
344 basic_block bb = get_immediate_dominator (CDI_DOMINATORS, block);
345 if (! bb)
346 return ENTRY_BLOCK_PTR_FOR_FN (cfun);
347 return bb;
351 /* Returns true if BB1 is postdominating BB2 and BB1 is
352 not a loop exit bb. The loop exit bb check is simple and does
353 not cover all cases. */
355 static bool
356 is_non_loop_exit_postdominating (basic_block bb1, basic_block bb2)
358 if (!dominated_by_p (CDI_POST_DOMINATORS, bb2, bb1))
359 return false;
361 if (single_pred_p (bb1) && !single_succ_p (bb2))
362 return false;
364 return true;
367 /* Find the closest postdominator of a specified BB, which is control
368 equivalent to BB. */
370 static inline basic_block
371 find_control_equiv_block (basic_block bb)
373 basic_block pdom;
375 pdom = find_pdom (bb);
377 /* Skip the postdominating bb that is also loop exit. */
378 if (!is_non_loop_exit_postdominating (pdom, bb))
379 return NULL;
381 if (dominated_by_p (CDI_DOMINATORS, pdom, bb))
382 return pdom;
384 return NULL;
387 #define MAX_NUM_CHAINS 8
388 #define MAX_CHAIN_LEN 5
389 #define MAX_POSTDOM_CHECK 8
391 /* Computes the control dependence chains (paths of edges)
392 for DEP_BB up to the dominating basic block BB (the head node of a
393 chain should be dominated by it). CD_CHAINS is pointer to an
394 array holding the result chains. CUR_CD_CHAIN is the current
395 chain being computed. *NUM_CHAINS is total number of chains. The
396 function returns true if the information is successfully computed,
397 return false if there is no control dependence or not computed. */
399 static bool
400 compute_control_dep_chain (basic_block bb, basic_block dep_bb,
401 vec<edge> *cd_chains,
402 size_t *num_chains,
403 vec<edge> *cur_cd_chain,
404 int *num_calls)
406 edge_iterator ei;
407 edge e;
408 size_t i;
409 bool found_cd_chain = false;
410 size_t cur_chain_len = 0;
412 if (EDGE_COUNT (bb->succs) < 2)
413 return false;
415 if (*num_calls > PARAM_VALUE (PARAM_UNINIT_CONTROL_DEP_ATTEMPTS))
416 return false;
417 ++*num_calls;
419 /* Could use a set instead. */
420 cur_chain_len = cur_cd_chain->length ();
421 if (cur_chain_len > MAX_CHAIN_LEN)
422 return false;
424 for (i = 0; i < cur_chain_len; i++)
426 edge e = (*cur_cd_chain)[i];
427 /* Cycle detected. */
428 if (e->src == bb)
429 return false;
432 FOR_EACH_EDGE (e, ei, bb->succs)
434 basic_block cd_bb;
435 int post_dom_check = 0;
436 if (e->flags & (EDGE_FAKE | EDGE_ABNORMAL))
437 continue;
439 cd_bb = e->dest;
440 cur_cd_chain->safe_push (e);
441 while (!is_non_loop_exit_postdominating (cd_bb, bb))
443 if (cd_bb == dep_bb)
445 /* Found a direct control dependence. */
446 if (*num_chains < MAX_NUM_CHAINS)
448 cd_chains[*num_chains] = cur_cd_chain->copy ();
449 (*num_chains)++;
451 found_cd_chain = true;
452 /* Check path from next edge. */
453 break;
456 /* Now check if DEP_BB is indirectly control dependent on BB. */
457 if (compute_control_dep_chain (cd_bb, dep_bb, cd_chains,
458 num_chains, cur_cd_chain, num_calls))
460 found_cd_chain = true;
461 break;
464 cd_bb = find_pdom (cd_bb);
465 post_dom_check++;
466 if (cd_bb == EXIT_BLOCK_PTR_FOR_FN (cfun) || post_dom_check >
467 MAX_POSTDOM_CHECK)
468 break;
470 cur_cd_chain->pop ();
471 gcc_assert (cur_cd_chain->length () == cur_chain_len);
473 gcc_assert (cur_cd_chain->length () == cur_chain_len);
475 return found_cd_chain;
478 /* The type to represent a simple predicate */
480 typedef struct use_def_pred_info
482 tree pred_lhs;
483 tree pred_rhs;
484 enum tree_code cond_code;
485 bool invert;
486 } pred_info;
488 /* The type to represent a sequence of predicates grouped
489 with .AND. operation. */
491 typedef vec<pred_info, va_heap, vl_ptr> pred_chain;
493 /* The type to represent a sequence of pred_chains grouped
494 with .OR. operation. */
496 typedef vec<pred_chain, va_heap, vl_ptr> pred_chain_union;
498 /* Converts the chains of control dependence edges into a set of
499 predicates. A control dependence chain is represented by a vector
500 edges. DEP_CHAINS points to an array of dependence chains.
501 NUM_CHAINS is the size of the chain array. One edge in a dependence
502 chain is mapped to predicate expression represented by pred_info
503 type. One dependence chain is converted to a composite predicate that
504 is the result of AND operation of pred_info mapped to each edge.
505 A composite predicate is presented by a vector of pred_info. On
506 return, *PREDS points to the resulting array of composite predicates.
507 *NUM_PREDS is the number of composite predictes. */
509 static bool
510 convert_control_dep_chain_into_preds (vec<edge> *dep_chains,
511 size_t num_chains,
512 pred_chain_union *preds)
514 bool has_valid_pred = false;
515 size_t i, j;
516 if (num_chains == 0 || num_chains >= MAX_NUM_CHAINS)
517 return false;
519 /* Now convert the control dep chain into a set
520 of predicates. */
521 preds->reserve (num_chains);
523 for (i = 0; i < num_chains; i++)
525 vec<edge> one_cd_chain = dep_chains[i];
527 has_valid_pred = false;
528 pred_chain t_chain = vNULL;
529 for (j = 0; j < one_cd_chain.length (); j++)
531 gimple cond_stmt;
532 gimple_stmt_iterator gsi;
533 basic_block guard_bb;
534 pred_info one_pred;
535 edge e;
537 e = one_cd_chain[j];
538 guard_bb = e->src;
539 gsi = gsi_last_bb (guard_bb);
540 if (gsi_end_p (gsi))
542 has_valid_pred = false;
543 break;
545 cond_stmt = gsi_stmt (gsi);
546 if (is_gimple_call (cond_stmt)
547 && EDGE_COUNT (e->src->succs) >= 2)
549 /* Ignore EH edge. Can add assertion
550 on the other edge's flag. */
551 continue;
553 /* Skip if there is essentially one succesor. */
554 if (EDGE_COUNT (e->src->succs) == 2)
556 edge e1;
557 edge_iterator ei1;
558 bool skip = false;
560 FOR_EACH_EDGE (e1, ei1, e->src->succs)
562 if (EDGE_COUNT (e1->dest->succs) == 0)
564 skip = true;
565 break;
568 if (skip)
569 continue;
571 if (gimple_code (cond_stmt) != GIMPLE_COND)
573 has_valid_pred = false;
574 break;
576 one_pred.pred_lhs = gimple_cond_lhs (cond_stmt);
577 one_pred.pred_rhs = gimple_cond_rhs (cond_stmt);
578 one_pred.cond_code = gimple_cond_code (cond_stmt);
579 one_pred.invert = !!(e->flags & EDGE_FALSE_VALUE);
580 t_chain.safe_push (one_pred);
581 has_valid_pred = true;
584 if (!has_valid_pred)
585 break;
586 else
587 preds->safe_push (t_chain);
589 return has_valid_pred;
592 /* Computes all control dependence chains for USE_BB. The control
593 dependence chains are then converted to an array of composite
594 predicates pointed to by PREDS. PHI_BB is the basic block of
595 the phi whose result is used in USE_BB. */
597 static bool
598 find_predicates (pred_chain_union *preds,
599 basic_block phi_bb,
600 basic_block use_bb)
602 size_t num_chains = 0, i;
603 int num_calls = 0;
604 vec<edge> dep_chains[MAX_NUM_CHAINS];
605 auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
606 bool has_valid_pred = false;
607 basic_block cd_root = 0;
609 /* First find the closest bb that is control equivalent to PHI_BB
610 that also dominates USE_BB. */
611 cd_root = phi_bb;
612 while (dominated_by_p (CDI_DOMINATORS, use_bb, cd_root))
614 basic_block ctrl_eq_bb = find_control_equiv_block (cd_root);
615 if (ctrl_eq_bb && dominated_by_p (CDI_DOMINATORS, use_bb, ctrl_eq_bb))
616 cd_root = ctrl_eq_bb;
617 else
618 break;
621 compute_control_dep_chain (cd_root, use_bb, dep_chains, &num_chains,
622 &cur_chain, &num_calls);
624 has_valid_pred
625 = convert_control_dep_chain_into_preds (dep_chains, num_chains, preds);
626 for (i = 0; i < num_chains; i++)
627 dep_chains[i].release ();
628 return has_valid_pred;
631 /* Computes the set of incoming edges of PHI that have non empty
632 definitions of a phi chain. The collection will be done
633 recursively on operands that are defined by phis. CD_ROOT
634 is the control dependence root. *EDGES holds the result, and
635 VISITED_PHIS is a pointer set for detecting cycles. */
637 static void
638 collect_phi_def_edges (gimple phi, basic_block cd_root,
639 vec<edge> *edges,
640 pointer_set_t *visited_phis)
642 size_t i, n;
643 edge opnd_edge;
644 tree opnd;
646 if (pointer_set_insert (visited_phis, phi))
647 return;
649 n = gimple_phi_num_args (phi);
650 for (i = 0; i < n; i++)
652 opnd_edge = gimple_phi_arg_edge (phi, i);
653 opnd = gimple_phi_arg_def (phi, i);
655 if (TREE_CODE (opnd) != SSA_NAME)
657 if (dump_file && (dump_flags & TDF_DETAILS))
659 fprintf (dump_file, "\n[CHECK] Found def edge %d in ", (int)i);
660 print_gimple_stmt (dump_file, phi, 0, 0);
662 edges->safe_push (opnd_edge);
664 else
666 gimple def = SSA_NAME_DEF_STMT (opnd);
668 if (gimple_code (def) == GIMPLE_PHI
669 && dominated_by_p (CDI_DOMINATORS,
670 gimple_bb (def), cd_root))
671 collect_phi_def_edges (def, cd_root, edges,
672 visited_phis);
673 else if (!uninit_undefined_value_p (opnd))
675 if (dump_file && (dump_flags & TDF_DETAILS))
677 fprintf (dump_file, "\n[CHECK] Found def edge %d in ", (int)i);
678 print_gimple_stmt (dump_file, phi, 0, 0);
680 edges->safe_push (opnd_edge);
686 /* For each use edge of PHI, computes all control dependence chains.
687 The control dependence chains are then converted to an array of
688 composite predicates pointed to by PREDS. */
690 static bool
691 find_def_preds (pred_chain_union *preds, gimple phi)
693 size_t num_chains = 0, i, n;
694 vec<edge> dep_chains[MAX_NUM_CHAINS];
695 auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
696 vec<edge> def_edges = vNULL;
697 bool has_valid_pred = false;
698 basic_block phi_bb, cd_root = 0;
699 pointer_set_t *visited_phis;
701 phi_bb = gimple_bb (phi);
702 /* First find the closest dominating bb to be
703 the control dependence root */
704 cd_root = find_dom (phi_bb);
705 if (!cd_root)
706 return false;
708 visited_phis = pointer_set_create ();
709 collect_phi_def_edges (phi, cd_root, &def_edges, visited_phis);
710 pointer_set_destroy (visited_phis);
712 n = def_edges.length ();
713 if (n == 0)
714 return false;
716 for (i = 0; i < n; i++)
718 size_t prev_nc, j;
719 int num_calls = 0;
720 edge opnd_edge;
722 opnd_edge = def_edges[i];
723 prev_nc = num_chains;
724 compute_control_dep_chain (cd_root, opnd_edge->src, dep_chains,
725 &num_chains, &cur_chain, &num_calls);
727 /* Now update the newly added chains with
728 the phi operand edge: */
729 if (EDGE_COUNT (opnd_edge->src->succs) > 1)
731 if (prev_nc == num_chains && num_chains < MAX_NUM_CHAINS)
732 dep_chains[num_chains++] = vNULL;
733 for (j = prev_nc; j < num_chains; j++)
734 dep_chains[j].safe_push (opnd_edge);
738 has_valid_pred
739 = convert_control_dep_chain_into_preds (dep_chains, num_chains, preds);
740 for (i = 0; i < num_chains; i++)
741 dep_chains[i].release ();
742 return has_valid_pred;
745 /* Dumps the predicates (PREDS) for USESTMT. */
747 static void
748 dump_predicates (gimple usestmt, pred_chain_union preds,
749 const char* msg)
751 size_t i, j;
752 pred_chain one_pred_chain = vNULL;
753 fprintf (dump_file, msg);
754 print_gimple_stmt (dump_file, usestmt, 0, 0);
755 fprintf (dump_file, "is guarded by :\n\n");
756 size_t num_preds = preds.length ();
757 /* Do some dumping here: */
758 for (i = 0; i < num_preds; i++)
760 size_t np;
762 one_pred_chain = preds[i];
763 np = one_pred_chain.length ();
765 for (j = 0; j < np; j++)
767 pred_info one_pred = one_pred_chain[j];
768 if (one_pred.invert)
769 fprintf (dump_file, " (.NOT.) ");
770 print_generic_expr (dump_file, one_pred.pred_lhs, 0);
771 fprintf (dump_file, " %s ", op_symbol_code (one_pred.cond_code));
772 print_generic_expr (dump_file, one_pred.pred_rhs, 0);
773 if (j < np - 1)
774 fprintf (dump_file, " (.AND.) ");
775 else
776 fprintf (dump_file, "\n");
778 if (i < num_preds - 1)
779 fprintf (dump_file, "(.OR.)\n");
780 else
781 fprintf (dump_file, "\n\n");
785 /* Destroys the predicate set *PREDS. */
787 static void
788 destroy_predicate_vecs (pred_chain_union preds)
790 size_t i;
792 size_t n = preds.length ();
793 for (i = 0; i < n; i++)
794 preds[i].release ();
795 preds.release ();
799 /* Computes the 'normalized' conditional code with operand
800 swapping and condition inversion. */
802 static enum tree_code
803 get_cmp_code (enum tree_code orig_cmp_code,
804 bool swap_cond, bool invert)
806 enum tree_code tc = orig_cmp_code;
808 if (swap_cond)
809 tc = swap_tree_comparison (orig_cmp_code);
810 if (invert)
811 tc = invert_tree_comparison (tc, false);
813 switch (tc)
815 case LT_EXPR:
816 case LE_EXPR:
817 case GT_EXPR:
818 case GE_EXPR:
819 case EQ_EXPR:
820 case NE_EXPR:
821 break;
822 default:
823 return ERROR_MARK;
825 return tc;
828 /* Returns true if VAL falls in the range defined by BOUNDARY and CMPC, i.e.
829 all values in the range satisfies (x CMPC BOUNDARY) == true. */
831 static bool
832 is_value_included_in (tree val, tree boundary, enum tree_code cmpc)
834 bool inverted = false;
835 bool is_unsigned;
836 bool result;
838 /* Only handle integer constant here. */
839 if (TREE_CODE (val) != INTEGER_CST
840 || TREE_CODE (boundary) != INTEGER_CST)
841 return true;
843 is_unsigned = TYPE_UNSIGNED (TREE_TYPE (val));
845 if (cmpc == GE_EXPR || cmpc == GT_EXPR
846 || cmpc == NE_EXPR)
848 cmpc = invert_tree_comparison (cmpc, false);
849 inverted = true;
852 if (is_unsigned)
854 if (cmpc == EQ_EXPR)
855 result = tree_int_cst_equal (val, boundary);
856 else if (cmpc == LT_EXPR)
857 result = INT_CST_LT_UNSIGNED (val, boundary);
858 else
860 gcc_assert (cmpc == LE_EXPR);
861 result = (tree_int_cst_equal (val, boundary)
862 || INT_CST_LT_UNSIGNED (val, boundary));
865 else
867 if (cmpc == EQ_EXPR)
868 result = tree_int_cst_equal (val, boundary);
869 else if (cmpc == LT_EXPR)
870 result = INT_CST_LT (val, boundary);
871 else
873 gcc_assert (cmpc == LE_EXPR);
874 result = (tree_int_cst_equal (val, boundary)
875 || INT_CST_LT (val, boundary));
879 if (inverted)
880 result ^= 1;
882 return result;
885 /* Returns true if PRED is common among all the predicate
886 chains (PREDS) (and therefore can be factored out).
887 NUM_PRED_CHAIN is the size of array PREDS. */
889 static bool
890 find_matching_predicate_in_rest_chains (pred_info pred,
891 pred_chain_union preds,
892 size_t num_pred_chains)
894 size_t i, j, n;
896 /* Trival case. */
897 if (num_pred_chains == 1)
898 return true;
900 for (i = 1; i < num_pred_chains; i++)
902 bool found = false;
903 pred_chain one_chain = preds[i];
904 n = one_chain.length ();
905 for (j = 0; j < n; j++)
907 pred_info pred2 = one_chain[j];
908 /* Can relax the condition comparison to not
909 use address comparison. However, the most common
910 case is that multiple control dependent paths share
911 a common path prefix, so address comparison should
912 be ok. */
914 if (operand_equal_p (pred2.pred_lhs, pred.pred_lhs, 0)
915 && operand_equal_p (pred2.pred_rhs, pred.pred_rhs, 0)
916 && pred2.invert == pred.invert)
918 found = true;
919 break;
922 if (!found)
923 return false;
925 return true;
928 /* Forward declaration. */
929 static bool
930 is_use_properly_guarded (gimple use_stmt,
931 basic_block use_bb,
932 gimple phi,
933 unsigned uninit_opnds,
934 pointer_set_t *visited_phis);
936 /* Returns true if all uninitialized opnds are pruned. Returns false
937 otherwise. PHI is the phi node with uninitialized operands,
938 UNINIT_OPNDS is the bitmap of the uninitialize operand positions,
939 FLAG_DEF is the statement defining the flag guarding the use of the
940 PHI output, BOUNDARY_CST is the const value used in the predicate
941 associated with the flag, CMP_CODE is the comparison code used in
942 the predicate, VISITED_PHIS is the pointer set of phis visited, and
943 VISITED_FLAG_PHIS is the pointer to the pointer set of flag definitions
944 that are also phis.
946 Example scenario:
948 BB1:
949 flag_1 = phi <0, 1> // (1)
950 var_1 = phi <undef, some_val>
953 BB2:
954 flag_2 = phi <0, flag_1, flag_1> // (2)
955 var_2 = phi <undef, var_1, var_1>
956 if (flag_2 == 1)
957 goto BB3;
959 BB3:
960 use of var_2 // (3)
962 Because some flag arg in (1) is not constant, if we do not look into the
963 flag phis recursively, it is conservatively treated as unknown and var_1
964 is thought to be flowed into use at (3). Since var_1 is potentially uninitialized
965 a false warning will be emitted. Checking recursively into (1), the compiler can
966 find out that only some_val (which is defined) can flow into (3) which is OK.
970 static bool
971 prune_uninit_phi_opnds_in_unrealizable_paths (gimple phi,
972 unsigned uninit_opnds,
973 gimple flag_def,
974 tree boundary_cst,
975 enum tree_code cmp_code,
976 pointer_set_t *visited_phis,
977 bitmap *visited_flag_phis)
979 unsigned i;
981 for (i = 0; i < MIN (32, gimple_phi_num_args (flag_def)); i++)
983 tree flag_arg;
985 if (!MASK_TEST_BIT (uninit_opnds, i))
986 continue;
988 flag_arg = gimple_phi_arg_def (flag_def, i);
989 if (!is_gimple_constant (flag_arg))
991 gimple flag_arg_def, phi_arg_def;
992 tree phi_arg;
993 unsigned uninit_opnds_arg_phi;
995 if (TREE_CODE (flag_arg) != SSA_NAME)
996 return false;
997 flag_arg_def = SSA_NAME_DEF_STMT (flag_arg);
998 if (gimple_code (flag_arg_def) != GIMPLE_PHI)
999 return false;
1001 phi_arg = gimple_phi_arg_def (phi, i);
1002 if (TREE_CODE (phi_arg) != SSA_NAME)
1003 return false;
1005 phi_arg_def = SSA_NAME_DEF_STMT (phi_arg);
1006 if (gimple_code (phi_arg_def) != GIMPLE_PHI)
1007 return false;
1009 if (gimple_bb (phi_arg_def) != gimple_bb (flag_arg_def))
1010 return false;
1012 if (!*visited_flag_phis)
1013 *visited_flag_phis = BITMAP_ALLOC (NULL);
1015 if (bitmap_bit_p (*visited_flag_phis,
1016 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def))))
1017 return false;
1019 bitmap_set_bit (*visited_flag_phis,
1020 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def)));
1022 /* Now recursively prune the uninitialized phi args. */
1023 uninit_opnds_arg_phi = compute_uninit_opnds_pos (phi_arg_def);
1024 if (!prune_uninit_phi_opnds_in_unrealizable_paths
1025 (phi_arg_def, uninit_opnds_arg_phi, flag_arg_def,
1026 boundary_cst, cmp_code, visited_phis, visited_flag_phis))
1027 return false;
1029 bitmap_clear_bit (*visited_flag_phis,
1030 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def)));
1031 continue;
1034 /* Now check if the constant is in the guarded range. */
1035 if (is_value_included_in (flag_arg, boundary_cst, cmp_code))
1037 tree opnd;
1038 gimple opnd_def;
1040 /* Now that we know that this undefined edge is not
1041 pruned. If the operand is defined by another phi,
1042 we can further prune the incoming edges of that
1043 phi by checking the predicates of this operands. */
1045 opnd = gimple_phi_arg_def (phi, i);
1046 opnd_def = SSA_NAME_DEF_STMT (opnd);
1047 if (gimple_code (opnd_def) == GIMPLE_PHI)
1049 edge opnd_edge;
1050 unsigned uninit_opnds2
1051 = compute_uninit_opnds_pos (opnd_def);
1052 gcc_assert (!MASK_EMPTY (uninit_opnds2));
1053 opnd_edge = gimple_phi_arg_edge (phi, i);
1054 if (!is_use_properly_guarded (phi,
1055 opnd_edge->src,
1056 opnd_def,
1057 uninit_opnds2,
1058 visited_phis))
1059 return false;
1061 else
1062 return false;
1066 return true;
1069 /* A helper function that determines if the predicate set
1070 of the use is not overlapping with that of the uninit paths.
1071 The most common senario of guarded use is in Example 1:
1072 Example 1:
1073 if (some_cond)
1075 x = ...;
1076 flag = true;
1079 ... some code ...
1081 if (flag)
1082 use (x);
1084 The real world examples are usually more complicated, but similar
1085 and usually result from inlining:
1087 bool init_func (int * x)
1089 if (some_cond)
1090 return false;
1091 *x = ..
1092 return true;
1095 void foo(..)
1097 int x;
1099 if (!init_func(&x))
1100 return;
1102 .. some_code ...
1103 use (x);
1106 Another possible use scenario is in the following trivial example:
1108 Example 2:
1109 if (n > 0)
1110 x = 1;
1112 if (n > 0)
1114 if (m < 2)
1115 .. = x;
1118 Predicate analysis needs to compute the composite predicate:
1120 1) 'x' use predicate: (n > 0) .AND. (m < 2)
1121 2) 'x' default value (non-def) predicate: .NOT. (n > 0)
1122 (the predicate chain for phi operand defs can be computed
1123 starting from a bb that is control equivalent to the phi's
1124 bb and is dominating the operand def.)
1126 and check overlapping:
1127 (n > 0) .AND. (m < 2) .AND. (.NOT. (n > 0))
1128 <==> false
1130 This implementation provides framework that can handle
1131 scenarios. (Note that many simple cases are handled properly
1132 without the predicate analysis -- this is due to jump threading
1133 transformation which eliminates the merge point thus makes
1134 path sensitive analysis unnecessary.)
1136 NUM_PREDS is the number is the number predicate chains, PREDS is
1137 the array of chains, PHI is the phi node whose incoming (undefined)
1138 paths need to be pruned, and UNINIT_OPNDS is the bitmap holding
1139 uninit operand positions. VISITED_PHIS is the pointer set of phi
1140 stmts being checked. */
1143 static bool
1144 use_pred_not_overlap_with_undef_path_pred (pred_chain_union preds,
1145 gimple phi, unsigned uninit_opnds,
1146 pointer_set_t *visited_phis)
1148 unsigned int i, n;
1149 gimple flag_def = 0;
1150 tree boundary_cst = 0;
1151 enum tree_code cmp_code;
1152 bool swap_cond = false;
1153 bool invert = false;
1154 pred_chain the_pred_chain = vNULL;
1155 bitmap visited_flag_phis = NULL;
1156 bool all_pruned = false;
1157 size_t num_preds = preds.length ();
1159 gcc_assert (num_preds > 0);
1160 /* Find within the common prefix of multiple predicate chains
1161 a predicate that is a comparison of a flag variable against
1162 a constant. */
1163 the_pred_chain = preds[0];
1164 n = the_pred_chain.length ();
1165 for (i = 0; i < n; i++)
1167 tree cond_lhs, cond_rhs, flag = 0;
1169 pred_info the_pred = the_pred_chain[i];
1171 invert = the_pred.invert;
1172 cond_lhs = the_pred.pred_lhs;
1173 cond_rhs = the_pred.pred_rhs;
1174 cmp_code = the_pred.cond_code;
1176 if (cond_lhs != NULL_TREE && TREE_CODE (cond_lhs) == SSA_NAME
1177 && cond_rhs != NULL_TREE && is_gimple_constant (cond_rhs))
1179 boundary_cst = cond_rhs;
1180 flag = cond_lhs;
1182 else if (cond_rhs != NULL_TREE && TREE_CODE (cond_rhs) == SSA_NAME
1183 && cond_lhs != NULL_TREE && is_gimple_constant (cond_lhs))
1185 boundary_cst = cond_lhs;
1186 flag = cond_rhs;
1187 swap_cond = true;
1190 if (!flag)
1191 continue;
1193 flag_def = SSA_NAME_DEF_STMT (flag);
1195 if (!flag_def)
1196 continue;
1198 if ((gimple_code (flag_def) == GIMPLE_PHI)
1199 && (gimple_bb (flag_def) == gimple_bb (phi))
1200 && find_matching_predicate_in_rest_chains (the_pred, preds,
1201 num_preds))
1202 break;
1204 flag_def = 0;
1207 if (!flag_def)
1208 return false;
1210 /* Now check all the uninit incoming edge has a constant flag value
1211 that is in conflict with the use guard/predicate. */
1212 cmp_code = get_cmp_code (cmp_code, swap_cond, invert);
1214 if (cmp_code == ERROR_MARK)
1215 return false;
1217 all_pruned = prune_uninit_phi_opnds_in_unrealizable_paths (phi,
1218 uninit_opnds,
1219 flag_def,
1220 boundary_cst,
1221 cmp_code,
1222 visited_phis,
1223 &visited_flag_phis);
1225 if (visited_flag_phis)
1226 BITMAP_FREE (visited_flag_phis);
1228 return all_pruned;
1231 /* The helper function returns true if two predicates X1 and X2
1232 are equivalent. It assumes the expressions have already
1233 properly re-associated. */
1235 static inline bool
1236 pred_equal_p (pred_info x1, pred_info x2)
1238 enum tree_code c1, c2;
1239 if (!operand_equal_p (x1.pred_lhs, x2.pred_lhs, 0)
1240 || !operand_equal_p (x1.pred_rhs, x2.pred_rhs, 0))
1241 return false;
1243 c1 = x1.cond_code;
1244 if (x1.invert != x2.invert)
1245 c2 = invert_tree_comparison (x2.cond_code, false);
1246 else
1247 c2 = x2.cond_code;
1249 return c1 == c2;
1252 /* Returns true if the predication is testing !=. */
1254 static inline bool
1255 is_neq_relop_p (pred_info pred)
1258 return (pred.cond_code == NE_EXPR && !pred.invert)
1259 || (pred.cond_code == EQ_EXPR && pred.invert);
1262 /* Returns true if pred is of the form X != 0. */
1264 static inline bool
1265 is_neq_zero_form_p (pred_info pred)
1267 if (!is_neq_relop_p (pred) || !integer_zerop (pred.pred_rhs)
1268 || TREE_CODE (pred.pred_lhs) != SSA_NAME)
1269 return false;
1270 return true;
1273 /* The helper function returns true if two predicates X1
1274 is equivalent to X2 != 0. */
1276 static inline bool
1277 pred_expr_equal_p (pred_info x1, tree x2)
1279 if (!is_neq_zero_form_p (x1))
1280 return false;
1282 return operand_equal_p (x1.pred_lhs, x2, 0);
1285 /* Returns true of the domain of single predicate expression
1286 EXPR1 is a subset of that of EXPR2. Returns false if it
1287 can not be proved. */
1289 static bool
1290 is_pred_expr_subset_of (pred_info expr1, pred_info expr2)
1292 enum tree_code code1, code2;
1294 if (pred_equal_p (expr1, expr2))
1295 return true;
1297 if ((TREE_CODE (expr1.pred_rhs) != INTEGER_CST)
1298 || (TREE_CODE (expr2.pred_rhs) != INTEGER_CST))
1299 return false;
1301 if (!operand_equal_p (expr1.pred_lhs, expr2.pred_lhs, 0))
1302 return false;
1304 code1 = expr1.cond_code;
1305 if (expr1.invert)
1306 code1 = invert_tree_comparison (code1, false);
1307 code2 = expr2.cond_code;
1308 if (expr2.invert)
1309 code2 = invert_tree_comparison (code2, false);
1311 if (code1 != code2 && code2 != NE_EXPR)
1312 return false;
1314 if (is_value_included_in (expr1.pred_rhs, expr2.pred_rhs, code2))
1315 return true;
1317 return false;
1320 /* Returns true if the domain of PRED1 is a subset
1321 of that of PRED2. Returns false if it can not be proved so. */
1323 static bool
1324 is_pred_chain_subset_of (pred_chain pred1,
1325 pred_chain pred2)
1327 size_t np1, np2, i1, i2;
1329 np1 = pred1.length ();
1330 np2 = pred2.length ();
1332 for (i2 = 0; i2 < np2; i2++)
1334 bool found = false;
1335 pred_info info2 = pred2[i2];
1336 for (i1 = 0; i1 < np1; i1++)
1338 pred_info info1 = pred1[i1];
1339 if (is_pred_expr_subset_of (info1, info2))
1341 found = true;
1342 break;
1345 if (!found)
1346 return false;
1348 return true;
1351 /* Returns true if the domain defined by
1352 one pred chain ONE_PRED is a subset of the domain
1353 of *PREDS. It returns false if ONE_PRED's domain is
1354 not a subset of any of the sub-domains of PREDS
1355 (corresponding to each individual chains in it), even
1356 though it may be still be a subset of whole domain
1357 of PREDS which is the union (ORed) of all its subdomains.
1358 In other words, the result is conservative. */
1360 static bool
1361 is_included_in (pred_chain one_pred, pred_chain_union preds)
1363 size_t i;
1364 size_t n = preds.length ();
1366 for (i = 0; i < n; i++)
1368 if (is_pred_chain_subset_of (one_pred, preds[i]))
1369 return true;
1372 return false;
1375 /* Compares two predicate sets PREDS1 and PREDS2 and returns
1376 true if the domain defined by PREDS1 is a superset
1377 of PREDS2's domain. N1 and N2 are array sizes of PREDS1 and
1378 PREDS2 respectively. The implementation chooses not to build
1379 generic trees (and relying on the folding capability of the
1380 compiler), but instead performs brute force comparison of
1381 individual predicate chains (won't be a compile time problem
1382 as the chains are pretty short). When the function returns
1383 false, it does not necessarily mean *PREDS1 is not a superset
1384 of *PREDS2, but mean it may not be so since the analysis can
1385 not prove it. In such cases, false warnings may still be
1386 emitted. */
1388 static bool
1389 is_superset_of (pred_chain_union preds1, pred_chain_union preds2)
1391 size_t i, n2;
1392 pred_chain one_pred_chain = vNULL;
1394 n2 = preds2.length ();
1396 for (i = 0; i < n2; i++)
1398 one_pred_chain = preds2[i];
1399 if (!is_included_in (one_pred_chain, preds1))
1400 return false;
1403 return true;
1406 /* Returns true if TC is AND or OR. */
1408 static inline bool
1409 is_and_or_or_p (enum tree_code tc, tree type)
1411 return (tc == BIT_IOR_EXPR
1412 || (tc == BIT_AND_EXPR
1413 && (type == 0 || TREE_CODE (type) == BOOLEAN_TYPE)));
1416 /* Returns true if X1 is the negate of X2. */
1418 static inline bool
1419 pred_neg_p (pred_info x1, pred_info x2)
1421 enum tree_code c1, c2;
1422 if (!operand_equal_p (x1.pred_lhs, x2.pred_lhs, 0)
1423 || !operand_equal_p (x1.pred_rhs, x2.pred_rhs, 0))
1424 return false;
1426 c1 = x1.cond_code;
1427 if (x1.invert == x2.invert)
1428 c2 = invert_tree_comparison (x2.cond_code, false);
1429 else
1430 c2 = x2.cond_code;
1432 return c1 == c2;
1435 /* 1) ((x IOR y) != 0) AND (x != 0) is equivalent to (x != 0);
1436 2) (X AND Y) OR (!X AND Y) is equivalent to Y;
1437 3) X OR (!X AND Y) is equivalent to (X OR Y);
1438 4) ((x IAND y) != 0) || (x != 0 AND y != 0)) is equivalent to
1439 (x != 0 AND y != 0)
1440 5) (X AND Y) OR (!X AND Z) OR (!Y AND Z) is equivalent to
1441 (X AND Y) OR Z
1443 PREDS is the predicate chains, and N is the number of chains. */
1445 /* Helper function to implement rule 1 above. ONE_CHAIN is
1446 the AND predication to be simplified. */
1448 static void
1449 simplify_pred (pred_chain *one_chain)
1451 size_t i, j, n;
1452 bool simplified = false;
1453 pred_chain s_chain = vNULL;
1455 n = one_chain->length ();
1457 for (i = 0; i < n; i++)
1459 pred_info *a_pred = &(*one_chain)[i];
1461 if (!a_pred->pred_lhs)
1462 continue;
1463 if (!is_neq_zero_form_p (*a_pred))
1464 continue;
1466 gimple def_stmt = SSA_NAME_DEF_STMT (a_pred->pred_lhs);
1467 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1468 continue;
1469 if (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR)
1471 for (j = 0; j < n; j++)
1473 pred_info *b_pred = &(*one_chain)[j];
1475 if (!b_pred->pred_lhs)
1476 continue;
1477 if (!is_neq_zero_form_p (*b_pred))
1478 continue;
1480 if (pred_expr_equal_p (*b_pred, gimple_assign_rhs1 (def_stmt))
1481 || pred_expr_equal_p (*b_pred, gimple_assign_rhs2 (def_stmt)))
1483 /* Mark a_pred for removal. */
1484 a_pred->pred_lhs = NULL;
1485 a_pred->pred_rhs = NULL;
1486 simplified = true;
1487 break;
1493 if (!simplified)
1494 return;
1496 for (i = 0; i < n; i++)
1498 pred_info *a_pred = &(*one_chain)[i];
1499 if (!a_pred->pred_lhs)
1500 continue;
1501 s_chain.safe_push (*a_pred);
1504 one_chain->release ();
1505 *one_chain = s_chain;
1508 /* The helper function implements the rule 2 for the
1509 OR predicate PREDS.
1511 2) (X AND Y) OR (!X AND Y) is equivalent to Y. */
1513 static bool
1514 simplify_preds_2 (pred_chain_union *preds)
1516 size_t i, j, n;
1517 bool simplified = false;
1518 pred_chain_union s_preds = vNULL;
1520 /* (X AND Y) OR (!X AND Y) is equivalent to Y.
1521 (X AND Y) OR (X AND !Y) is equivalent to X. */
1523 n = preds->length ();
1524 for (i = 0; i < n; i++)
1526 pred_info x, y;
1527 pred_chain *a_chain = &(*preds)[i];
1529 if (a_chain->length () != 2)
1530 continue;
1532 x = (*a_chain)[0];
1533 y = (*a_chain)[1];
1535 for (j = 0; j < n; j++)
1537 pred_chain *b_chain;
1538 pred_info x2, y2;
1540 if (j == i)
1541 continue;
1543 b_chain = &(*preds)[j];
1544 if (b_chain->length () != 2)
1545 continue;
1547 x2 = (*b_chain)[0];
1548 y2 = (*b_chain)[1];
1550 if (pred_equal_p (x, x2) && pred_neg_p (y, y2))
1552 /* Kill a_chain. */
1553 a_chain->release ();
1554 b_chain->release ();
1555 b_chain->safe_push (x);
1556 simplified = true;
1557 break;
1559 if (pred_neg_p (x, x2) && pred_equal_p (y, y2))
1561 /* Kill a_chain. */
1562 a_chain->release ();
1563 b_chain->release ();
1564 b_chain->safe_push (y);
1565 simplified = true;
1566 break;
1570 /* Now clean up the chain. */
1571 if (simplified)
1573 for (i = 0; i < n; i++)
1575 if ((*preds)[i].is_empty ())
1576 continue;
1577 s_preds.safe_push ((*preds)[i]);
1579 preds->release ();
1580 (*preds) = s_preds;
1581 s_preds = vNULL;
1584 return simplified;
1587 /* The helper function implements the rule 2 for the
1588 OR predicate PREDS.
1590 3) x OR (!x AND y) is equivalent to x OR y. */
1592 static bool
1593 simplify_preds_3 (pred_chain_union *preds)
1595 size_t i, j, n;
1596 bool simplified = false;
1598 /* Now iteratively simplify X OR (!X AND Z ..)
1599 into X OR (Z ...). */
1601 n = preds->length ();
1602 if (n < 2)
1603 return false;
1605 for (i = 0; i < n; i++)
1607 pred_info x;
1608 pred_chain *a_chain = &(*preds)[i];
1610 if (a_chain->length () != 1)
1611 continue;
1613 x = (*a_chain)[0];
1615 for (j = 0; j < n; j++)
1617 pred_chain *b_chain;
1618 pred_info x2;
1619 size_t k;
1621 if (j == i)
1622 continue;
1624 b_chain = &(*preds)[j];
1625 if (b_chain->length () < 2)
1626 continue;
1628 for (k = 0; k < b_chain->length (); k++)
1630 x2 = (*b_chain)[k];
1631 if (pred_neg_p (x, x2))
1633 b_chain->unordered_remove (k);
1634 simplified = true;
1635 break;
1640 return simplified;
1643 /* The helper function implements the rule 4 for the
1644 OR predicate PREDS.
1646 2) ((x AND y) != 0) OR (x != 0 AND y != 0) is equivalent to
1647 (x != 0 ANd y != 0). */
1649 static bool
1650 simplify_preds_4 (pred_chain_union *preds)
1652 size_t i, j, n;
1653 bool simplified = false;
1654 pred_chain_union s_preds = vNULL;
1655 gimple def_stmt;
1657 n = preds->length ();
1658 for (i = 0; i < n; i++)
1660 pred_info z;
1661 pred_chain *a_chain = &(*preds)[i];
1663 if (a_chain->length () != 1)
1664 continue;
1666 z = (*a_chain)[0];
1668 if (!is_neq_zero_form_p (z))
1669 continue;
1671 def_stmt = SSA_NAME_DEF_STMT (z.pred_lhs);
1672 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1673 continue;
1675 if (gimple_assign_rhs_code (def_stmt) != BIT_AND_EXPR)
1676 continue;
1678 for (j = 0; j < n; j++)
1680 pred_chain *b_chain;
1681 pred_info x2, y2;
1683 if (j == i)
1684 continue;
1686 b_chain = &(*preds)[j];
1687 if (b_chain->length () != 2)
1688 continue;
1690 x2 = (*b_chain)[0];
1691 y2 = (*b_chain)[1];
1692 if (!is_neq_zero_form_p (x2)
1693 || !is_neq_zero_form_p (y2))
1694 continue;
1696 if ((pred_expr_equal_p (x2, gimple_assign_rhs1 (def_stmt))
1697 && pred_expr_equal_p (y2, gimple_assign_rhs2 (def_stmt)))
1698 || (pred_expr_equal_p (x2, gimple_assign_rhs2 (def_stmt))
1699 && pred_expr_equal_p (y2, gimple_assign_rhs1 (def_stmt))))
1701 /* Kill a_chain. */
1702 a_chain->release ();
1703 simplified = true;
1704 break;
1708 /* Now clean up the chain. */
1709 if (simplified)
1711 for (i = 0; i < n; i++)
1713 if ((*preds)[i].is_empty ())
1714 continue;
1715 s_preds.safe_push ((*preds)[i]);
1717 preds->release ();
1718 (*preds) = s_preds;
1719 s_preds = vNULL;
1722 return simplified;
1726 /* This function simplifies predicates in PREDS. */
1728 static void
1729 simplify_preds (pred_chain_union *preds, gimple use_or_def, bool is_use)
1731 size_t i, n;
1732 bool changed = false;
1734 if (dump_file && dump_flags & TDF_DETAILS)
1736 fprintf (dump_file, "[BEFORE SIMPLICATION -- ");
1737 dump_predicates (use_or_def, *preds, is_use ? "[USE]:\n" : "[DEF]:\n");
1740 for (i = 0; i < preds->length (); i++)
1741 simplify_pred (&(*preds)[i]);
1743 n = preds->length ();
1744 if (n < 2)
1745 return;
1749 changed = false;
1750 if (simplify_preds_2 (preds))
1751 changed = true;
1753 /* Now iteratively simplify X OR (!X AND Z ..)
1754 into X OR (Z ...). */
1755 if (simplify_preds_3 (preds))
1756 changed = true;
1758 if (simplify_preds_4 (preds))
1759 changed = true;
1761 } while (changed);
1763 return;
1766 /* This is a helper function which attempts to normalize predicate chains
1767 by following UD chains. It basically builds up a big tree of either IOR
1768 operations or AND operations, and convert the IOR tree into a
1769 pred_chain_union or BIT_AND tree into a pred_chain.
1770 Example:
1772 _3 = _2 RELOP1 _1;
1773 _6 = _5 RELOP2 _4;
1774 _9 = _8 RELOP3 _7;
1775 _10 = _3 | _6;
1776 _12 = _9 | _0;
1777 _t = _10 | _12;
1779 then _t != 0 will be normalized into a pred_chain_union
1781 (_2 RELOP1 _1) OR (_5 RELOP2 _4) OR (_8 RELOP3 _7) OR (_0 != 0)
1783 Similarly given,
1785 _3 = _2 RELOP1 _1;
1786 _6 = _5 RELOP2 _4;
1787 _9 = _8 RELOP3 _7;
1788 _10 = _3 & _6;
1789 _12 = _9 & _0;
1791 then _t != 0 will be normalized into a pred_chain:
1792 (_2 RELOP1 _1) AND (_5 RELOP2 _4) AND (_8 RELOP3 _7) AND (_0 != 0)
1796 /* This is a helper function that stores a PRED into NORM_PREDS. */
1798 inline static void
1799 push_pred (pred_chain_union *norm_preds, pred_info pred)
1801 pred_chain pred_chain = vNULL;
1802 pred_chain.safe_push (pred);
1803 norm_preds->safe_push (pred_chain);
1806 /* A helper function that creates a predicate of the form
1807 OP != 0 and push it WORK_LIST. */
1809 inline static void
1810 push_to_worklist (tree op, vec<pred_info, va_heap, vl_ptr> *work_list,
1811 pointer_set_t *mark_set)
1813 if (pointer_set_contains (mark_set, op))
1814 return;
1815 pointer_set_insert (mark_set, op);
1817 pred_info arg_pred;
1818 arg_pred.pred_lhs = op;
1819 arg_pred.pred_rhs = integer_zero_node;
1820 arg_pred.cond_code = NE_EXPR;
1821 arg_pred.invert = false;
1822 work_list->safe_push (arg_pred);
1825 /* A helper that generates a pred_info from a gimple assignment
1826 CMP_ASSIGN with comparison rhs. */
1828 static pred_info
1829 get_pred_info_from_cmp (gimple cmp_assign)
1831 pred_info n_pred;
1832 n_pred.pred_lhs = gimple_assign_rhs1 (cmp_assign);
1833 n_pred.pred_rhs = gimple_assign_rhs2 (cmp_assign);
1834 n_pred.cond_code = gimple_assign_rhs_code (cmp_assign);
1835 n_pred.invert = false;
1836 return n_pred;
1839 /* Returns true if the PHI is a degenerated phi with
1840 all args with the same value (relop). In that case, *PRED
1841 will be updated to that value. */
1843 static bool
1844 is_degenerated_phi (gimple phi, pred_info *pred_p)
1846 int i, n;
1847 tree op0;
1848 gimple def0;
1849 pred_info pred0;
1851 n = gimple_phi_num_args (phi);
1852 op0 = gimple_phi_arg_def (phi, 0);
1854 if (TREE_CODE (op0) != SSA_NAME)
1855 return false;
1857 def0 = SSA_NAME_DEF_STMT (op0);
1858 if (gimple_code (def0) != GIMPLE_ASSIGN)
1859 return false;
1860 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def0))
1861 != tcc_comparison)
1862 return false;
1863 pred0 = get_pred_info_from_cmp (def0);
1865 for (i = 1; i < n; ++i)
1867 gimple def;
1868 pred_info pred;
1869 tree op = gimple_phi_arg_def (phi, i);
1871 if (TREE_CODE (op) != SSA_NAME)
1872 return false;
1874 def = SSA_NAME_DEF_STMT (op);
1875 if (gimple_code (def) != GIMPLE_ASSIGN)
1876 return false;
1877 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def))
1878 != tcc_comparison)
1879 return false;
1880 pred = get_pred_info_from_cmp (def);
1881 if (!pred_equal_p (pred, pred0))
1882 return false;
1885 *pred_p = pred0;
1886 return true;
1889 /* Normalize one predicate PRED
1890 1) if PRED can no longer be normlized, put it into NORM_PREDS.
1891 2) otherwise if PRED is of the form x != 0, follow x's definition
1892 and put normalized predicates into WORK_LIST. */
1894 static void
1895 normalize_one_pred_1 (pred_chain_union *norm_preds,
1896 pred_chain *norm_chain,
1897 pred_info pred,
1898 enum tree_code and_or_code,
1899 vec<pred_info, va_heap, vl_ptr> *work_list,
1900 pointer_set_t *mark_set)
1902 if (!is_neq_zero_form_p (pred))
1904 if (and_or_code == BIT_IOR_EXPR)
1905 push_pred (norm_preds, pred);
1906 else
1907 norm_chain->safe_push (pred);
1908 return;
1911 gimple def_stmt = SSA_NAME_DEF_STMT (pred.pred_lhs);
1913 if (gimple_code (def_stmt) == GIMPLE_PHI
1914 && is_degenerated_phi (def_stmt, &pred))
1915 work_list->safe_push (pred);
1916 else if (gimple_code (def_stmt) == GIMPLE_PHI
1917 && and_or_code == BIT_IOR_EXPR)
1919 int i, n;
1920 n = gimple_phi_num_args (def_stmt);
1922 /* If we see non zero constant, we should punt. The predicate
1923 * should be one guarding the phi edge. */
1924 for (i = 0; i < n; ++i)
1926 tree op = gimple_phi_arg_def (def_stmt, i);
1927 if (TREE_CODE (op) == INTEGER_CST && !integer_zerop (op))
1929 push_pred (norm_preds, pred);
1930 return;
1934 for (i = 0; i < n; ++i)
1936 tree op = gimple_phi_arg_def (def_stmt, i);
1937 if (integer_zerop (op))
1938 continue;
1940 push_to_worklist (op, work_list, mark_set);
1943 else if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1945 if (and_or_code == BIT_IOR_EXPR)
1946 push_pred (norm_preds, pred);
1947 else
1948 norm_chain->safe_push (pred);
1950 else if (gimple_assign_rhs_code (def_stmt) == and_or_code)
1952 push_to_worklist (gimple_assign_rhs1 (def_stmt), work_list, mark_set);
1953 push_to_worklist (gimple_assign_rhs2 (def_stmt), work_list, mark_set);
1955 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt))
1956 == tcc_comparison)
1958 pred_info n_pred = get_pred_info_from_cmp (def_stmt);
1959 if (and_or_code == BIT_IOR_EXPR)
1960 push_pred (norm_preds, n_pred);
1961 else
1962 norm_chain->safe_push (n_pred);
1964 else
1966 if (and_or_code == BIT_IOR_EXPR)
1967 push_pred (norm_preds, pred);
1968 else
1969 norm_chain->safe_push (pred);
1973 /* Normalize PRED and store the normalized predicates into NORM_PREDS. */
1975 static void
1976 normalize_one_pred (pred_chain_union *norm_preds,
1977 pred_info pred)
1979 vec<pred_info, va_heap, vl_ptr> work_list = vNULL;
1980 pointer_set_t *mark_set = NULL;
1981 enum tree_code and_or_code = ERROR_MARK;
1982 pred_chain norm_chain = vNULL;
1984 if (!is_neq_zero_form_p (pred))
1986 push_pred (norm_preds, pred);
1987 return;
1990 gimple def_stmt = SSA_NAME_DEF_STMT (pred.pred_lhs);
1991 if (gimple_code (def_stmt) == GIMPLE_ASSIGN)
1992 and_or_code = gimple_assign_rhs_code (def_stmt);
1993 if (and_or_code != BIT_IOR_EXPR
1994 && and_or_code != BIT_AND_EXPR)
1996 if (TREE_CODE_CLASS (and_or_code)
1997 == tcc_comparison)
1999 pred_info n_pred = get_pred_info_from_cmp (def_stmt);
2000 push_pred (norm_preds, n_pred);
2002 else
2003 push_pred (norm_preds, pred);
2004 return;
2007 work_list.safe_push (pred);
2008 mark_set = pointer_set_create ();
2010 while (!work_list.is_empty ())
2012 pred_info a_pred = work_list.pop ();
2013 normalize_one_pred_1 (norm_preds, &norm_chain, a_pred,
2014 and_or_code, &work_list, mark_set);
2016 if (and_or_code == BIT_AND_EXPR)
2017 norm_preds->safe_push (norm_chain);
2019 work_list.release ();
2020 pointer_set_destroy (mark_set);
2023 static void
2024 normalize_one_pred_chain (pred_chain_union *norm_preds,
2025 pred_chain one_chain)
2027 vec<pred_info, va_heap, vl_ptr> work_list = vNULL;
2028 pointer_set_t *mark_set = pointer_set_create ();
2029 pred_chain norm_chain = vNULL;
2030 size_t i;
2032 for (i = 0; i < one_chain.length (); i++)
2034 work_list.safe_push (one_chain[i]);
2035 pointer_set_insert (mark_set, one_chain[i].pred_lhs);
2038 while (!work_list.is_empty ())
2040 pred_info a_pred = work_list.pop ();
2041 normalize_one_pred_1 (0, &norm_chain, a_pred,
2042 BIT_AND_EXPR, &work_list, mark_set);
2045 norm_preds->safe_push (norm_chain);
2046 work_list.release ();
2047 pointer_set_destroy (mark_set);
2050 /* Normalize predicate chains PREDS and returns the normalized one. */
2052 static pred_chain_union
2053 normalize_preds (pred_chain_union preds, gimple use_or_def, bool is_use)
2055 pred_chain_union norm_preds = vNULL;
2056 size_t n = preds.length ();
2057 size_t i;
2059 if (dump_file && dump_flags & TDF_DETAILS)
2061 fprintf (dump_file, "[BEFORE NORMALIZATION --");
2062 dump_predicates (use_or_def, preds, is_use ? "[USE]:\n" : "[DEF]:\n");
2065 for (i = 0; i < n; i++)
2067 if (preds[i].length () != 1)
2068 normalize_one_pred_chain (&norm_preds, preds[i]);
2069 else
2071 normalize_one_pred (&norm_preds, preds[i][0]);
2072 preds[i].release ();
2076 if (dump_file)
2078 fprintf (dump_file, "[AFTER NORMALIZATION -- ");
2079 dump_predicates (use_or_def, norm_preds, is_use ? "[USE]:\n" : "[DEF]:\n");
2082 preds.release ();
2083 return norm_preds;
2087 /* Computes the predicates that guard the use and checks
2088 if the incoming paths that have empty (or possibly
2089 empty) definition can be pruned/filtered. The function returns
2090 true if it can be determined that the use of PHI's def in
2091 USE_STMT is guarded with a predicate set not overlapping with
2092 predicate sets of all runtime paths that do not have a definition.
2093 Returns false if it is not or it can not be determined. USE_BB is
2094 the bb of the use (for phi operand use, the bb is not the bb of
2095 the phi stmt, but the src bb of the operand edge). UNINIT_OPNDS
2096 is a bit vector. If an operand of PHI is uninitialized, the
2097 corresponding bit in the vector is 1. VISIED_PHIS is a pointer
2098 set of phis being visted. */
2100 static bool
2101 is_use_properly_guarded (gimple use_stmt,
2102 basic_block use_bb,
2103 gimple phi,
2104 unsigned uninit_opnds,
2105 pointer_set_t *visited_phis)
2107 basic_block phi_bb;
2108 pred_chain_union preds = vNULL;
2109 pred_chain_union def_preds = vNULL;
2110 bool has_valid_preds = false;
2111 bool is_properly_guarded = false;
2113 if (pointer_set_insert (visited_phis, phi))
2114 return false;
2116 phi_bb = gimple_bb (phi);
2118 if (is_non_loop_exit_postdominating (use_bb, phi_bb))
2119 return false;
2121 has_valid_preds = find_predicates (&preds, phi_bb, use_bb);
2123 if (!has_valid_preds)
2125 destroy_predicate_vecs (preds);
2126 return false;
2129 /* Try to prune the dead incoming phi edges. */
2130 is_properly_guarded
2131 = use_pred_not_overlap_with_undef_path_pred (preds, phi, uninit_opnds,
2132 visited_phis);
2134 if (is_properly_guarded)
2136 destroy_predicate_vecs (preds);
2137 return true;
2140 has_valid_preds = find_def_preds (&def_preds, phi);
2142 if (!has_valid_preds)
2144 destroy_predicate_vecs (preds);
2145 destroy_predicate_vecs (def_preds);
2146 return false;
2149 simplify_preds (&preds, use_stmt, true);
2150 preds = normalize_preds (preds, use_stmt, true);
2152 simplify_preds (&def_preds, phi, false);
2153 def_preds = normalize_preds (def_preds, phi, false);
2155 is_properly_guarded = is_superset_of (def_preds, preds);
2157 destroy_predicate_vecs (preds);
2158 destroy_predicate_vecs (def_preds);
2159 return is_properly_guarded;
2162 /* Searches through all uses of a potentially
2163 uninitialized variable defined by PHI and returns a use
2164 statement if the use is not properly guarded. It returns
2165 NULL if all uses are guarded. UNINIT_OPNDS is a bitvector
2166 holding the position(s) of uninit PHI operands. WORKLIST
2167 is the vector of candidate phis that may be updated by this
2168 function. ADDED_TO_WORKLIST is the pointer set tracking
2169 if the new phi is already in the worklist. */
2171 static gimple
2172 find_uninit_use (gimple phi, unsigned uninit_opnds,
2173 vec<gimple> *worklist,
2174 pointer_set_t *added_to_worklist)
2176 tree phi_result;
2177 use_operand_p use_p;
2178 gimple use_stmt;
2179 imm_use_iterator iter;
2181 phi_result = gimple_phi_result (phi);
2183 FOR_EACH_IMM_USE_FAST (use_p, iter, phi_result)
2185 pointer_set_t *visited_phis;
2186 basic_block use_bb;
2188 use_stmt = USE_STMT (use_p);
2189 if (is_gimple_debug (use_stmt))
2190 continue;
2192 visited_phis = pointer_set_create ();
2194 if (gimple_code (use_stmt) == GIMPLE_PHI)
2195 use_bb = gimple_phi_arg_edge (use_stmt,
2196 PHI_ARG_INDEX_FROM_USE (use_p))->src;
2197 else
2198 use_bb = gimple_bb (use_stmt);
2200 if (is_use_properly_guarded (use_stmt, use_bb, phi, uninit_opnds,
2201 visited_phis))
2203 pointer_set_destroy (visited_phis);
2204 continue;
2206 pointer_set_destroy (visited_phis);
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 (!pointer_set_insert (added_to_worklist, 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 pointer_set_insert (possibly_undefined_names, 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 pointer_set_t *added_to_worklist)
2247 unsigned uninit_opnds;
2248 gimple uninit_use_stmt = 0;
2249 tree uninit_op;
2251 /* Don't look at virtual operands. */
2252 if (virtual_operand_p (gimple_phi_result (phi)))
2253 return;
2255 uninit_opnds = compute_uninit_opnds_pos (phi);
2257 if (MASK_EMPTY (uninit_opnds))
2258 return;
2260 if (dump_file && (dump_flags & TDF_DETAILS))
2262 fprintf (dump_file, "[CHECK]: examining phi: ");
2263 print_gimple_stmt (dump_file, phi, 0, 0);
2266 /* Now check if we have any use of the value without proper guard. */
2267 uninit_use_stmt = find_uninit_use (phi, uninit_opnds,
2268 worklist, added_to_worklist);
2270 /* All uses are properly guarded. */
2271 if (!uninit_use_stmt)
2272 return;
2274 uninit_op = gimple_phi_arg_def (phi, MASK_FIRST_SET_BIT (uninit_opnds));
2275 if (SSA_NAME_VAR (uninit_op) == NULL_TREE)
2276 return;
2277 warn_uninit (OPT_Wmaybe_uninitialized, uninit_op, SSA_NAME_VAR (uninit_op),
2278 SSA_NAME_VAR (uninit_op),
2279 "%qD may be used uninitialized in this function",
2280 uninit_use_stmt);
2284 static bool
2285 gate_warn_uninitialized (void)
2287 return warn_uninitialized || warn_maybe_uninitialized;
2290 namespace {
2292 const pass_data pass_data_late_warn_uninitialized =
2294 GIMPLE_PASS, /* type */
2295 "uninit", /* name */
2296 OPTGROUP_NONE, /* optinfo_flags */
2297 true, /* has_execute */
2298 TV_NONE, /* tv_id */
2299 PROP_ssa, /* properties_required */
2300 0, /* properties_provided */
2301 0, /* properties_destroyed */
2302 0, /* todo_flags_start */
2303 0, /* todo_flags_finish */
2306 class pass_late_warn_uninitialized : public gimple_opt_pass
2308 public:
2309 pass_late_warn_uninitialized (gcc::context *ctxt)
2310 : gimple_opt_pass (pass_data_late_warn_uninitialized, ctxt)
2313 /* opt_pass methods: */
2314 opt_pass * clone () { return new pass_late_warn_uninitialized (m_ctxt); }
2315 virtual bool gate (function *) { return gate_warn_uninitialized (); }
2316 virtual unsigned int execute (function *);
2318 }; // class pass_late_warn_uninitialized
2320 unsigned int
2321 pass_late_warn_uninitialized::execute (function *fun)
2323 basic_block bb;
2324 gimple_stmt_iterator gsi;
2325 vec<gimple> worklist = vNULL;
2326 pointer_set_t *added_to_worklist;
2328 calculate_dominance_info (CDI_DOMINATORS);
2329 calculate_dominance_info (CDI_POST_DOMINATORS);
2330 /* Re-do the plain uninitialized variable check, as optimization may have
2331 straightened control flow. Do this first so that we don't accidentally
2332 get a "may be" warning when we'd have seen an "is" warning later. */
2333 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/1);
2335 timevar_push (TV_TREE_UNINIT);
2337 possibly_undefined_names = pointer_set_create ();
2338 added_to_worklist = pointer_set_create ();
2340 /* Initialize worklist */
2341 FOR_EACH_BB_FN (bb, fun)
2342 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2344 gimple phi = gsi_stmt (gsi);
2345 size_t n, i;
2347 n = gimple_phi_num_args (phi);
2349 /* Don't look at virtual operands. */
2350 if (virtual_operand_p (gimple_phi_result (phi)))
2351 continue;
2353 for (i = 0; i < n; ++i)
2355 tree op = gimple_phi_arg_def (phi, i);
2356 if (TREE_CODE (op) == SSA_NAME
2357 && uninit_undefined_value_p (op))
2359 worklist.safe_push (phi);
2360 pointer_set_insert (added_to_worklist, phi);
2361 if (dump_file && (dump_flags & TDF_DETAILS))
2363 fprintf (dump_file, "[WORKLIST]: add to initial list: ");
2364 print_gimple_stmt (dump_file, phi, 0, 0);
2366 break;
2371 while (worklist.length () != 0)
2373 gimple cur_phi = 0;
2374 cur_phi = worklist.pop ();
2375 warn_uninitialized_phi (cur_phi, &worklist, added_to_worklist);
2378 worklist.release ();
2379 pointer_set_destroy (added_to_worklist);
2380 pointer_set_destroy (possibly_undefined_names);
2381 possibly_undefined_names = NULL;
2382 free_dominance_info (CDI_POST_DOMINATORS);
2383 timevar_pop (TV_TREE_UNINIT);
2384 return 0;
2387 } // anon namespace
2389 gimple_opt_pass *
2390 make_pass_late_warn_uninitialized (gcc::context *ctxt)
2392 return new pass_late_warn_uninitialized (ctxt);
2396 static unsigned int
2397 execute_early_warn_uninitialized (void)
2399 /* Currently, this pass runs always but
2400 execute_late_warn_uninitialized only runs with optimization. With
2401 optimization we want to warn about possible uninitialized as late
2402 as possible, thus don't do it here. However, without
2403 optimization we need to warn here about "may be uninitialized". */
2404 calculate_dominance_info (CDI_POST_DOMINATORS);
2406 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/!optimize);
2408 /* Post-dominator information can not be reliably updated. Free it
2409 after the use. */
2411 free_dominance_info (CDI_POST_DOMINATORS);
2412 return 0;
2416 namespace {
2418 const pass_data pass_data_early_warn_uninitialized =
2420 GIMPLE_PASS, /* type */
2421 "*early_warn_uninitialized", /* name */
2422 OPTGROUP_NONE, /* optinfo_flags */
2423 true, /* has_execute */
2424 TV_TREE_UNINIT, /* tv_id */
2425 PROP_ssa, /* properties_required */
2426 0, /* properties_provided */
2427 0, /* properties_destroyed */
2428 0, /* todo_flags_start */
2429 0, /* todo_flags_finish */
2432 class pass_early_warn_uninitialized : public gimple_opt_pass
2434 public:
2435 pass_early_warn_uninitialized (gcc::context *ctxt)
2436 : gimple_opt_pass (pass_data_early_warn_uninitialized, ctxt)
2439 /* opt_pass methods: */
2440 virtual bool gate (function *) { return gate_warn_uninitialized (); }
2441 virtual unsigned int execute (function *)
2443 return execute_early_warn_uninitialized ();
2446 }; // class pass_early_warn_uninitialized
2448 } // anon namespace
2450 gimple_opt_pass *
2451 make_pass_early_warn_uninitialized (gcc::context *ctxt)
2453 return new pass_early_warn_uninitialized (ctxt);