save-temps_0.c: New file.
[official-gcc.git] / gcc / tree-ssa-uninit.c
blobeee83f79a9acaba83ab4aedb7f720af00563f419
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 ??? Note that at -O0 we do not have virtual operands.
214 ??? Not so cheap would be to use the alias oracle via
215 walk_aliased_vdefs, if we don't find any aliasing vdef
216 warn as is-used-uninitialized, if we don't find an aliasing
217 vdef that kills our use (stmt_kills_ref_p), warn as
218 may-be-used-uninitialized. But this walk is quadratic and
219 so must be limited which means we would miss warning
220 opportunities. */
221 use = gimple_vuse (stmt);
222 if (use
223 && gimple_assign_single_p (stmt)
224 && !gimple_vdef (stmt)
225 && SSA_NAME_IS_DEFAULT_DEF (use))
227 tree rhs = gimple_assign_rhs1 (stmt);
228 tree base = get_base_address (rhs);
230 /* Do not warn if it can be initialized outside this function. */
231 if (TREE_CODE (base) != VAR_DECL
232 || DECL_HARD_REGISTER (base)
233 || is_global_var (base))
234 continue;
236 if (always_executed)
237 warn_uninit (OPT_Wuninitialized, use,
238 gimple_assign_rhs1 (stmt), base,
239 "%qE is used uninitialized in this function",
240 stmt);
241 else if (warn_possibly_uninitialized)
242 warn_uninit (OPT_Wmaybe_uninitialized, use,
243 gimple_assign_rhs1 (stmt), base,
244 "%qE may be used uninitialized in this function",
245 stmt);
250 return 0;
253 /* Checks if the operand OPND of PHI is defined by
254 another phi with one operand defined by this PHI,
255 but the rest operands are all defined. If yes,
256 returns true to skip this this operand as being
257 redundant. Can be enhanced to be more general. */
259 static bool
260 can_skip_redundant_opnd (tree opnd, gimple phi)
262 gimple op_def;
263 tree phi_def;
264 int i, n;
266 phi_def = gimple_phi_result (phi);
267 op_def = SSA_NAME_DEF_STMT (opnd);
268 if (gimple_code (op_def) != GIMPLE_PHI)
269 return false;
270 n = gimple_phi_num_args (op_def);
271 for (i = 0; i < n; ++i)
273 tree op = gimple_phi_arg_def (op_def, i);
274 if (TREE_CODE (op) != SSA_NAME)
275 continue;
276 if (op != phi_def && uninit_undefined_value_p (op))
277 return false;
280 return true;
283 /* Returns a bit mask holding the positions of arguments in PHI
284 that have empty (or possibly empty) definitions. */
286 static unsigned
287 compute_uninit_opnds_pos (gimple phi)
289 size_t i, n;
290 unsigned uninit_opnds = 0;
292 n = gimple_phi_num_args (phi);
293 /* Bail out for phi with too many args. */
294 if (n > 32)
295 return 0;
297 for (i = 0; i < n; ++i)
299 tree op = gimple_phi_arg_def (phi, i);
300 if (TREE_CODE (op) == SSA_NAME
301 && uninit_undefined_value_p (op)
302 && !can_skip_redundant_opnd (op, phi))
304 if (cfun->has_nonlocal_label || cfun->calls_setjmp)
306 /* Ignore SSA_NAMEs that appear on abnormal edges
307 somewhere. */
308 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
309 continue;
311 MASK_SET_BIT (uninit_opnds, i);
314 return uninit_opnds;
317 /* Find the immediate postdominator PDOM of the specified
318 basic block BLOCK. */
320 static inline basic_block
321 find_pdom (basic_block block)
323 if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
324 return EXIT_BLOCK_PTR_FOR_FN (cfun);
325 else
327 basic_block bb
328 = get_immediate_dominator (CDI_POST_DOMINATORS, block);
329 if (! bb)
330 return EXIT_BLOCK_PTR_FOR_FN (cfun);
331 return bb;
335 /* Find the immediate DOM of the specified
336 basic block BLOCK. */
338 static inline basic_block
339 find_dom (basic_block block)
341 if (block == ENTRY_BLOCK_PTR_FOR_FN (cfun))
342 return ENTRY_BLOCK_PTR_FOR_FN (cfun);
343 else
345 basic_block bb = get_immediate_dominator (CDI_DOMINATORS, block);
346 if (! bb)
347 return ENTRY_BLOCK_PTR_FOR_FN (cfun);
348 return bb;
352 /* Returns true if BB1 is postdominating BB2 and BB1 is
353 not a loop exit bb. The loop exit bb check is simple and does
354 not cover all cases. */
356 static bool
357 is_non_loop_exit_postdominating (basic_block bb1, basic_block bb2)
359 if (!dominated_by_p (CDI_POST_DOMINATORS, bb2, bb1))
360 return false;
362 if (single_pred_p (bb1) && !single_succ_p (bb2))
363 return false;
365 return true;
368 /* Find the closest postdominator of a specified BB, which is control
369 equivalent to BB. */
371 static inline basic_block
372 find_control_equiv_block (basic_block bb)
374 basic_block pdom;
376 pdom = find_pdom (bb);
378 /* Skip the postdominating bb that is also loop exit. */
379 if (!is_non_loop_exit_postdominating (pdom, bb))
380 return NULL;
382 if (dominated_by_p (CDI_DOMINATORS, pdom, bb))
383 return pdom;
385 return NULL;
388 #define MAX_NUM_CHAINS 8
389 #define MAX_CHAIN_LEN 5
390 #define MAX_POSTDOM_CHECK 8
392 /* Computes the control dependence chains (paths of edges)
393 for DEP_BB up to the dominating basic block BB (the head node of a
394 chain should be dominated by it). CD_CHAINS is pointer to an
395 array holding the result chains. CUR_CD_CHAIN is the current
396 chain being computed. *NUM_CHAINS is total number of chains. The
397 function returns true if the information is successfully computed,
398 return false if there is no control dependence or not computed. */
400 static bool
401 compute_control_dep_chain (basic_block bb, basic_block dep_bb,
402 vec<edge> *cd_chains,
403 size_t *num_chains,
404 vec<edge> *cur_cd_chain,
405 int *num_calls)
407 edge_iterator ei;
408 edge e;
409 size_t i;
410 bool found_cd_chain = false;
411 size_t cur_chain_len = 0;
413 if (EDGE_COUNT (bb->succs) < 2)
414 return false;
416 if (*num_calls > PARAM_VALUE (PARAM_UNINIT_CONTROL_DEP_ATTEMPTS))
417 return false;
418 ++*num_calls;
420 /* Could use a set instead. */
421 cur_chain_len = cur_cd_chain->length ();
422 if (cur_chain_len > MAX_CHAIN_LEN)
423 return false;
425 for (i = 0; i < cur_chain_len; i++)
427 edge e = (*cur_cd_chain)[i];
428 /* Cycle detected. */
429 if (e->src == bb)
430 return false;
433 FOR_EACH_EDGE (e, ei, bb->succs)
435 basic_block cd_bb;
436 int post_dom_check = 0;
437 if (e->flags & (EDGE_FAKE | EDGE_ABNORMAL))
438 continue;
440 cd_bb = e->dest;
441 cur_cd_chain->safe_push (e);
442 while (!is_non_loop_exit_postdominating (cd_bb, bb))
444 if (cd_bb == dep_bb)
446 /* Found a direct control dependence. */
447 if (*num_chains < MAX_NUM_CHAINS)
449 cd_chains[*num_chains] = cur_cd_chain->copy ();
450 (*num_chains)++;
452 found_cd_chain = true;
453 /* Check path from next edge. */
454 break;
457 /* Now check if DEP_BB is indirectly control dependent on BB. */
458 if (compute_control_dep_chain (cd_bb, dep_bb, cd_chains,
459 num_chains, cur_cd_chain, num_calls))
461 found_cd_chain = true;
462 break;
465 cd_bb = find_pdom (cd_bb);
466 post_dom_check++;
467 if (cd_bb == EXIT_BLOCK_PTR_FOR_FN (cfun) || post_dom_check >
468 MAX_POSTDOM_CHECK)
469 break;
471 cur_cd_chain->pop ();
472 gcc_assert (cur_cd_chain->length () == cur_chain_len);
474 gcc_assert (cur_cd_chain->length () == cur_chain_len);
476 return found_cd_chain;
479 /* The type to represent a simple predicate */
481 typedef struct use_def_pred_info
483 tree pred_lhs;
484 tree pred_rhs;
485 enum tree_code cond_code;
486 bool invert;
487 } pred_info;
489 /* The type to represent a sequence of predicates grouped
490 with .AND. operation. */
492 typedef vec<pred_info, va_heap, vl_ptr> pred_chain;
494 /* The type to represent a sequence of pred_chains grouped
495 with .OR. operation. */
497 typedef vec<pred_chain, va_heap, vl_ptr> pred_chain_union;
499 /* Converts the chains of control dependence edges into a set of
500 predicates. A control dependence chain is represented by a vector
501 edges. DEP_CHAINS points to an array of dependence chains.
502 NUM_CHAINS is the size of the chain array. One edge in a dependence
503 chain is mapped to predicate expression represented by pred_info
504 type. One dependence chain is converted to a composite predicate that
505 is the result of AND operation of pred_info mapped to each edge.
506 A composite predicate is presented by a vector of pred_info. On
507 return, *PREDS points to the resulting array of composite predicates.
508 *NUM_PREDS is the number of composite predictes. */
510 static bool
511 convert_control_dep_chain_into_preds (vec<edge> *dep_chains,
512 size_t num_chains,
513 pred_chain_union *preds)
515 bool has_valid_pred = false;
516 size_t i, j;
517 if (num_chains == 0 || num_chains >= MAX_NUM_CHAINS)
518 return false;
520 /* Now convert the control dep chain into a set
521 of predicates. */
522 preds->reserve (num_chains);
524 for (i = 0; i < num_chains; i++)
526 vec<edge> one_cd_chain = dep_chains[i];
528 has_valid_pred = false;
529 pred_chain t_chain = vNULL;
530 for (j = 0; j < one_cd_chain.length (); j++)
532 gimple cond_stmt;
533 gimple_stmt_iterator gsi;
534 basic_block guard_bb;
535 pred_info one_pred;
536 edge e;
538 e = one_cd_chain[j];
539 guard_bb = e->src;
540 gsi = gsi_last_bb (guard_bb);
541 if (gsi_end_p (gsi))
543 has_valid_pred = false;
544 break;
546 cond_stmt = gsi_stmt (gsi);
547 if (is_gimple_call (cond_stmt)
548 && EDGE_COUNT (e->src->succs) >= 2)
550 /* Ignore EH edge. Can add assertion
551 on the other edge's flag. */
552 continue;
554 /* Skip if there is essentially one succesor. */
555 if (EDGE_COUNT (e->src->succs) == 2)
557 edge e1;
558 edge_iterator ei1;
559 bool skip = false;
561 FOR_EACH_EDGE (e1, ei1, e->src->succs)
563 if (EDGE_COUNT (e1->dest->succs) == 0)
565 skip = true;
566 break;
569 if (skip)
570 continue;
572 if (gimple_code (cond_stmt) != GIMPLE_COND)
574 has_valid_pred = false;
575 break;
577 one_pred.pred_lhs = gimple_cond_lhs (cond_stmt);
578 one_pred.pred_rhs = gimple_cond_rhs (cond_stmt);
579 one_pred.cond_code = gimple_cond_code (cond_stmt);
580 one_pred.invert = !!(e->flags & EDGE_FALSE_VALUE);
581 t_chain.safe_push (one_pred);
582 has_valid_pred = true;
585 if (!has_valid_pred)
586 break;
587 else
588 preds->safe_push (t_chain);
590 return has_valid_pred;
593 /* Computes all control dependence chains for USE_BB. The control
594 dependence chains are then converted to an array of composite
595 predicates pointed to by PREDS. PHI_BB is the basic block of
596 the phi whose result is used in USE_BB. */
598 static bool
599 find_predicates (pred_chain_union *preds,
600 basic_block phi_bb,
601 basic_block use_bb)
603 size_t num_chains = 0, i;
604 int num_calls = 0;
605 vec<edge> dep_chains[MAX_NUM_CHAINS];
606 auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
607 bool has_valid_pred = false;
608 basic_block cd_root = 0;
610 /* First find the closest bb that is control equivalent to PHI_BB
611 that also dominates USE_BB. */
612 cd_root = phi_bb;
613 while (dominated_by_p (CDI_DOMINATORS, use_bb, cd_root))
615 basic_block ctrl_eq_bb = find_control_equiv_block (cd_root);
616 if (ctrl_eq_bb && dominated_by_p (CDI_DOMINATORS, use_bb, ctrl_eq_bb))
617 cd_root = ctrl_eq_bb;
618 else
619 break;
622 compute_control_dep_chain (cd_root, use_bb, dep_chains, &num_chains,
623 &cur_chain, &num_calls);
625 has_valid_pred
626 = convert_control_dep_chain_into_preds (dep_chains, num_chains, preds);
627 for (i = 0; i < num_chains; i++)
628 dep_chains[i].release ();
629 return has_valid_pred;
632 /* Computes the set of incoming edges of PHI that have non empty
633 definitions of a phi chain. The collection will be done
634 recursively on operands that are defined by phis. CD_ROOT
635 is the control dependence root. *EDGES holds the result, and
636 VISITED_PHIS is a pointer set for detecting cycles. */
638 static void
639 collect_phi_def_edges (gimple phi, basic_block cd_root,
640 vec<edge> *edges,
641 pointer_set_t *visited_phis)
643 size_t i, n;
644 edge opnd_edge;
645 tree opnd;
647 if (pointer_set_insert (visited_phis, phi))
648 return;
650 n = gimple_phi_num_args (phi);
651 for (i = 0; i < n; i++)
653 opnd_edge = gimple_phi_arg_edge (phi, i);
654 opnd = gimple_phi_arg_def (phi, i);
656 if (TREE_CODE (opnd) != SSA_NAME)
658 if (dump_file && (dump_flags & TDF_DETAILS))
660 fprintf (dump_file, "\n[CHECK] Found def edge %d in ", (int)i);
661 print_gimple_stmt (dump_file, phi, 0, 0);
663 edges->safe_push (opnd_edge);
665 else
667 gimple def = SSA_NAME_DEF_STMT (opnd);
669 if (gimple_code (def) == GIMPLE_PHI
670 && dominated_by_p (CDI_DOMINATORS,
671 gimple_bb (def), cd_root))
672 collect_phi_def_edges (def, cd_root, edges,
673 visited_phis);
674 else if (!uninit_undefined_value_p (opnd))
676 if (dump_file && (dump_flags & TDF_DETAILS))
678 fprintf (dump_file, "\n[CHECK] Found def edge %d in ", (int)i);
679 print_gimple_stmt (dump_file, phi, 0, 0);
681 edges->safe_push (opnd_edge);
687 /* For each use edge of PHI, computes all control dependence chains.
688 The control dependence chains are then converted to an array of
689 composite predicates pointed to by PREDS. */
691 static bool
692 find_def_preds (pred_chain_union *preds, gimple phi)
694 size_t num_chains = 0, i, n;
695 vec<edge> dep_chains[MAX_NUM_CHAINS];
696 auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
697 vec<edge> def_edges = vNULL;
698 bool has_valid_pred = false;
699 basic_block phi_bb, cd_root = 0;
700 pointer_set_t *visited_phis;
702 phi_bb = gimple_bb (phi);
703 /* First find the closest dominating bb to be
704 the control dependence root */
705 cd_root = find_dom (phi_bb);
706 if (!cd_root)
707 return false;
709 visited_phis = pointer_set_create ();
710 collect_phi_def_edges (phi, cd_root, &def_edges, visited_phis);
711 pointer_set_destroy (visited_phis);
713 n = def_edges.length ();
714 if (n == 0)
715 return false;
717 for (i = 0; i < n; i++)
719 size_t prev_nc, j;
720 int num_calls = 0;
721 edge opnd_edge;
723 opnd_edge = def_edges[i];
724 prev_nc = num_chains;
725 compute_control_dep_chain (cd_root, opnd_edge->src, dep_chains,
726 &num_chains, &cur_chain, &num_calls);
728 /* Now update the newly added chains with
729 the phi operand edge: */
730 if (EDGE_COUNT (opnd_edge->src->succs) > 1)
732 if (prev_nc == num_chains && num_chains < MAX_NUM_CHAINS)
733 dep_chains[num_chains++] = vNULL;
734 for (j = prev_nc; j < num_chains; j++)
735 dep_chains[j].safe_push (opnd_edge);
739 has_valid_pred
740 = convert_control_dep_chain_into_preds (dep_chains, num_chains, preds);
741 for (i = 0; i < num_chains; i++)
742 dep_chains[i].release ();
743 return has_valid_pred;
746 /* Dumps the predicates (PREDS) for USESTMT. */
748 static void
749 dump_predicates (gimple usestmt, pred_chain_union preds,
750 const char* msg)
752 size_t i, j;
753 pred_chain one_pred_chain = vNULL;
754 fprintf (dump_file, msg);
755 print_gimple_stmt (dump_file, usestmt, 0, 0);
756 fprintf (dump_file, "is guarded by :\n\n");
757 size_t num_preds = preds.length ();
758 /* Do some dumping here: */
759 for (i = 0; i < num_preds; i++)
761 size_t np;
763 one_pred_chain = preds[i];
764 np = one_pred_chain.length ();
766 for (j = 0; j < np; j++)
768 pred_info one_pred = one_pred_chain[j];
769 if (one_pred.invert)
770 fprintf (dump_file, " (.NOT.) ");
771 print_generic_expr (dump_file, one_pred.pred_lhs, 0);
772 fprintf (dump_file, " %s ", op_symbol_code (one_pred.cond_code));
773 print_generic_expr (dump_file, one_pred.pred_rhs, 0);
774 if (j < np - 1)
775 fprintf (dump_file, " (.AND.) ");
776 else
777 fprintf (dump_file, "\n");
779 if (i < num_preds - 1)
780 fprintf (dump_file, "(.OR.)\n");
781 else
782 fprintf (dump_file, "\n\n");
786 /* Destroys the predicate set *PREDS. */
788 static void
789 destroy_predicate_vecs (pred_chain_union preds)
791 size_t i;
793 size_t n = preds.length ();
794 for (i = 0; i < n; i++)
795 preds[i].release ();
796 preds.release ();
800 /* Computes the 'normalized' conditional code with operand
801 swapping and condition inversion. */
803 static enum tree_code
804 get_cmp_code (enum tree_code orig_cmp_code,
805 bool swap_cond, bool invert)
807 enum tree_code tc = orig_cmp_code;
809 if (swap_cond)
810 tc = swap_tree_comparison (orig_cmp_code);
811 if (invert)
812 tc = invert_tree_comparison (tc, false);
814 switch (tc)
816 case LT_EXPR:
817 case LE_EXPR:
818 case GT_EXPR:
819 case GE_EXPR:
820 case EQ_EXPR:
821 case NE_EXPR:
822 break;
823 default:
824 return ERROR_MARK;
826 return tc;
829 /* Returns true if VAL falls in the range defined by BOUNDARY and CMPC, i.e.
830 all values in the range satisfies (x CMPC BOUNDARY) == true. */
832 static bool
833 is_value_included_in (tree val, tree boundary, enum tree_code cmpc)
835 bool inverted = false;
836 bool is_unsigned;
837 bool result;
839 /* Only handle integer constant here. */
840 if (TREE_CODE (val) != INTEGER_CST
841 || TREE_CODE (boundary) != INTEGER_CST)
842 return true;
844 is_unsigned = TYPE_UNSIGNED (TREE_TYPE (val));
846 if (cmpc == GE_EXPR || cmpc == GT_EXPR
847 || cmpc == NE_EXPR)
849 cmpc = invert_tree_comparison (cmpc, false);
850 inverted = true;
853 if (is_unsigned)
855 if (cmpc == EQ_EXPR)
856 result = tree_int_cst_equal (val, boundary);
857 else if (cmpc == LT_EXPR)
858 result = INT_CST_LT_UNSIGNED (val, boundary);
859 else
861 gcc_assert (cmpc == LE_EXPR);
862 result = (tree_int_cst_equal (val, boundary)
863 || INT_CST_LT_UNSIGNED (val, boundary));
866 else
868 if (cmpc == EQ_EXPR)
869 result = tree_int_cst_equal (val, boundary);
870 else if (cmpc == LT_EXPR)
871 result = INT_CST_LT (val, boundary);
872 else
874 gcc_assert (cmpc == LE_EXPR);
875 result = (tree_int_cst_equal (val, boundary)
876 || INT_CST_LT (val, boundary));
880 if (inverted)
881 result ^= 1;
883 return result;
886 /* Returns true if PRED is common among all the predicate
887 chains (PREDS) (and therefore can be factored out).
888 NUM_PRED_CHAIN is the size of array PREDS. */
890 static bool
891 find_matching_predicate_in_rest_chains (pred_info pred,
892 pred_chain_union preds,
893 size_t num_pred_chains)
895 size_t i, j, n;
897 /* Trival case. */
898 if (num_pred_chains == 1)
899 return true;
901 for (i = 1; i < num_pred_chains; i++)
903 bool found = false;
904 pred_chain one_chain = preds[i];
905 n = one_chain.length ();
906 for (j = 0; j < n; j++)
908 pred_info pred2 = one_chain[j];
909 /* Can relax the condition comparison to not
910 use address comparison. However, the most common
911 case is that multiple control dependent paths share
912 a common path prefix, so address comparison should
913 be ok. */
915 if (operand_equal_p (pred2.pred_lhs, pred.pred_lhs, 0)
916 && operand_equal_p (pred2.pred_rhs, pred.pred_rhs, 0)
917 && pred2.invert == pred.invert)
919 found = true;
920 break;
923 if (!found)
924 return false;
926 return true;
929 /* Forward declaration. */
930 static bool
931 is_use_properly_guarded (gimple use_stmt,
932 basic_block use_bb,
933 gimple phi,
934 unsigned uninit_opnds,
935 pointer_set_t *visited_phis);
937 /* Returns true if all uninitialized opnds are pruned. Returns false
938 otherwise. PHI is the phi node with uninitialized operands,
939 UNINIT_OPNDS is the bitmap of the uninitialize operand positions,
940 FLAG_DEF is the statement defining the flag guarding the use of the
941 PHI output, BOUNDARY_CST is the const value used in the predicate
942 associated with the flag, CMP_CODE is the comparison code used in
943 the predicate, VISITED_PHIS is the pointer set of phis visited, and
944 VISITED_FLAG_PHIS is the pointer to the pointer set of flag definitions
945 that are also phis.
947 Example scenario:
949 BB1:
950 flag_1 = phi <0, 1> // (1)
951 var_1 = phi <undef, some_val>
954 BB2:
955 flag_2 = phi <0, flag_1, flag_1> // (2)
956 var_2 = phi <undef, var_1, var_1>
957 if (flag_2 == 1)
958 goto BB3;
960 BB3:
961 use of var_2 // (3)
963 Because some flag arg in (1) is not constant, if we do not look into the
964 flag phis recursively, it is conservatively treated as unknown and var_1
965 is thought to be flowed into use at (3). Since var_1 is potentially uninitialized
966 a false warning will be emitted. Checking recursively into (1), the compiler can
967 find out that only some_val (which is defined) can flow into (3) which is OK.
971 static bool
972 prune_uninit_phi_opnds_in_unrealizable_paths (gimple phi,
973 unsigned uninit_opnds,
974 gimple flag_def,
975 tree boundary_cst,
976 enum tree_code cmp_code,
977 pointer_set_t *visited_phis,
978 bitmap *visited_flag_phis)
980 unsigned i;
982 for (i = 0; i < MIN (32, gimple_phi_num_args (flag_def)); i++)
984 tree flag_arg;
986 if (!MASK_TEST_BIT (uninit_opnds, i))
987 continue;
989 flag_arg = gimple_phi_arg_def (flag_def, i);
990 if (!is_gimple_constant (flag_arg))
992 gimple flag_arg_def, phi_arg_def;
993 tree phi_arg;
994 unsigned uninit_opnds_arg_phi;
996 if (TREE_CODE (flag_arg) != SSA_NAME)
997 return false;
998 flag_arg_def = SSA_NAME_DEF_STMT (flag_arg);
999 if (gimple_code (flag_arg_def) != GIMPLE_PHI)
1000 return false;
1002 phi_arg = gimple_phi_arg_def (phi, i);
1003 if (TREE_CODE (phi_arg) != SSA_NAME)
1004 return false;
1006 phi_arg_def = SSA_NAME_DEF_STMT (phi_arg);
1007 if (gimple_code (phi_arg_def) != GIMPLE_PHI)
1008 return false;
1010 if (gimple_bb (phi_arg_def) != gimple_bb (flag_arg_def))
1011 return false;
1013 if (!*visited_flag_phis)
1014 *visited_flag_phis = BITMAP_ALLOC (NULL);
1016 if (bitmap_bit_p (*visited_flag_phis,
1017 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def))))
1018 return false;
1020 bitmap_set_bit (*visited_flag_phis,
1021 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def)));
1023 /* Now recursively prune the uninitialized phi args. */
1024 uninit_opnds_arg_phi = compute_uninit_opnds_pos (phi_arg_def);
1025 if (!prune_uninit_phi_opnds_in_unrealizable_paths
1026 (phi_arg_def, uninit_opnds_arg_phi, flag_arg_def,
1027 boundary_cst, cmp_code, visited_phis, visited_flag_phis))
1028 return false;
1030 bitmap_clear_bit (*visited_flag_phis,
1031 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def)));
1032 continue;
1035 /* Now check if the constant is in the guarded range. */
1036 if (is_value_included_in (flag_arg, boundary_cst, cmp_code))
1038 tree opnd;
1039 gimple opnd_def;
1041 /* Now that we know that this undefined edge is not
1042 pruned. If the operand is defined by another phi,
1043 we can further prune the incoming edges of that
1044 phi by checking the predicates of this operands. */
1046 opnd = gimple_phi_arg_def (phi, i);
1047 opnd_def = SSA_NAME_DEF_STMT (opnd);
1048 if (gimple_code (opnd_def) == GIMPLE_PHI)
1050 edge opnd_edge;
1051 unsigned uninit_opnds2
1052 = compute_uninit_opnds_pos (opnd_def);
1053 gcc_assert (!MASK_EMPTY (uninit_opnds2));
1054 opnd_edge = gimple_phi_arg_edge (phi, i);
1055 if (!is_use_properly_guarded (phi,
1056 opnd_edge->src,
1057 opnd_def,
1058 uninit_opnds2,
1059 visited_phis))
1060 return false;
1062 else
1063 return false;
1067 return true;
1070 /* A helper function that determines if the predicate set
1071 of the use is not overlapping with that of the uninit paths.
1072 The most common senario of guarded use is in Example 1:
1073 Example 1:
1074 if (some_cond)
1076 x = ...;
1077 flag = true;
1080 ... some code ...
1082 if (flag)
1083 use (x);
1085 The real world examples are usually more complicated, but similar
1086 and usually result from inlining:
1088 bool init_func (int * x)
1090 if (some_cond)
1091 return false;
1092 *x = ..
1093 return true;
1096 void foo(..)
1098 int x;
1100 if (!init_func(&x))
1101 return;
1103 .. some_code ...
1104 use (x);
1107 Another possible use scenario is in the following trivial example:
1109 Example 2:
1110 if (n > 0)
1111 x = 1;
1113 if (n > 0)
1115 if (m < 2)
1116 .. = x;
1119 Predicate analysis needs to compute the composite predicate:
1121 1) 'x' use predicate: (n > 0) .AND. (m < 2)
1122 2) 'x' default value (non-def) predicate: .NOT. (n > 0)
1123 (the predicate chain for phi operand defs can be computed
1124 starting from a bb that is control equivalent to the phi's
1125 bb and is dominating the operand def.)
1127 and check overlapping:
1128 (n > 0) .AND. (m < 2) .AND. (.NOT. (n > 0))
1129 <==> false
1131 This implementation provides framework that can handle
1132 scenarios. (Note that many simple cases are handled properly
1133 without the predicate analysis -- this is due to jump threading
1134 transformation which eliminates the merge point thus makes
1135 path sensitive analysis unnecessary.)
1137 NUM_PREDS is the number is the number predicate chains, PREDS is
1138 the array of chains, PHI is the phi node whose incoming (undefined)
1139 paths need to be pruned, and UNINIT_OPNDS is the bitmap holding
1140 uninit operand positions. VISITED_PHIS is the pointer set of phi
1141 stmts being checked. */
1144 static bool
1145 use_pred_not_overlap_with_undef_path_pred (pred_chain_union preds,
1146 gimple phi, unsigned uninit_opnds,
1147 pointer_set_t *visited_phis)
1149 unsigned int i, n;
1150 gimple flag_def = 0;
1151 tree boundary_cst = 0;
1152 enum tree_code cmp_code;
1153 bool swap_cond = false;
1154 bool invert = false;
1155 pred_chain the_pred_chain = vNULL;
1156 bitmap visited_flag_phis = NULL;
1157 bool all_pruned = false;
1158 size_t num_preds = preds.length ();
1160 gcc_assert (num_preds > 0);
1161 /* Find within the common prefix of multiple predicate chains
1162 a predicate that is a comparison of a flag variable against
1163 a constant. */
1164 the_pred_chain = preds[0];
1165 n = the_pred_chain.length ();
1166 for (i = 0; i < n; i++)
1168 tree cond_lhs, cond_rhs, flag = 0;
1170 pred_info the_pred = the_pred_chain[i];
1172 invert = the_pred.invert;
1173 cond_lhs = the_pred.pred_lhs;
1174 cond_rhs = the_pred.pred_rhs;
1175 cmp_code = the_pred.cond_code;
1177 if (cond_lhs != NULL_TREE && TREE_CODE (cond_lhs) == SSA_NAME
1178 && cond_rhs != NULL_TREE && is_gimple_constant (cond_rhs))
1180 boundary_cst = cond_rhs;
1181 flag = cond_lhs;
1183 else if (cond_rhs != NULL_TREE && TREE_CODE (cond_rhs) == SSA_NAME
1184 && cond_lhs != NULL_TREE && is_gimple_constant (cond_lhs))
1186 boundary_cst = cond_lhs;
1187 flag = cond_rhs;
1188 swap_cond = true;
1191 if (!flag)
1192 continue;
1194 flag_def = SSA_NAME_DEF_STMT (flag);
1196 if (!flag_def)
1197 continue;
1199 if ((gimple_code (flag_def) == GIMPLE_PHI)
1200 && (gimple_bb (flag_def) == gimple_bb (phi))
1201 && find_matching_predicate_in_rest_chains (the_pred, preds,
1202 num_preds))
1203 break;
1205 flag_def = 0;
1208 if (!flag_def)
1209 return false;
1211 /* Now check all the uninit incoming edge has a constant flag value
1212 that is in conflict with the use guard/predicate. */
1213 cmp_code = get_cmp_code (cmp_code, swap_cond, invert);
1215 if (cmp_code == ERROR_MARK)
1216 return false;
1218 all_pruned = prune_uninit_phi_opnds_in_unrealizable_paths (phi,
1219 uninit_opnds,
1220 flag_def,
1221 boundary_cst,
1222 cmp_code,
1223 visited_phis,
1224 &visited_flag_phis);
1226 if (visited_flag_phis)
1227 BITMAP_FREE (visited_flag_phis);
1229 return all_pruned;
1232 /* The helper function returns true if two predicates X1 and X2
1233 are equivalent. It assumes the expressions have already
1234 properly re-associated. */
1236 static inline bool
1237 pred_equal_p (pred_info x1, pred_info x2)
1239 enum tree_code c1, c2;
1240 if (!operand_equal_p (x1.pred_lhs, x2.pred_lhs, 0)
1241 || !operand_equal_p (x1.pred_rhs, x2.pred_rhs, 0))
1242 return false;
1244 c1 = x1.cond_code;
1245 if (x1.invert != x2.invert)
1246 c2 = invert_tree_comparison (x2.cond_code, false);
1247 else
1248 c2 = x2.cond_code;
1250 return c1 == c2;
1253 /* Returns true if the predication is testing !=. */
1255 static inline bool
1256 is_neq_relop_p (pred_info pred)
1259 return (pred.cond_code == NE_EXPR && !pred.invert)
1260 || (pred.cond_code == EQ_EXPR && pred.invert);
1263 /* Returns true if pred is of the form X != 0. */
1265 static inline bool
1266 is_neq_zero_form_p (pred_info pred)
1268 if (!is_neq_relop_p (pred) || !integer_zerop (pred.pred_rhs)
1269 || TREE_CODE (pred.pred_lhs) != SSA_NAME)
1270 return false;
1271 return true;
1274 /* The helper function returns true if two predicates X1
1275 is equivalent to X2 != 0. */
1277 static inline bool
1278 pred_expr_equal_p (pred_info x1, tree x2)
1280 if (!is_neq_zero_form_p (x1))
1281 return false;
1283 return operand_equal_p (x1.pred_lhs, x2, 0);
1286 /* Returns true of the domain of single predicate expression
1287 EXPR1 is a subset of that of EXPR2. Returns false if it
1288 can not be proved. */
1290 static bool
1291 is_pred_expr_subset_of (pred_info expr1, pred_info expr2)
1293 enum tree_code code1, code2;
1295 if (pred_equal_p (expr1, expr2))
1296 return true;
1298 if ((TREE_CODE (expr1.pred_rhs) != INTEGER_CST)
1299 || (TREE_CODE (expr2.pred_rhs) != INTEGER_CST))
1300 return false;
1302 if (!operand_equal_p (expr1.pred_lhs, expr2.pred_lhs, 0))
1303 return false;
1305 code1 = expr1.cond_code;
1306 if (expr1.invert)
1307 code1 = invert_tree_comparison (code1, false);
1308 code2 = expr2.cond_code;
1309 if (expr2.invert)
1310 code2 = invert_tree_comparison (code2, false);
1312 if (code1 != code2 && code2 != NE_EXPR)
1313 return false;
1315 if (is_value_included_in (expr1.pred_rhs, expr2.pred_rhs, code2))
1316 return true;
1318 return false;
1321 /* Returns true if the domain of PRED1 is a subset
1322 of that of PRED2. Returns false if it can not be proved so. */
1324 static bool
1325 is_pred_chain_subset_of (pred_chain pred1,
1326 pred_chain pred2)
1328 size_t np1, np2, i1, i2;
1330 np1 = pred1.length ();
1331 np2 = pred2.length ();
1333 for (i2 = 0; i2 < np2; i2++)
1335 bool found = false;
1336 pred_info info2 = pred2[i2];
1337 for (i1 = 0; i1 < np1; i1++)
1339 pred_info info1 = pred1[i1];
1340 if (is_pred_expr_subset_of (info1, info2))
1342 found = true;
1343 break;
1346 if (!found)
1347 return false;
1349 return true;
1352 /* Returns true if the domain defined by
1353 one pred chain ONE_PRED is a subset of the domain
1354 of *PREDS. It returns false if ONE_PRED's domain is
1355 not a subset of any of the sub-domains of PREDS
1356 (corresponding to each individual chains in it), even
1357 though it may be still be a subset of whole domain
1358 of PREDS which is the union (ORed) of all its subdomains.
1359 In other words, the result is conservative. */
1361 static bool
1362 is_included_in (pred_chain one_pred, pred_chain_union preds)
1364 size_t i;
1365 size_t n = preds.length ();
1367 for (i = 0; i < n; i++)
1369 if (is_pred_chain_subset_of (one_pred, preds[i]))
1370 return true;
1373 return false;
1376 /* Compares two predicate sets PREDS1 and PREDS2 and returns
1377 true if the domain defined by PREDS1 is a superset
1378 of PREDS2's domain. N1 and N2 are array sizes of PREDS1 and
1379 PREDS2 respectively. The implementation chooses not to build
1380 generic trees (and relying on the folding capability of the
1381 compiler), but instead performs brute force comparison of
1382 individual predicate chains (won't be a compile time problem
1383 as the chains are pretty short). When the function returns
1384 false, it does not necessarily mean *PREDS1 is not a superset
1385 of *PREDS2, but mean it may not be so since the analysis can
1386 not prove it. In such cases, false warnings may still be
1387 emitted. */
1389 static bool
1390 is_superset_of (pred_chain_union preds1, pred_chain_union preds2)
1392 size_t i, n2;
1393 pred_chain one_pred_chain = vNULL;
1395 n2 = preds2.length ();
1397 for (i = 0; i < n2; i++)
1399 one_pred_chain = preds2[i];
1400 if (!is_included_in (one_pred_chain, preds1))
1401 return false;
1404 return true;
1407 /* Returns true if TC is AND or OR. */
1409 static inline bool
1410 is_and_or_or_p (enum tree_code tc, tree type)
1412 return (tc == BIT_IOR_EXPR
1413 || (tc == BIT_AND_EXPR
1414 && (type == 0 || TREE_CODE (type) == BOOLEAN_TYPE)));
1417 /* Returns true if X1 is the negate of X2. */
1419 static inline bool
1420 pred_neg_p (pred_info x1, pred_info x2)
1422 enum tree_code c1, c2;
1423 if (!operand_equal_p (x1.pred_lhs, x2.pred_lhs, 0)
1424 || !operand_equal_p (x1.pred_rhs, x2.pred_rhs, 0))
1425 return false;
1427 c1 = x1.cond_code;
1428 if (x1.invert == x2.invert)
1429 c2 = invert_tree_comparison (x2.cond_code, false);
1430 else
1431 c2 = x2.cond_code;
1433 return c1 == c2;
1436 /* 1) ((x IOR y) != 0) AND (x != 0) is equivalent to (x != 0);
1437 2) (X AND Y) OR (!X AND Y) is equivalent to Y;
1438 3) X OR (!X AND Y) is equivalent to (X OR Y);
1439 4) ((x IAND y) != 0) || (x != 0 AND y != 0)) is equivalent to
1440 (x != 0 AND y != 0)
1441 5) (X AND Y) OR (!X AND Z) OR (!Y AND Z) is equivalent to
1442 (X AND Y) OR Z
1444 PREDS is the predicate chains, and N is the number of chains. */
1446 /* Helper function to implement rule 1 above. ONE_CHAIN is
1447 the AND predication to be simplified. */
1449 static void
1450 simplify_pred (pred_chain *one_chain)
1452 size_t i, j, n;
1453 bool simplified = false;
1454 pred_chain s_chain = vNULL;
1456 n = one_chain->length ();
1458 for (i = 0; i < n; i++)
1460 pred_info *a_pred = &(*one_chain)[i];
1462 if (!a_pred->pred_lhs)
1463 continue;
1464 if (!is_neq_zero_form_p (*a_pred))
1465 continue;
1467 gimple def_stmt = SSA_NAME_DEF_STMT (a_pred->pred_lhs);
1468 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1469 continue;
1470 if (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR)
1472 for (j = 0; j < n; j++)
1474 pred_info *b_pred = &(*one_chain)[j];
1476 if (!b_pred->pred_lhs)
1477 continue;
1478 if (!is_neq_zero_form_p (*b_pred))
1479 continue;
1481 if (pred_expr_equal_p (*b_pred, gimple_assign_rhs1 (def_stmt))
1482 || pred_expr_equal_p (*b_pred, gimple_assign_rhs2 (def_stmt)))
1484 /* Mark a_pred for removal. */
1485 a_pred->pred_lhs = NULL;
1486 a_pred->pred_rhs = NULL;
1487 simplified = true;
1488 break;
1494 if (!simplified)
1495 return;
1497 for (i = 0; i < n; i++)
1499 pred_info *a_pred = &(*one_chain)[i];
1500 if (!a_pred->pred_lhs)
1501 continue;
1502 s_chain.safe_push (*a_pred);
1505 one_chain->release ();
1506 *one_chain = s_chain;
1509 /* The helper function implements the rule 2 for the
1510 OR predicate PREDS.
1512 2) (X AND Y) OR (!X AND Y) is equivalent to Y. */
1514 static bool
1515 simplify_preds_2 (pred_chain_union *preds)
1517 size_t i, j, n;
1518 bool simplified = false;
1519 pred_chain_union s_preds = vNULL;
1521 /* (X AND Y) OR (!X AND Y) is equivalent to Y.
1522 (X AND Y) OR (X AND !Y) is equivalent to X. */
1524 n = preds->length ();
1525 for (i = 0; i < n; i++)
1527 pred_info x, y;
1528 pred_chain *a_chain = &(*preds)[i];
1530 if (a_chain->length () != 2)
1531 continue;
1533 x = (*a_chain)[0];
1534 y = (*a_chain)[1];
1536 for (j = 0; j < n; j++)
1538 pred_chain *b_chain;
1539 pred_info x2, y2;
1541 if (j == i)
1542 continue;
1544 b_chain = &(*preds)[j];
1545 if (b_chain->length () != 2)
1546 continue;
1548 x2 = (*b_chain)[0];
1549 y2 = (*b_chain)[1];
1551 if (pred_equal_p (x, x2) && pred_neg_p (y, y2))
1553 /* Kill a_chain. */
1554 a_chain->release ();
1555 b_chain->release ();
1556 b_chain->safe_push (x);
1557 simplified = true;
1558 break;
1560 if (pred_neg_p (x, x2) && pred_equal_p (y, y2))
1562 /* Kill a_chain. */
1563 a_chain->release ();
1564 b_chain->release ();
1565 b_chain->safe_push (y);
1566 simplified = true;
1567 break;
1571 /* Now clean up the chain. */
1572 if (simplified)
1574 for (i = 0; i < n; i++)
1576 if ((*preds)[i].is_empty ())
1577 continue;
1578 s_preds.safe_push ((*preds)[i]);
1580 preds->release ();
1581 (*preds) = s_preds;
1582 s_preds = vNULL;
1585 return simplified;
1588 /* The helper function implements the rule 2 for the
1589 OR predicate PREDS.
1591 3) x OR (!x AND y) is equivalent to x OR y. */
1593 static bool
1594 simplify_preds_3 (pred_chain_union *preds)
1596 size_t i, j, n;
1597 bool simplified = false;
1599 /* Now iteratively simplify X OR (!X AND Z ..)
1600 into X OR (Z ...). */
1602 n = preds->length ();
1603 if (n < 2)
1604 return false;
1606 for (i = 0; i < n; i++)
1608 pred_info x;
1609 pred_chain *a_chain = &(*preds)[i];
1611 if (a_chain->length () != 1)
1612 continue;
1614 x = (*a_chain)[0];
1616 for (j = 0; j < n; j++)
1618 pred_chain *b_chain;
1619 pred_info x2;
1620 size_t k;
1622 if (j == i)
1623 continue;
1625 b_chain = &(*preds)[j];
1626 if (b_chain->length () < 2)
1627 continue;
1629 for (k = 0; k < b_chain->length (); k++)
1631 x2 = (*b_chain)[k];
1632 if (pred_neg_p (x, x2))
1634 b_chain->unordered_remove (k);
1635 simplified = true;
1636 break;
1641 return simplified;
1644 /* The helper function implements the rule 4 for the
1645 OR predicate PREDS.
1647 2) ((x AND y) != 0) OR (x != 0 AND y != 0) is equivalent to
1648 (x != 0 ANd y != 0). */
1650 static bool
1651 simplify_preds_4 (pred_chain_union *preds)
1653 size_t i, j, n;
1654 bool simplified = false;
1655 pred_chain_union s_preds = vNULL;
1656 gimple def_stmt;
1658 n = preds->length ();
1659 for (i = 0; i < n; i++)
1661 pred_info z;
1662 pred_chain *a_chain = &(*preds)[i];
1664 if (a_chain->length () != 1)
1665 continue;
1667 z = (*a_chain)[0];
1669 if (!is_neq_zero_form_p (z))
1670 continue;
1672 def_stmt = SSA_NAME_DEF_STMT (z.pred_lhs);
1673 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1674 continue;
1676 if (gimple_assign_rhs_code (def_stmt) != BIT_AND_EXPR)
1677 continue;
1679 for (j = 0; j < n; j++)
1681 pred_chain *b_chain;
1682 pred_info x2, y2;
1684 if (j == i)
1685 continue;
1687 b_chain = &(*preds)[j];
1688 if (b_chain->length () != 2)
1689 continue;
1691 x2 = (*b_chain)[0];
1692 y2 = (*b_chain)[1];
1693 if (!is_neq_zero_form_p (x2)
1694 || !is_neq_zero_form_p (y2))
1695 continue;
1697 if ((pred_expr_equal_p (x2, gimple_assign_rhs1 (def_stmt))
1698 && pred_expr_equal_p (y2, gimple_assign_rhs2 (def_stmt)))
1699 || (pred_expr_equal_p (x2, gimple_assign_rhs2 (def_stmt))
1700 && pred_expr_equal_p (y2, gimple_assign_rhs1 (def_stmt))))
1702 /* Kill a_chain. */
1703 a_chain->release ();
1704 simplified = true;
1705 break;
1709 /* Now clean up the chain. */
1710 if (simplified)
1712 for (i = 0; i < n; i++)
1714 if ((*preds)[i].is_empty ())
1715 continue;
1716 s_preds.safe_push ((*preds)[i]);
1718 preds->release ();
1719 (*preds) = s_preds;
1720 s_preds = vNULL;
1723 return simplified;
1727 /* This function simplifies predicates in PREDS. */
1729 static void
1730 simplify_preds (pred_chain_union *preds, gimple use_or_def, bool is_use)
1732 size_t i, n;
1733 bool changed = false;
1735 if (dump_file && dump_flags & TDF_DETAILS)
1737 fprintf (dump_file, "[BEFORE SIMPLICATION -- ");
1738 dump_predicates (use_or_def, *preds, is_use ? "[USE]:\n" : "[DEF]:\n");
1741 for (i = 0; i < preds->length (); i++)
1742 simplify_pred (&(*preds)[i]);
1744 n = preds->length ();
1745 if (n < 2)
1746 return;
1750 changed = false;
1751 if (simplify_preds_2 (preds))
1752 changed = true;
1754 /* Now iteratively simplify X OR (!X AND Z ..)
1755 into X OR (Z ...). */
1756 if (simplify_preds_3 (preds))
1757 changed = true;
1759 if (simplify_preds_4 (preds))
1760 changed = true;
1762 } while (changed);
1764 return;
1767 /* This is a helper function which attempts to normalize predicate chains
1768 by following UD chains. It basically builds up a big tree of either IOR
1769 operations or AND operations, and convert the IOR tree into a
1770 pred_chain_union or BIT_AND tree into a pred_chain.
1771 Example:
1773 _3 = _2 RELOP1 _1;
1774 _6 = _5 RELOP2 _4;
1775 _9 = _8 RELOP3 _7;
1776 _10 = _3 | _6;
1777 _12 = _9 | _0;
1778 _t = _10 | _12;
1780 then _t != 0 will be normalized into a pred_chain_union
1782 (_2 RELOP1 _1) OR (_5 RELOP2 _4) OR (_8 RELOP3 _7) OR (_0 != 0)
1784 Similarly given,
1786 _3 = _2 RELOP1 _1;
1787 _6 = _5 RELOP2 _4;
1788 _9 = _8 RELOP3 _7;
1789 _10 = _3 & _6;
1790 _12 = _9 & _0;
1792 then _t != 0 will be normalized into a pred_chain:
1793 (_2 RELOP1 _1) AND (_5 RELOP2 _4) AND (_8 RELOP3 _7) AND (_0 != 0)
1797 /* This is a helper function that stores a PRED into NORM_PREDS. */
1799 inline static void
1800 push_pred (pred_chain_union *norm_preds, pred_info pred)
1802 pred_chain pred_chain = vNULL;
1803 pred_chain.safe_push (pred);
1804 norm_preds->safe_push (pred_chain);
1807 /* A helper function that creates a predicate of the form
1808 OP != 0 and push it WORK_LIST. */
1810 inline static void
1811 push_to_worklist (tree op, vec<pred_info, va_heap, vl_ptr> *work_list,
1812 pointer_set_t *mark_set)
1814 if (pointer_set_contains (mark_set, op))
1815 return;
1816 pointer_set_insert (mark_set, op);
1818 pred_info arg_pred;
1819 arg_pred.pred_lhs = op;
1820 arg_pred.pred_rhs = integer_zero_node;
1821 arg_pred.cond_code = NE_EXPR;
1822 arg_pred.invert = false;
1823 work_list->safe_push (arg_pred);
1826 /* A helper that generates a pred_info from a gimple assignment
1827 CMP_ASSIGN with comparison rhs. */
1829 static pred_info
1830 get_pred_info_from_cmp (gimple cmp_assign)
1832 pred_info n_pred;
1833 n_pred.pred_lhs = gimple_assign_rhs1 (cmp_assign);
1834 n_pred.pred_rhs = gimple_assign_rhs2 (cmp_assign);
1835 n_pred.cond_code = gimple_assign_rhs_code (cmp_assign);
1836 n_pred.invert = false;
1837 return n_pred;
1840 /* Returns true if the PHI is a degenerated phi with
1841 all args with the same value (relop). In that case, *PRED
1842 will be updated to that value. */
1844 static bool
1845 is_degenerated_phi (gimple phi, pred_info *pred_p)
1847 int i, n;
1848 tree op0;
1849 gimple def0;
1850 pred_info pred0;
1852 n = gimple_phi_num_args (phi);
1853 op0 = gimple_phi_arg_def (phi, 0);
1855 if (TREE_CODE (op0) != SSA_NAME)
1856 return false;
1858 def0 = SSA_NAME_DEF_STMT (op0);
1859 if (gimple_code (def0) != GIMPLE_ASSIGN)
1860 return false;
1861 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def0))
1862 != tcc_comparison)
1863 return false;
1864 pred0 = get_pred_info_from_cmp (def0);
1866 for (i = 1; i < n; ++i)
1868 gimple def;
1869 pred_info pred;
1870 tree op = gimple_phi_arg_def (phi, i);
1872 if (TREE_CODE (op) != SSA_NAME)
1873 return false;
1875 def = SSA_NAME_DEF_STMT (op);
1876 if (gimple_code (def) != GIMPLE_ASSIGN)
1877 return false;
1878 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def))
1879 != tcc_comparison)
1880 return false;
1881 pred = get_pred_info_from_cmp (def);
1882 if (!pred_equal_p (pred, pred0))
1883 return false;
1886 *pred_p = pred0;
1887 return true;
1890 /* Normalize one predicate PRED
1891 1) if PRED can no longer be normlized, put it into NORM_PREDS.
1892 2) otherwise if PRED is of the form x != 0, follow x's definition
1893 and put normalized predicates into WORK_LIST. */
1895 static void
1896 normalize_one_pred_1 (pred_chain_union *norm_preds,
1897 pred_chain *norm_chain,
1898 pred_info pred,
1899 enum tree_code and_or_code,
1900 vec<pred_info, va_heap, vl_ptr> *work_list,
1901 pointer_set_t *mark_set)
1903 if (!is_neq_zero_form_p (pred))
1905 if (and_or_code == BIT_IOR_EXPR)
1906 push_pred (norm_preds, pred);
1907 else
1908 norm_chain->safe_push (pred);
1909 return;
1912 gimple def_stmt = SSA_NAME_DEF_STMT (pred.pred_lhs);
1914 if (gimple_code (def_stmt) == GIMPLE_PHI
1915 && is_degenerated_phi (def_stmt, &pred))
1916 work_list->safe_push (pred);
1917 else if (gimple_code (def_stmt) == GIMPLE_PHI
1918 && and_or_code == BIT_IOR_EXPR)
1920 int i, n;
1921 n = gimple_phi_num_args (def_stmt);
1923 /* If we see non zero constant, we should punt. The predicate
1924 * should be one guarding the phi edge. */
1925 for (i = 0; i < n; ++i)
1927 tree op = gimple_phi_arg_def (def_stmt, i);
1928 if (TREE_CODE (op) == INTEGER_CST && !integer_zerop (op))
1930 push_pred (norm_preds, pred);
1931 return;
1935 for (i = 0; i < n; ++i)
1937 tree op = gimple_phi_arg_def (def_stmt, i);
1938 if (integer_zerop (op))
1939 continue;
1941 push_to_worklist (op, work_list, mark_set);
1944 else if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1946 if (and_or_code == BIT_IOR_EXPR)
1947 push_pred (norm_preds, pred);
1948 else
1949 norm_chain->safe_push (pred);
1951 else if (gimple_assign_rhs_code (def_stmt) == and_or_code)
1953 push_to_worklist (gimple_assign_rhs1 (def_stmt), work_list, mark_set);
1954 push_to_worklist (gimple_assign_rhs2 (def_stmt), work_list, mark_set);
1956 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt))
1957 == tcc_comparison)
1959 pred_info n_pred = get_pred_info_from_cmp (def_stmt);
1960 if (and_or_code == BIT_IOR_EXPR)
1961 push_pred (norm_preds, n_pred);
1962 else
1963 norm_chain->safe_push (n_pred);
1965 else
1967 if (and_or_code == BIT_IOR_EXPR)
1968 push_pred (norm_preds, pred);
1969 else
1970 norm_chain->safe_push (pred);
1974 /* Normalize PRED and store the normalized predicates into NORM_PREDS. */
1976 static void
1977 normalize_one_pred (pred_chain_union *norm_preds,
1978 pred_info pred)
1980 vec<pred_info, va_heap, vl_ptr> work_list = vNULL;
1981 pointer_set_t *mark_set = NULL;
1982 enum tree_code and_or_code = ERROR_MARK;
1983 pred_chain norm_chain = vNULL;
1985 if (!is_neq_zero_form_p (pred))
1987 push_pred (norm_preds, pred);
1988 return;
1991 gimple def_stmt = SSA_NAME_DEF_STMT (pred.pred_lhs);
1992 if (gimple_code (def_stmt) == GIMPLE_ASSIGN)
1993 and_or_code = gimple_assign_rhs_code (def_stmt);
1994 if (and_or_code != BIT_IOR_EXPR
1995 && and_or_code != BIT_AND_EXPR)
1997 if (TREE_CODE_CLASS (and_or_code)
1998 == tcc_comparison)
2000 pred_info n_pred = get_pred_info_from_cmp (def_stmt);
2001 push_pred (norm_preds, n_pred);
2003 else
2004 push_pred (norm_preds, pred);
2005 return;
2008 work_list.safe_push (pred);
2009 mark_set = pointer_set_create ();
2011 while (!work_list.is_empty ())
2013 pred_info a_pred = work_list.pop ();
2014 normalize_one_pred_1 (norm_preds, &norm_chain, a_pred,
2015 and_or_code, &work_list, mark_set);
2017 if (and_or_code == BIT_AND_EXPR)
2018 norm_preds->safe_push (norm_chain);
2020 work_list.release ();
2021 pointer_set_destroy (mark_set);
2024 static void
2025 normalize_one_pred_chain (pred_chain_union *norm_preds,
2026 pred_chain one_chain)
2028 vec<pred_info, va_heap, vl_ptr> work_list = vNULL;
2029 pointer_set_t *mark_set = pointer_set_create ();
2030 pred_chain norm_chain = vNULL;
2031 size_t i;
2033 for (i = 0; i < one_chain.length (); i++)
2035 work_list.safe_push (one_chain[i]);
2036 pointer_set_insert (mark_set, one_chain[i].pred_lhs);
2039 while (!work_list.is_empty ())
2041 pred_info a_pred = work_list.pop ();
2042 normalize_one_pred_1 (0, &norm_chain, a_pred,
2043 BIT_AND_EXPR, &work_list, mark_set);
2046 norm_preds->safe_push (norm_chain);
2047 work_list.release ();
2048 pointer_set_destroy (mark_set);
2051 /* Normalize predicate chains PREDS and returns the normalized one. */
2053 static pred_chain_union
2054 normalize_preds (pred_chain_union preds, gimple use_or_def, bool is_use)
2056 pred_chain_union norm_preds = vNULL;
2057 size_t n = preds.length ();
2058 size_t i;
2060 if (dump_file && dump_flags & TDF_DETAILS)
2062 fprintf (dump_file, "[BEFORE NORMALIZATION --");
2063 dump_predicates (use_or_def, preds, is_use ? "[USE]:\n" : "[DEF]:\n");
2066 for (i = 0; i < n; i++)
2068 if (preds[i].length () != 1)
2069 normalize_one_pred_chain (&norm_preds, preds[i]);
2070 else
2072 normalize_one_pred (&norm_preds, preds[i][0]);
2073 preds[i].release ();
2077 if (dump_file)
2079 fprintf (dump_file, "[AFTER NORMALIZATION -- ");
2080 dump_predicates (use_or_def, norm_preds, is_use ? "[USE]:\n" : "[DEF]:\n");
2083 preds.release ();
2084 return norm_preds;
2088 /* Computes the predicates that guard the use and checks
2089 if the incoming paths that have empty (or possibly
2090 empty) definition can be pruned/filtered. The function returns
2091 true if it can be determined that the use of PHI's def in
2092 USE_STMT is guarded with a predicate set not overlapping with
2093 predicate sets of all runtime paths that do not have a definition.
2094 Returns false if it is not or it can not be determined. USE_BB is
2095 the bb of the use (for phi operand use, the bb is not the bb of
2096 the phi stmt, but the src bb of the operand edge). UNINIT_OPNDS
2097 is a bit vector. If an operand of PHI is uninitialized, the
2098 corresponding bit in the vector is 1. VISIED_PHIS is a pointer
2099 set of phis being visted. */
2101 static bool
2102 is_use_properly_guarded (gimple use_stmt,
2103 basic_block use_bb,
2104 gimple phi,
2105 unsigned uninit_opnds,
2106 pointer_set_t *visited_phis)
2108 basic_block phi_bb;
2109 pred_chain_union preds = vNULL;
2110 pred_chain_union def_preds = vNULL;
2111 bool has_valid_preds = false;
2112 bool is_properly_guarded = false;
2114 if (pointer_set_insert (visited_phis, phi))
2115 return false;
2117 phi_bb = gimple_bb (phi);
2119 if (is_non_loop_exit_postdominating (use_bb, phi_bb))
2120 return false;
2122 has_valid_preds = find_predicates (&preds, phi_bb, use_bb);
2124 if (!has_valid_preds)
2126 destroy_predicate_vecs (preds);
2127 return false;
2130 /* Try to prune the dead incoming phi edges. */
2131 is_properly_guarded
2132 = use_pred_not_overlap_with_undef_path_pred (preds, phi, uninit_opnds,
2133 visited_phis);
2135 if (is_properly_guarded)
2137 destroy_predicate_vecs (preds);
2138 return true;
2141 has_valid_preds = find_def_preds (&def_preds, phi);
2143 if (!has_valid_preds)
2145 destroy_predicate_vecs (preds);
2146 destroy_predicate_vecs (def_preds);
2147 return false;
2150 simplify_preds (&preds, use_stmt, true);
2151 preds = normalize_preds (preds, use_stmt, true);
2153 simplify_preds (&def_preds, phi, false);
2154 def_preds = normalize_preds (def_preds, phi, false);
2156 is_properly_guarded = is_superset_of (def_preds, preds);
2158 destroy_predicate_vecs (preds);
2159 destroy_predicate_vecs (def_preds);
2160 return is_properly_guarded;
2163 /* Searches through all uses of a potentially
2164 uninitialized variable defined by PHI and returns a use
2165 statement if the use is not properly guarded. It returns
2166 NULL if all uses are guarded. UNINIT_OPNDS is a bitvector
2167 holding the position(s) of uninit PHI operands. WORKLIST
2168 is the vector of candidate phis that may be updated by this
2169 function. ADDED_TO_WORKLIST is the pointer set tracking
2170 if the new phi is already in the worklist. */
2172 static gimple
2173 find_uninit_use (gimple phi, unsigned uninit_opnds,
2174 vec<gimple> *worklist,
2175 pointer_set_t *added_to_worklist)
2177 tree phi_result;
2178 use_operand_p use_p;
2179 gimple use_stmt;
2180 imm_use_iterator iter;
2182 phi_result = gimple_phi_result (phi);
2184 FOR_EACH_IMM_USE_FAST (use_p, iter, phi_result)
2186 pointer_set_t *visited_phis;
2187 basic_block use_bb;
2189 use_stmt = USE_STMT (use_p);
2190 if (is_gimple_debug (use_stmt))
2191 continue;
2193 visited_phis = pointer_set_create ();
2195 if (gimple_code (use_stmt) == GIMPLE_PHI)
2196 use_bb = gimple_phi_arg_edge (use_stmt,
2197 PHI_ARG_INDEX_FROM_USE (use_p))->src;
2198 else
2199 use_bb = gimple_bb (use_stmt);
2201 if (is_use_properly_guarded (use_stmt, use_bb, phi, uninit_opnds,
2202 visited_phis))
2204 pointer_set_destroy (visited_phis);
2205 continue;
2207 pointer_set_destroy (visited_phis);
2209 if (dump_file && (dump_flags & TDF_DETAILS))
2211 fprintf (dump_file, "[CHECK]: Found unguarded use: ");
2212 print_gimple_stmt (dump_file, use_stmt, 0, 0);
2214 /* Found one real use, return. */
2215 if (gimple_code (use_stmt) != GIMPLE_PHI)
2216 return use_stmt;
2218 /* Found a phi use that is not guarded,
2219 add the phi to the worklist. */
2220 if (!pointer_set_insert (added_to_worklist, use_stmt))
2222 if (dump_file && (dump_flags & TDF_DETAILS))
2224 fprintf (dump_file, "[WORKLIST]: Update worklist with phi: ");
2225 print_gimple_stmt (dump_file, use_stmt, 0, 0);
2228 worklist->safe_push (use_stmt);
2229 pointer_set_insert (possibly_undefined_names, phi_result);
2233 return NULL;
2236 /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions
2237 and gives warning if there exists a runtime path from the entry to a
2238 use of the PHI def that does not contain a definition. In other words,
2239 the warning is on the real use. The more dead paths that can be pruned
2240 by the compiler, the fewer false positives the warning is. WORKLIST
2241 is a vector of candidate phis to be examined. ADDED_TO_WORKLIST is
2242 a pointer set tracking if the new phi is added to the worklist or not. */
2244 static void
2245 warn_uninitialized_phi (gimple phi, vec<gimple> *worklist,
2246 pointer_set_t *added_to_worklist)
2248 unsigned uninit_opnds;
2249 gimple uninit_use_stmt = 0;
2250 tree uninit_op;
2252 /* Don't look at virtual operands. */
2253 if (virtual_operand_p (gimple_phi_result (phi)))
2254 return;
2256 uninit_opnds = compute_uninit_opnds_pos (phi);
2258 if (MASK_EMPTY (uninit_opnds))
2259 return;
2261 if (dump_file && (dump_flags & TDF_DETAILS))
2263 fprintf (dump_file, "[CHECK]: examining phi: ");
2264 print_gimple_stmt (dump_file, phi, 0, 0);
2267 /* Now check if we have any use of the value without proper guard. */
2268 uninit_use_stmt = find_uninit_use (phi, uninit_opnds,
2269 worklist, added_to_worklist);
2271 /* All uses are properly guarded. */
2272 if (!uninit_use_stmt)
2273 return;
2275 uninit_op = gimple_phi_arg_def (phi, MASK_FIRST_SET_BIT (uninit_opnds));
2276 if (SSA_NAME_VAR (uninit_op) == NULL_TREE)
2277 return;
2278 warn_uninit (OPT_Wmaybe_uninitialized, uninit_op, SSA_NAME_VAR (uninit_op),
2279 SSA_NAME_VAR (uninit_op),
2280 "%qD may be used uninitialized in this function",
2281 uninit_use_stmt);
2286 /* Entry point to the late uninitialized warning pass. */
2288 static unsigned int
2289 execute_late_warn_uninitialized (void)
2291 basic_block bb;
2292 gimple_stmt_iterator gsi;
2293 vec<gimple> worklist = vNULL;
2294 pointer_set_t *added_to_worklist;
2296 calculate_dominance_info (CDI_DOMINATORS);
2297 calculate_dominance_info (CDI_POST_DOMINATORS);
2298 /* Re-do the plain uninitialized variable check, as optimization may have
2299 straightened control flow. Do this first so that we don't accidentally
2300 get a "may be" warning when we'd have seen an "is" warning later. */
2301 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/1);
2303 timevar_push (TV_TREE_UNINIT);
2305 possibly_undefined_names = pointer_set_create ();
2306 added_to_worklist = pointer_set_create ();
2308 /* Initialize worklist */
2309 FOR_EACH_BB_FN (bb, cfun)
2310 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2312 gimple phi = gsi_stmt (gsi);
2313 size_t n, i;
2315 n = gimple_phi_num_args (phi);
2317 /* Don't look at virtual operands. */
2318 if (virtual_operand_p (gimple_phi_result (phi)))
2319 continue;
2321 for (i = 0; i < n; ++i)
2323 tree op = gimple_phi_arg_def (phi, i);
2324 if (TREE_CODE (op) == SSA_NAME
2325 && uninit_undefined_value_p (op))
2327 worklist.safe_push (phi);
2328 pointer_set_insert (added_to_worklist, phi);
2329 if (dump_file && (dump_flags & TDF_DETAILS))
2331 fprintf (dump_file, "[WORKLIST]: add to initial list: ");
2332 print_gimple_stmt (dump_file, phi, 0, 0);
2334 break;
2339 while (worklist.length () != 0)
2341 gimple cur_phi = 0;
2342 cur_phi = worklist.pop ();
2343 warn_uninitialized_phi (cur_phi, &worklist, added_to_worklist);
2346 worklist.release ();
2347 pointer_set_destroy (added_to_worklist);
2348 pointer_set_destroy (possibly_undefined_names);
2349 possibly_undefined_names = NULL;
2350 free_dominance_info (CDI_POST_DOMINATORS);
2351 timevar_pop (TV_TREE_UNINIT);
2352 return 0;
2355 static bool
2356 gate_warn_uninitialized (void)
2358 return warn_uninitialized || warn_maybe_uninitialized;
2361 namespace {
2363 const pass_data pass_data_late_warn_uninitialized =
2365 GIMPLE_PASS, /* type */
2366 "uninit", /* name */
2367 OPTGROUP_NONE, /* optinfo_flags */
2368 true, /* has_gate */
2369 true, /* has_execute */
2370 TV_NONE, /* tv_id */
2371 PROP_ssa, /* properties_required */
2372 0, /* properties_provided */
2373 0, /* properties_destroyed */
2374 0, /* todo_flags_start */
2375 0, /* todo_flags_finish */
2378 class pass_late_warn_uninitialized : public gimple_opt_pass
2380 public:
2381 pass_late_warn_uninitialized (gcc::context *ctxt)
2382 : gimple_opt_pass (pass_data_late_warn_uninitialized, ctxt)
2385 /* opt_pass methods: */
2386 opt_pass * clone () { return new pass_late_warn_uninitialized (m_ctxt); }
2387 bool gate () { return gate_warn_uninitialized (); }
2388 unsigned int execute () { return execute_late_warn_uninitialized (); }
2390 }; // class pass_late_warn_uninitialized
2392 } // anon namespace
2394 gimple_opt_pass *
2395 make_pass_late_warn_uninitialized (gcc::context *ctxt)
2397 return new pass_late_warn_uninitialized (ctxt);
2401 static unsigned int
2402 execute_early_warn_uninitialized (void)
2404 /* Currently, this pass runs always but
2405 execute_late_warn_uninitialized only runs with optimization. With
2406 optimization we want to warn about possible uninitialized as late
2407 as possible, thus don't do it here. However, without
2408 optimization we need to warn here about "may be uninitialized". */
2409 calculate_dominance_info (CDI_POST_DOMINATORS);
2411 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/!optimize);
2413 /* Post-dominator information can not be reliably updated. Free it
2414 after the use. */
2416 free_dominance_info (CDI_POST_DOMINATORS);
2417 return 0;
2421 namespace {
2423 const pass_data pass_data_early_warn_uninitialized =
2425 GIMPLE_PASS, /* type */
2426 "*early_warn_uninitialized", /* name */
2427 OPTGROUP_NONE, /* optinfo_flags */
2428 true, /* has_gate */
2429 true, /* has_execute */
2430 TV_TREE_UNINIT, /* tv_id */
2431 PROP_ssa, /* properties_required */
2432 0, /* properties_provided */
2433 0, /* properties_destroyed */
2434 0, /* todo_flags_start */
2435 0, /* todo_flags_finish */
2438 class pass_early_warn_uninitialized : public gimple_opt_pass
2440 public:
2441 pass_early_warn_uninitialized (gcc::context *ctxt)
2442 : gimple_opt_pass (pass_data_early_warn_uninitialized, ctxt)
2445 /* opt_pass methods: */
2446 bool gate () { return gate_warn_uninitialized (); }
2447 unsigned int execute () { 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);