2016-06-16 Hristian Kirtchev <kirtchev@adacore.com>
[official-gcc.git] / gcc / tree-ssa-uninit.c
blob941d575f848d11e95bae9d96df5cdd8f5dbbfca2
1 /* Predicate aware uninitialized variable warning.
2 Copyright (C) 2001-2016 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 "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "tree-pass.h"
28 #include "ssa.h"
29 #include "gimple-pretty-print.h"
30 #include "diagnostic-core.h"
31 #include "fold-const.h"
32 #include "gimple-iterator.h"
33 #include "tree-ssa.h"
34 #include "params.h"
35 #include "tree-cfg.h"
37 /* This implements the pass that does predicate aware warning on uses of
38 possibly uninitialized variables. The pass first collects the set of
39 possibly uninitialized SSA names. For each such name, it walks through
40 all its immediate uses. For each immediate use, it rebuilds the condition
41 expression (the predicate) that guards the use. The predicate is then
42 examined to see if the variable is always defined under that same condition.
43 This is done either by pruning the unrealizable paths that lead to the
44 default definitions or by checking if the predicate set that guards the
45 defining paths is a superset of the use predicate. */
47 /* Pointer set of potentially undefined ssa names, i.e.,
48 ssa names that are defined by phi with operands that
49 are not defined or potentially undefined. */
50 static hash_set<tree> *possibly_undefined_names = 0;
52 /* Bit mask handling macros. */
53 #define MASK_SET_BIT(mask, pos) mask |= (1 << pos)
54 #define MASK_TEST_BIT(mask, pos) (mask & (1 << pos))
55 #define MASK_EMPTY(mask) (mask == 0)
57 /* Returns the first bit position (starting from LSB)
58 in mask that is non zero. Returns -1 if the mask is empty. */
59 static int
60 get_mask_first_set_bit (unsigned mask)
62 int pos = 0;
63 if (mask == 0)
64 return -1;
66 while ((mask & (1 << pos)) == 0)
67 pos++;
69 return pos;
71 #define MASK_FIRST_SET_BIT(mask) get_mask_first_set_bit (mask)
73 /* Return true if T, an SSA_NAME, has an undefined value. */
74 static bool
75 has_undefined_value_p (tree t)
77 return (ssa_undefined_value_p (t)
78 || (possibly_undefined_names
79 && possibly_undefined_names->contains (t)));
82 /* Like has_undefined_value_p, but don't return true if TREE_NO_WARNING
83 is set on SSA_NAME_VAR. */
85 static inline bool
86 uninit_undefined_value_p (tree t)
88 if (!has_undefined_value_p (t))
89 return false;
90 if (SSA_NAME_VAR (t) && TREE_NO_WARNING (SSA_NAME_VAR (t)))
91 return false;
92 return true;
95 /* Emit warnings for uninitialized variables. This is done in two passes.
97 The first pass notices real uses of SSA names with undefined values.
98 Such uses are unconditionally uninitialized, and we can be certain that
99 such a use is a mistake. This pass is run before most optimizations,
100 so that we catch as many as we can.
102 The second pass follows PHI nodes to find uses that are potentially
103 uninitialized. In this case we can't necessarily prove that the use
104 is really uninitialized. This pass is run after most optimizations,
105 so that we thread as many jumps and possible, and delete as much dead
106 code as possible, in order to reduce false positives. We also look
107 again for plain uninitialized variables, since optimization may have
108 changed conditionally uninitialized to unconditionally uninitialized. */
110 /* Emit a warning for EXPR based on variable VAR at the point in the
111 program T, an SSA_NAME, is used being uninitialized. The exact
112 warning text is in MSGID and DATA is the gimple stmt with info about
113 the location in source code. When DATA is a GIMPLE_PHI, PHIARG_IDX
114 gives which argument of the phi node to take the location from. WC
115 is the warning code. */
117 static void
118 warn_uninit (enum opt_code wc, tree t, tree expr, tree var,
119 const char *gmsgid, void *data, location_t phiarg_loc)
121 gimple *context = (gimple *) data;
122 location_t location, cfun_loc;
123 expanded_location xloc, floc;
125 /* Ignore COMPLEX_EXPR as initializing only a part of a complex
126 turns in a COMPLEX_EXPR with the not initialized part being
127 set to its previous (undefined) value. */
128 if (is_gimple_assign (context)
129 && gimple_assign_rhs_code (context) == COMPLEX_EXPR)
130 return;
131 if (!has_undefined_value_p (t))
132 return;
134 /* TREE_NO_WARNING either means we already warned, or the front end
135 wishes to suppress the warning. */
136 if ((context
137 && (gimple_no_warning_p (context)
138 || (gimple_assign_single_p (context)
139 && TREE_NO_WARNING (gimple_assign_rhs1 (context)))))
140 || TREE_NO_WARNING (expr))
141 return;
143 if (context != NULL && gimple_has_location (context))
144 location = gimple_location (context);
145 else if (phiarg_loc != UNKNOWN_LOCATION)
146 location = phiarg_loc;
147 else
148 location = DECL_SOURCE_LOCATION (var);
149 location = linemap_resolve_location (line_table, location,
150 LRK_SPELLING_LOCATION, NULL);
151 cfun_loc = DECL_SOURCE_LOCATION (cfun->decl);
152 xloc = expand_location (location);
153 floc = expand_location (cfun_loc);
154 if (warning_at (location, wc, gmsgid, expr))
156 TREE_NO_WARNING (expr) = 1;
158 if (location == DECL_SOURCE_LOCATION (var))
159 return;
160 if (xloc.file != floc.file
161 || linemap_location_before_p (line_table, location, cfun_loc)
162 || linemap_location_before_p (line_table, cfun->function_end_locus,
163 location))
164 inform (DECL_SOURCE_LOCATION (var), "%qD was declared here", var);
168 static unsigned int
169 warn_uninitialized_vars (bool warn_possibly_uninitialized)
171 gimple_stmt_iterator gsi;
172 basic_block bb;
174 FOR_EACH_BB_FN (bb, cfun)
176 basic_block succ = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
177 bool always_executed = dominated_by_p (CDI_POST_DOMINATORS, succ, bb);
178 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
180 gimple *stmt = gsi_stmt (gsi);
181 use_operand_p use_p;
182 ssa_op_iter op_iter;
183 tree use;
185 if (is_gimple_debug (stmt))
186 continue;
188 /* We only do data flow with SSA_NAMEs, so that's all we
189 can warn about. */
190 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, op_iter, SSA_OP_USE)
192 use = USE_FROM_PTR (use_p);
193 if (always_executed)
194 warn_uninit (OPT_Wuninitialized, use, SSA_NAME_VAR (use),
195 SSA_NAME_VAR (use),
196 "%qD is used uninitialized in this function", stmt,
197 UNKNOWN_LOCATION);
198 else if (warn_possibly_uninitialized)
199 warn_uninit (OPT_Wmaybe_uninitialized, use, SSA_NAME_VAR (use),
200 SSA_NAME_VAR (use),
201 "%qD may be used uninitialized in this function",
202 stmt, UNKNOWN_LOCATION);
205 /* For memory the only cheap thing we can do is see if we
206 have a use of the default def of the virtual operand.
207 ??? Not so cheap would be to use the alias oracle via
208 walk_aliased_vdefs, if we don't find any aliasing vdef
209 warn as is-used-uninitialized, if we don't find an aliasing
210 vdef that kills our use (stmt_kills_ref_p), warn as
211 may-be-used-uninitialized. But this walk is quadratic and
212 so must be limited which means we would miss warning
213 opportunities. */
214 use = gimple_vuse (stmt);
215 if (use
216 && gimple_assign_single_p (stmt)
217 && !gimple_vdef (stmt)
218 && SSA_NAME_IS_DEFAULT_DEF (use))
220 tree rhs = gimple_assign_rhs1 (stmt);
221 tree base = get_base_address (rhs);
223 /* Do not warn if it can be initialized outside this function. */
224 if (TREE_CODE (base) != VAR_DECL
225 || DECL_HARD_REGISTER (base)
226 || is_global_var (base))
227 continue;
229 if (always_executed)
230 warn_uninit (OPT_Wuninitialized, use, gimple_assign_rhs1 (stmt),
231 base, "%qE is used uninitialized in this function",
232 stmt, UNKNOWN_LOCATION);
233 else if (warn_possibly_uninitialized)
234 warn_uninit (OPT_Wmaybe_uninitialized, use,
235 gimple_assign_rhs1 (stmt), base,
236 "%qE may be used uninitialized in this function",
237 stmt, UNKNOWN_LOCATION);
242 return 0;
245 /* Checks if the operand OPND of PHI is defined by
246 another phi with one operand defined by this PHI,
247 but the rest operands are all defined. If yes,
248 returns true to skip this operand as being
249 redundant. Can be enhanced to be more general. */
251 static bool
252 can_skip_redundant_opnd (tree opnd, gimple *phi)
254 gimple *op_def;
255 tree phi_def;
256 int i, n;
258 phi_def = gimple_phi_result (phi);
259 op_def = SSA_NAME_DEF_STMT (opnd);
260 if (gimple_code (op_def) != GIMPLE_PHI)
261 return false;
262 n = gimple_phi_num_args (op_def);
263 for (i = 0; i < n; ++i)
265 tree op = gimple_phi_arg_def (op_def, i);
266 if (TREE_CODE (op) != SSA_NAME)
267 continue;
268 if (op != phi_def && uninit_undefined_value_p (op))
269 return false;
272 return true;
275 /* Returns a bit mask holding the positions of arguments in PHI
276 that have empty (or possibly empty) definitions. */
278 static unsigned
279 compute_uninit_opnds_pos (gphi *phi)
281 size_t i, n;
282 unsigned uninit_opnds = 0;
284 n = gimple_phi_num_args (phi);
285 /* Bail out for phi with too many args. */
286 if (n > 32)
287 return 0;
289 for (i = 0; i < n; ++i)
291 tree op = gimple_phi_arg_def (phi, i);
292 if (TREE_CODE (op) == SSA_NAME
293 && uninit_undefined_value_p (op)
294 && !can_skip_redundant_opnd (op, phi))
296 if (cfun->has_nonlocal_label || cfun->calls_setjmp)
298 /* Ignore SSA_NAMEs that appear on abnormal edges
299 somewhere. */
300 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
301 continue;
303 MASK_SET_BIT (uninit_opnds, i);
306 return uninit_opnds;
309 /* Find the immediate postdominator PDOM of the specified
310 basic block BLOCK. */
312 static inline basic_block
313 find_pdom (basic_block block)
315 if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
316 return EXIT_BLOCK_PTR_FOR_FN (cfun);
317 else
319 basic_block bb = get_immediate_dominator (CDI_POST_DOMINATORS, block);
320 if (!bb)
321 return EXIT_BLOCK_PTR_FOR_FN (cfun);
322 return bb;
326 /* Find the immediate DOM of the specified basic block BLOCK. */
328 static inline basic_block
329 find_dom (basic_block block)
331 if (block == ENTRY_BLOCK_PTR_FOR_FN (cfun))
332 return ENTRY_BLOCK_PTR_FOR_FN (cfun);
333 else
335 basic_block bb = get_immediate_dominator (CDI_DOMINATORS, block);
336 if (!bb)
337 return ENTRY_BLOCK_PTR_FOR_FN (cfun);
338 return bb;
342 /* Returns true if BB1 is postdominating BB2 and BB1 is
343 not a loop exit bb. The loop exit bb check is simple and does
344 not cover all cases. */
346 static bool
347 is_non_loop_exit_postdominating (basic_block bb1, basic_block bb2)
349 if (!dominated_by_p (CDI_POST_DOMINATORS, bb2, bb1))
350 return false;
352 if (single_pred_p (bb1) && !single_succ_p (bb2))
353 return false;
355 return true;
358 /* Find the closest postdominator of a specified BB, which is control
359 equivalent to BB. */
361 static inline basic_block
362 find_control_equiv_block (basic_block bb)
364 basic_block pdom;
366 pdom = find_pdom (bb);
368 /* Skip the postdominating bb that is also loop exit. */
369 if (!is_non_loop_exit_postdominating (pdom, bb))
370 return NULL;
372 if (dominated_by_p (CDI_DOMINATORS, pdom, bb))
373 return pdom;
375 return NULL;
378 #define MAX_NUM_CHAINS 8
379 #define MAX_CHAIN_LEN 5
380 #define MAX_POSTDOM_CHECK 8
381 #define MAX_SWITCH_CASES 40
383 /* Computes the control dependence chains (paths of edges)
384 for DEP_BB up to the dominating basic block BB (the head node of a
385 chain should be dominated by it). CD_CHAINS is pointer to an
386 array holding the result chains. CUR_CD_CHAIN is the current
387 chain being computed. *NUM_CHAINS is total number of chains. The
388 function returns true if the information is successfully computed,
389 return false if there is no control dependence or not computed. */
391 static bool
392 compute_control_dep_chain (basic_block bb, basic_block dep_bb,
393 vec<edge> *cd_chains,
394 size_t *num_chains,
395 vec<edge> *cur_cd_chain,
396 int *num_calls)
398 edge_iterator ei;
399 edge e;
400 size_t i;
401 bool found_cd_chain = false;
402 size_t cur_chain_len = 0;
404 if (EDGE_COUNT (bb->succs) < 2)
405 return false;
407 if (*num_calls > PARAM_VALUE (PARAM_UNINIT_CONTROL_DEP_ATTEMPTS))
408 return false;
409 ++*num_calls;
411 /* Could use a set instead. */
412 cur_chain_len = cur_cd_chain->length ();
413 if (cur_chain_len > MAX_CHAIN_LEN)
414 return false;
416 for (i = 0; i < cur_chain_len; i++)
418 edge e = (*cur_cd_chain)[i];
419 /* Cycle detected. */
420 if (e->src == bb)
421 return false;
424 FOR_EACH_EDGE (e, ei, bb->succs)
426 basic_block cd_bb;
427 int post_dom_check = 0;
428 if (e->flags & (EDGE_FAKE | EDGE_ABNORMAL))
429 continue;
431 cd_bb = e->dest;
432 cur_cd_chain->safe_push (e);
433 while (!is_non_loop_exit_postdominating (cd_bb, bb))
435 if (cd_bb == dep_bb)
437 /* Found a direct control dependence. */
438 if (*num_chains < MAX_NUM_CHAINS)
440 cd_chains[*num_chains] = cur_cd_chain->copy ();
441 (*num_chains)++;
443 found_cd_chain = true;
444 /* Check path from next edge. */
445 break;
448 /* Now check if DEP_BB is indirectly control dependent on BB. */
449 if (compute_control_dep_chain (cd_bb, dep_bb, cd_chains, num_chains,
450 cur_cd_chain, num_calls))
452 found_cd_chain = true;
453 break;
456 cd_bb = find_pdom (cd_bb);
457 post_dom_check++;
458 if (cd_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
459 || post_dom_check > MAX_POSTDOM_CHECK)
460 break;
462 cur_cd_chain->pop ();
463 gcc_assert (cur_cd_chain->length () == cur_chain_len);
465 gcc_assert (cur_cd_chain->length () == cur_chain_len);
467 return found_cd_chain;
470 /* The type to represent a simple predicate. */
472 struct pred_info
474 tree pred_lhs;
475 tree pred_rhs;
476 enum tree_code cond_code;
477 bool invert;
480 /* The type to represent a sequence of predicates grouped
481 with .AND. operation. */
483 typedef vec<pred_info, va_heap, vl_ptr> pred_chain;
485 /* The type to represent a sequence of pred_chains grouped
486 with .OR. operation. */
488 typedef vec<pred_chain, va_heap, vl_ptr> pred_chain_union;
490 /* Converts the chains of control dependence edges into a set of
491 predicates. A control dependence chain is represented by a vector
492 edges. DEP_CHAINS points to an array of dependence chains.
493 NUM_CHAINS is the size of the chain array. One edge in a dependence
494 chain is mapped to predicate expression represented by pred_info
495 type. One dependence chain is converted to a composite predicate that
496 is the result of AND operation of pred_info mapped to each edge.
497 A composite predicate is presented by a vector of pred_info. On
498 return, *PREDS points to the resulting array of composite predicates.
499 *NUM_PREDS is the number of composite predictes. */
501 static bool
502 convert_control_dep_chain_into_preds (vec<edge> *dep_chains,
503 size_t num_chains,
504 pred_chain_union *preds)
506 bool has_valid_pred = false;
507 size_t i, j;
508 if (num_chains == 0 || num_chains >= MAX_NUM_CHAINS)
509 return false;
511 /* Now convert the control dep chain into a set
512 of predicates. */
513 preds->reserve (num_chains);
515 for (i = 0; i < num_chains; i++)
517 vec<edge> one_cd_chain = dep_chains[i];
519 has_valid_pred = false;
520 pred_chain t_chain = vNULL;
521 for (j = 0; j < one_cd_chain.length (); j++)
523 gimple *cond_stmt;
524 gimple_stmt_iterator gsi;
525 basic_block guard_bb;
526 pred_info one_pred;
527 edge e;
529 e = one_cd_chain[j];
530 guard_bb = e->src;
531 gsi = gsi_last_bb (guard_bb);
532 if (gsi_end_p (gsi))
534 has_valid_pred = false;
535 break;
537 cond_stmt = gsi_stmt (gsi);
538 if (is_gimple_call (cond_stmt) && EDGE_COUNT (e->src->succs) >= 2)
539 /* Ignore EH edge. Can add assertion on the other edge's flag. */
540 continue;
541 /* Skip if there is essentially one succesor. */
542 if (EDGE_COUNT (e->src->succs) == 2)
544 edge e1;
545 edge_iterator ei1;
546 bool skip = false;
548 FOR_EACH_EDGE (e1, ei1, e->src->succs)
550 if (EDGE_COUNT (e1->dest->succs) == 0)
552 skip = true;
553 break;
556 if (skip)
557 continue;
559 if (gimple_code (cond_stmt) == GIMPLE_COND)
561 one_pred.pred_lhs = gimple_cond_lhs (cond_stmt);
562 one_pred.pred_rhs = gimple_cond_rhs (cond_stmt);
563 one_pred.cond_code = gimple_cond_code (cond_stmt);
564 one_pred.invert = !!(e->flags & EDGE_FALSE_VALUE);
565 t_chain.safe_push (one_pred);
566 has_valid_pred = true;
568 else if (gswitch *gs = dyn_cast<gswitch *> (cond_stmt))
570 /* Avoid quadratic behavior. */
571 if (gimple_switch_num_labels (gs) > MAX_SWITCH_CASES)
573 has_valid_pred = false;
574 break;
576 /* Find the case label. */
577 tree l = NULL_TREE;
578 unsigned idx;
579 for (idx = 0; idx < gimple_switch_num_labels (gs); ++idx)
581 tree tl = gimple_switch_label (gs, idx);
582 if (e->dest == label_to_block (CASE_LABEL (tl)))
584 if (!l)
585 l = tl;
586 else
588 l = NULL_TREE;
589 break;
593 /* If more than one label reaches this block or the case
594 label doesn't have a single value (like the default one)
595 fail. */
596 if (!l
597 || !CASE_LOW (l)
598 || (CASE_HIGH (l)
599 && !operand_equal_p (CASE_LOW (l), CASE_HIGH (l), 0)))
601 has_valid_pred = false;
602 break;
604 one_pred.pred_lhs = gimple_switch_index (gs);
605 one_pred.pred_rhs = CASE_LOW (l);
606 one_pred.cond_code = EQ_EXPR;
607 one_pred.invert = false;
608 t_chain.safe_push (one_pred);
609 has_valid_pred = true;
611 else
613 has_valid_pred = false;
614 break;
618 if (!has_valid_pred)
619 break;
620 else
621 preds->safe_push (t_chain);
623 return has_valid_pred;
626 /* Computes all control dependence chains for USE_BB. The control
627 dependence chains are then converted to an array of composite
628 predicates pointed to by PREDS. PHI_BB is the basic block of
629 the phi whose result is used in USE_BB. */
631 static bool
632 find_predicates (pred_chain_union *preds,
633 basic_block phi_bb,
634 basic_block use_bb)
636 size_t num_chains = 0, i;
637 int num_calls = 0;
638 vec<edge> dep_chains[MAX_NUM_CHAINS];
639 auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
640 bool has_valid_pred = false;
641 basic_block cd_root = 0;
643 /* First find the closest bb that is control equivalent to PHI_BB
644 that also dominates USE_BB. */
645 cd_root = phi_bb;
646 while (dominated_by_p (CDI_DOMINATORS, use_bb, cd_root))
648 basic_block ctrl_eq_bb = find_control_equiv_block (cd_root);
649 if (ctrl_eq_bb && dominated_by_p (CDI_DOMINATORS, use_bb, ctrl_eq_bb))
650 cd_root = ctrl_eq_bb;
651 else
652 break;
655 compute_control_dep_chain (cd_root, use_bb, dep_chains, &num_chains,
656 &cur_chain, &num_calls);
658 has_valid_pred
659 = convert_control_dep_chain_into_preds (dep_chains, num_chains, preds);
660 for (i = 0; i < num_chains; i++)
661 dep_chains[i].release ();
662 return has_valid_pred;
665 /* Computes the set of incoming edges of PHI that have non empty
666 definitions of a phi chain. The collection will be done
667 recursively on operands that are defined by phis. CD_ROOT
668 is the control dependence root. *EDGES holds the result, and
669 VISITED_PHIS is a pointer set for detecting cycles. */
671 static void
672 collect_phi_def_edges (gphi *phi, basic_block cd_root,
673 auto_vec<edge> *edges,
674 hash_set<gimple *> *visited_phis)
676 size_t i, n;
677 edge opnd_edge;
678 tree opnd;
680 if (visited_phis->add (phi))
681 return;
683 n = gimple_phi_num_args (phi);
684 for (i = 0; i < n; i++)
686 opnd_edge = gimple_phi_arg_edge (phi, i);
687 opnd = gimple_phi_arg_def (phi, i);
689 if (TREE_CODE (opnd) != SSA_NAME)
691 if (dump_file && (dump_flags & TDF_DETAILS))
693 fprintf (dump_file, "\n[CHECK] Found def edge %d in ", (int) i);
694 print_gimple_stmt (dump_file, phi, 0, 0);
696 edges->safe_push (opnd_edge);
698 else
700 gimple *def = SSA_NAME_DEF_STMT (opnd);
702 if (gimple_code (def) == GIMPLE_PHI
703 && dominated_by_p (CDI_DOMINATORS, gimple_bb (def), cd_root))
704 collect_phi_def_edges (as_a<gphi *> (def), cd_root, edges,
705 visited_phis);
706 else if (!uninit_undefined_value_p (opnd))
708 if (dump_file && (dump_flags & TDF_DETAILS))
710 fprintf (dump_file, "\n[CHECK] Found def edge %d in ",
711 (int) i);
712 print_gimple_stmt (dump_file, phi, 0, 0);
714 edges->safe_push (opnd_edge);
720 /* For each use edge of PHI, computes all control dependence chains.
721 The control dependence chains are then converted to an array of
722 composite predicates pointed to by PREDS. */
724 static bool
725 find_def_preds (pred_chain_union *preds, gphi *phi)
727 size_t num_chains = 0, i, n;
728 vec<edge> dep_chains[MAX_NUM_CHAINS];
729 auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
730 auto_vec<edge> def_edges;
731 bool has_valid_pred = false;
732 basic_block phi_bb, cd_root = 0;
734 phi_bb = gimple_bb (phi);
735 /* First find the closest dominating bb to be
736 the control dependence root. */
737 cd_root = find_dom (phi_bb);
738 if (!cd_root)
739 return false;
741 hash_set<gimple *> visited_phis;
742 collect_phi_def_edges (phi, cd_root, &def_edges, &visited_phis);
744 n = def_edges.length ();
745 if (n == 0)
746 return false;
748 for (i = 0; i < n; i++)
750 size_t prev_nc, j;
751 int num_calls = 0;
752 edge opnd_edge;
754 opnd_edge = def_edges[i];
755 prev_nc = num_chains;
756 compute_control_dep_chain (cd_root, opnd_edge->src, dep_chains,
757 &num_chains, &cur_chain, &num_calls);
759 /* Now update the newly added chains with
760 the phi operand edge: */
761 if (EDGE_COUNT (opnd_edge->src->succs) > 1)
763 if (prev_nc == num_chains && num_chains < MAX_NUM_CHAINS)
764 dep_chains[num_chains++] = vNULL;
765 for (j = prev_nc; j < num_chains; j++)
766 dep_chains[j].safe_push (opnd_edge);
770 has_valid_pred
771 = convert_control_dep_chain_into_preds (dep_chains, num_chains, preds);
772 for (i = 0; i < num_chains; i++)
773 dep_chains[i].release ();
774 return has_valid_pred;
777 /* Dumps the predicates (PREDS) for USESTMT. */
779 static void
780 dump_predicates (gimple *usestmt, pred_chain_union preds, const char *msg)
782 size_t i, j;
783 pred_chain one_pred_chain = vNULL;
784 fprintf (dump_file, "%s", msg);
785 print_gimple_stmt (dump_file, usestmt, 0, 0);
786 fprintf (dump_file, "is guarded by :\n\n");
787 size_t num_preds = preds.length ();
788 /* Do some dumping here: */
789 for (i = 0; i < num_preds; i++)
791 size_t np;
793 one_pred_chain = preds[i];
794 np = one_pred_chain.length ();
796 for (j = 0; j < np; j++)
798 pred_info one_pred = one_pred_chain[j];
799 if (one_pred.invert)
800 fprintf (dump_file, " (.NOT.) ");
801 print_generic_expr (dump_file, one_pred.pred_lhs, 0);
802 fprintf (dump_file, " %s ", op_symbol_code (one_pred.cond_code));
803 print_generic_expr (dump_file, one_pred.pred_rhs, 0);
804 if (j < np - 1)
805 fprintf (dump_file, " (.AND.) ");
806 else
807 fprintf (dump_file, "\n");
809 if (i < num_preds - 1)
810 fprintf (dump_file, "(.OR.)\n");
811 else
812 fprintf (dump_file, "\n\n");
816 /* Destroys the predicate set *PREDS. */
818 static void
819 destroy_predicate_vecs (pred_chain_union *preds)
821 size_t i;
823 size_t n = preds->length ();
824 for (i = 0; i < n; i++)
825 (*preds)[i].release ();
826 preds->release ();
829 /* Computes the 'normalized' conditional code with operand
830 swapping and condition inversion. */
832 static enum tree_code
833 get_cmp_code (enum tree_code orig_cmp_code, bool swap_cond, bool invert)
835 enum tree_code tc = orig_cmp_code;
837 if (swap_cond)
838 tc = swap_tree_comparison (orig_cmp_code);
839 if (invert)
840 tc = invert_tree_comparison (tc, false);
842 switch (tc)
844 case LT_EXPR:
845 case LE_EXPR:
846 case GT_EXPR:
847 case GE_EXPR:
848 case EQ_EXPR:
849 case NE_EXPR:
850 break;
851 default:
852 return ERROR_MARK;
854 return tc;
857 /* Returns true if VAL falls in the range defined by BOUNDARY and CMPC, i.e.
858 all values in the range satisfies (x CMPC BOUNDARY) == true. */
860 static bool
861 is_value_included_in (tree val, tree boundary, enum tree_code cmpc)
863 bool inverted = false;
864 bool is_unsigned;
865 bool result;
867 /* Only handle integer constant here. */
868 if (TREE_CODE (val) != INTEGER_CST || TREE_CODE (boundary) != INTEGER_CST)
869 return true;
871 is_unsigned = TYPE_UNSIGNED (TREE_TYPE (val));
873 if (cmpc == GE_EXPR || cmpc == GT_EXPR || cmpc == NE_EXPR)
875 cmpc = invert_tree_comparison (cmpc, false);
876 inverted = true;
879 if (is_unsigned)
881 if (cmpc == EQ_EXPR)
882 result = tree_int_cst_equal (val, boundary);
883 else if (cmpc == LT_EXPR)
884 result = tree_int_cst_lt (val, boundary);
885 else
887 gcc_assert (cmpc == LE_EXPR);
888 result = tree_int_cst_le (val, boundary);
891 else
893 if (cmpc == EQ_EXPR)
894 result = tree_int_cst_equal (val, boundary);
895 else if (cmpc == LT_EXPR)
896 result = tree_int_cst_lt (val, boundary);
897 else
899 gcc_assert (cmpc == LE_EXPR);
900 result = (tree_int_cst_equal (val, boundary)
901 || tree_int_cst_lt (val, boundary));
905 if (inverted)
906 result ^= 1;
908 return result;
911 /* Returns true if PRED is common among all the predicate
912 chains (PREDS) (and therefore can be factored out).
913 NUM_PRED_CHAIN is the size of array PREDS. */
915 static bool
916 find_matching_predicate_in_rest_chains (pred_info pred,
917 pred_chain_union preds,
918 size_t num_pred_chains)
920 size_t i, j, n;
922 /* Trival case. */
923 if (num_pred_chains == 1)
924 return true;
926 for (i = 1; i < num_pred_chains; i++)
928 bool found = false;
929 pred_chain one_chain = preds[i];
930 n = one_chain.length ();
931 for (j = 0; j < n; j++)
933 pred_info pred2 = one_chain[j];
934 /* Can relax the condition comparison to not
935 use address comparison. However, the most common
936 case is that multiple control dependent paths share
937 a common path prefix, so address comparison should
938 be ok. */
940 if (operand_equal_p (pred2.pred_lhs, pred.pred_lhs, 0)
941 && operand_equal_p (pred2.pred_rhs, pred.pred_rhs, 0)
942 && pred2.invert == pred.invert)
944 found = true;
945 break;
948 if (!found)
949 return false;
951 return true;
954 /* Forward declaration. */
955 static bool is_use_properly_guarded (gimple *use_stmt,
956 basic_block use_bb,
957 gphi *phi,
958 unsigned uninit_opnds,
959 pred_chain_union *def_preds,
960 hash_set<gphi *> *visited_phis);
962 /* Returns true if all uninitialized opnds are pruned. Returns false
963 otherwise. PHI is the phi node with uninitialized operands,
964 UNINIT_OPNDS is the bitmap of the uninitialize operand positions,
965 FLAG_DEF is the statement defining the flag guarding the use of the
966 PHI output, BOUNDARY_CST is the const value used in the predicate
967 associated with the flag, CMP_CODE is the comparison code used in
968 the predicate, VISITED_PHIS is the pointer set of phis visited, and
969 VISITED_FLAG_PHIS is the pointer to the pointer set of flag definitions
970 that are also phis.
972 Example scenario:
974 BB1:
975 flag_1 = phi <0, 1> // (1)
976 var_1 = phi <undef, some_val>
979 BB2:
980 flag_2 = phi <0, flag_1, flag_1> // (2)
981 var_2 = phi <undef, var_1, var_1>
982 if (flag_2 == 1)
983 goto BB3;
985 BB3:
986 use of var_2 // (3)
988 Because some flag arg in (1) is not constant, if we do not look into the
989 flag phis recursively, it is conservatively treated as unknown and var_1
990 is thought to be flowed into use at (3). Since var_1 is potentially
991 uninitialized a false warning will be emitted.
992 Checking recursively into (1), the compiler can find out that only some_val
993 (which is defined) can flow into (3) which is OK. */
995 static bool
996 prune_uninit_phi_opnds (gphi *phi, unsigned uninit_opnds, gphi *flag_def,
997 tree boundary_cst, enum tree_code cmp_code,
998 hash_set<gphi *> *visited_phis,
999 bitmap *visited_flag_phis)
1001 unsigned i;
1003 for (i = 0; i < MIN (32, gimple_phi_num_args (flag_def)); i++)
1005 tree flag_arg;
1007 if (!MASK_TEST_BIT (uninit_opnds, i))
1008 continue;
1010 flag_arg = gimple_phi_arg_def (flag_def, i);
1011 if (!is_gimple_constant (flag_arg))
1013 gphi *flag_arg_def, *phi_arg_def;
1014 tree phi_arg;
1015 unsigned uninit_opnds_arg_phi;
1017 if (TREE_CODE (flag_arg) != SSA_NAME)
1018 return false;
1019 flag_arg_def = dyn_cast<gphi *> (SSA_NAME_DEF_STMT (flag_arg));
1020 if (!flag_arg_def)
1021 return false;
1023 phi_arg = gimple_phi_arg_def (phi, i);
1024 if (TREE_CODE (phi_arg) != SSA_NAME)
1025 return false;
1027 phi_arg_def = dyn_cast<gphi *> (SSA_NAME_DEF_STMT (phi_arg));
1028 if (!phi_arg_def)
1029 return false;
1031 if (gimple_bb (phi_arg_def) != gimple_bb (flag_arg_def))
1032 return false;
1034 if (!*visited_flag_phis)
1035 *visited_flag_phis = BITMAP_ALLOC (NULL);
1037 tree phi_result = gimple_phi_result (flag_arg_def);
1038 if (bitmap_bit_p (*visited_flag_phis, SSA_NAME_VERSION (phi_result)))
1039 return false;
1041 bitmap_set_bit (*visited_flag_phis,
1042 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def)));
1044 /* Now recursively prune the uninitialized phi args. */
1045 uninit_opnds_arg_phi = compute_uninit_opnds_pos (phi_arg_def);
1046 if (!prune_uninit_phi_opnds
1047 (phi_arg_def, uninit_opnds_arg_phi, flag_arg_def, boundary_cst,
1048 cmp_code, visited_phis, visited_flag_phis))
1049 return false;
1051 phi_result = gimple_phi_result (flag_arg_def);
1052 bitmap_clear_bit (*visited_flag_phis, SSA_NAME_VERSION (phi_result));
1053 continue;
1056 /* Now check if the constant is in the guarded range. */
1057 if (is_value_included_in (flag_arg, boundary_cst, cmp_code))
1059 tree opnd;
1060 gimple *opnd_def;
1062 /* Now that we know that this undefined edge is not
1063 pruned. If the operand is defined by another phi,
1064 we can further prune the incoming edges of that
1065 phi by checking the predicates of this operands. */
1067 opnd = gimple_phi_arg_def (phi, i);
1068 opnd_def = SSA_NAME_DEF_STMT (opnd);
1069 if (gphi *opnd_def_phi = dyn_cast <gphi *> (opnd_def))
1071 edge opnd_edge;
1072 unsigned uninit_opnds2 = compute_uninit_opnds_pos (opnd_def_phi);
1073 if (!MASK_EMPTY (uninit_opnds2))
1075 pred_chain_union def_preds = vNULL;
1076 bool ok;
1077 opnd_edge = gimple_phi_arg_edge (phi, i);
1078 ok = is_use_properly_guarded (phi,
1079 opnd_edge->src,
1080 opnd_def_phi,
1081 uninit_opnds2,
1082 &def_preds,
1083 visited_phis);
1084 destroy_predicate_vecs (&def_preds);
1085 if (!ok)
1086 return false;
1089 else
1090 return false;
1094 return true;
1097 /* A helper function that determines if the predicate set
1098 of the use is not overlapping with that of the uninit paths.
1099 The most common senario of guarded use is in Example 1:
1100 Example 1:
1101 if (some_cond)
1103 x = ...;
1104 flag = true;
1107 ... some code ...
1109 if (flag)
1110 use (x);
1112 The real world examples are usually more complicated, but similar
1113 and usually result from inlining:
1115 bool init_func (int * x)
1117 if (some_cond)
1118 return false;
1119 *x = ..
1120 return true;
1123 void foo (..)
1125 int x;
1127 if (!init_func (&x))
1128 return;
1130 .. some_code ...
1131 use (x);
1134 Another possible use scenario is in the following trivial example:
1136 Example 2:
1137 if (n > 0)
1138 x = 1;
1140 if (n > 0)
1142 if (m < 2)
1143 .. = x;
1146 Predicate analysis needs to compute the composite predicate:
1148 1) 'x' use predicate: (n > 0) .AND. (m < 2)
1149 2) 'x' default value (non-def) predicate: .NOT. (n > 0)
1150 (the predicate chain for phi operand defs can be computed
1151 starting from a bb that is control equivalent to the phi's
1152 bb and is dominating the operand def.)
1154 and check overlapping:
1155 (n > 0) .AND. (m < 2) .AND. (.NOT. (n > 0))
1156 <==> false
1158 This implementation provides framework that can handle
1159 scenarios. (Note that many simple cases are handled properly
1160 without the predicate analysis -- this is due to jump threading
1161 transformation which eliminates the merge point thus makes
1162 path sensitive analysis unnecessary.)
1164 NUM_PREDS is the number is the number predicate chains, PREDS is
1165 the array of chains, PHI is the phi node whose incoming (undefined)
1166 paths need to be pruned, and UNINIT_OPNDS is the bitmap holding
1167 uninit operand positions. VISITED_PHIS is the pointer set of phi
1168 stmts being checked. */
1170 static bool
1171 use_pred_not_overlap_with_undef_path_pred (pred_chain_union preds,
1172 gphi *phi, unsigned uninit_opnds,
1173 hash_set<gphi *> *visited_phis)
1175 unsigned int i, n;
1176 gimple *flag_def = 0;
1177 tree boundary_cst = 0;
1178 enum tree_code cmp_code;
1179 bool swap_cond = false;
1180 bool invert = false;
1181 pred_chain the_pred_chain = vNULL;
1182 bitmap visited_flag_phis = NULL;
1183 bool all_pruned = false;
1184 size_t num_preds = preds.length ();
1186 gcc_assert (num_preds > 0);
1187 /* Find within the common prefix of multiple predicate chains
1188 a predicate that is a comparison of a flag variable against
1189 a constant. */
1190 the_pred_chain = preds[0];
1191 n = the_pred_chain.length ();
1192 for (i = 0; i < n; i++)
1194 tree cond_lhs, cond_rhs, flag = 0;
1196 pred_info the_pred = the_pred_chain[i];
1198 invert = the_pred.invert;
1199 cond_lhs = the_pred.pred_lhs;
1200 cond_rhs = the_pred.pred_rhs;
1201 cmp_code = the_pred.cond_code;
1203 if (cond_lhs != NULL_TREE && TREE_CODE (cond_lhs) == SSA_NAME
1204 && cond_rhs != NULL_TREE && is_gimple_constant (cond_rhs))
1206 boundary_cst = cond_rhs;
1207 flag = cond_lhs;
1209 else if (cond_rhs != NULL_TREE && TREE_CODE (cond_rhs) == SSA_NAME
1210 && cond_lhs != NULL_TREE && is_gimple_constant (cond_lhs))
1212 boundary_cst = cond_lhs;
1213 flag = cond_rhs;
1214 swap_cond = true;
1217 if (!flag)
1218 continue;
1220 flag_def = SSA_NAME_DEF_STMT (flag);
1222 if (!flag_def)
1223 continue;
1225 if ((gimple_code (flag_def) == GIMPLE_PHI)
1226 && (gimple_bb (flag_def) == gimple_bb (phi))
1227 && find_matching_predicate_in_rest_chains (the_pred, preds,
1228 num_preds))
1229 break;
1231 flag_def = 0;
1234 if (!flag_def)
1235 return false;
1237 /* Now check all the uninit incoming edge has a constant flag value
1238 that is in conflict with the use guard/predicate. */
1239 cmp_code = get_cmp_code (cmp_code, swap_cond, invert);
1241 if (cmp_code == ERROR_MARK)
1242 return false;
1244 all_pruned = prune_uninit_phi_opnds
1245 (phi, uninit_opnds, as_a<gphi *> (flag_def), boundary_cst, cmp_code,
1246 visited_phis, &visited_flag_phis);
1248 if (visited_flag_phis)
1249 BITMAP_FREE (visited_flag_phis);
1251 return all_pruned;
1254 /* The helper function returns true if two predicates X1 and X2
1255 are equivalent. It assumes the expressions have already
1256 properly re-associated. */
1258 static inline bool
1259 pred_equal_p (pred_info x1, pred_info x2)
1261 enum tree_code c1, c2;
1262 if (!operand_equal_p (x1.pred_lhs, x2.pred_lhs, 0)
1263 || !operand_equal_p (x1.pred_rhs, x2.pred_rhs, 0))
1264 return false;
1266 c1 = x1.cond_code;
1267 if (x1.invert != x2.invert
1268 && TREE_CODE_CLASS (x2.cond_code) == tcc_comparison)
1269 c2 = invert_tree_comparison (x2.cond_code, false);
1270 else
1271 c2 = x2.cond_code;
1273 return c1 == c2;
1276 /* Returns true if the predication is testing !=. */
1278 static inline bool
1279 is_neq_relop_p (pred_info pred)
1282 return ((pred.cond_code == NE_EXPR && !pred.invert)
1283 || (pred.cond_code == EQ_EXPR && pred.invert));
1286 /* Returns true if pred is of the form X != 0. */
1288 static inline bool
1289 is_neq_zero_form_p (pred_info pred)
1291 if (!is_neq_relop_p (pred) || !integer_zerop (pred.pred_rhs)
1292 || TREE_CODE (pred.pred_lhs) != SSA_NAME)
1293 return false;
1294 return true;
1297 /* The helper function returns true if two predicates X1
1298 is equivalent to X2 != 0. */
1300 static inline bool
1301 pred_expr_equal_p (pred_info x1, tree x2)
1303 if (!is_neq_zero_form_p (x1))
1304 return false;
1306 return operand_equal_p (x1.pred_lhs, x2, 0);
1309 /* Returns true of the domain of single predicate expression
1310 EXPR1 is a subset of that of EXPR2. Returns false if it
1311 can not be proved. */
1313 static bool
1314 is_pred_expr_subset_of (pred_info expr1, pred_info expr2)
1316 enum tree_code code1, code2;
1318 if (pred_equal_p (expr1, expr2))
1319 return true;
1321 if ((TREE_CODE (expr1.pred_rhs) != INTEGER_CST)
1322 || (TREE_CODE (expr2.pred_rhs) != INTEGER_CST))
1323 return false;
1325 if (!operand_equal_p (expr1.pred_lhs, expr2.pred_lhs, 0))
1326 return false;
1328 code1 = expr1.cond_code;
1329 if (expr1.invert)
1330 code1 = invert_tree_comparison (code1, false);
1331 code2 = expr2.cond_code;
1332 if (expr2.invert)
1333 code2 = invert_tree_comparison (code2, false);
1335 if ((code1 == EQ_EXPR || code1 == BIT_AND_EXPR) && code2 == BIT_AND_EXPR)
1336 return wi::eq_p (expr1.pred_rhs,
1337 wi::bit_and (expr1.pred_rhs, expr2.pred_rhs));
1339 if (code1 != code2 && code2 != NE_EXPR)
1340 return false;
1342 if (is_value_included_in (expr1.pred_rhs, expr2.pred_rhs, code2))
1343 return true;
1345 return false;
1348 /* Returns true if the domain of PRED1 is a subset
1349 of that of PRED2. Returns false if it can not be proved so. */
1351 static bool
1352 is_pred_chain_subset_of (pred_chain pred1, pred_chain pred2)
1354 size_t np1, np2, i1, i2;
1356 np1 = pred1.length ();
1357 np2 = pred2.length ();
1359 for (i2 = 0; i2 < np2; i2++)
1361 bool found = false;
1362 pred_info info2 = pred2[i2];
1363 for (i1 = 0; i1 < np1; i1++)
1365 pred_info info1 = pred1[i1];
1366 if (is_pred_expr_subset_of (info1, info2))
1368 found = true;
1369 break;
1372 if (!found)
1373 return false;
1375 return true;
1378 /* Returns true if the domain defined by
1379 one pred chain ONE_PRED is a subset of the domain
1380 of *PREDS. It returns false if ONE_PRED's domain is
1381 not a subset of any of the sub-domains of PREDS
1382 (corresponding to each individual chains in it), even
1383 though it may be still be a subset of whole domain
1384 of PREDS which is the union (ORed) of all its subdomains.
1385 In other words, the result is conservative. */
1387 static bool
1388 is_included_in (pred_chain one_pred, pred_chain_union preds)
1390 size_t i;
1391 size_t n = preds.length ();
1393 for (i = 0; i < n; i++)
1395 if (is_pred_chain_subset_of (one_pred, preds[i]))
1396 return true;
1399 return false;
1402 /* Compares two predicate sets PREDS1 and PREDS2 and returns
1403 true if the domain defined by PREDS1 is a superset
1404 of PREDS2's domain. N1 and N2 are array sizes of PREDS1 and
1405 PREDS2 respectively. The implementation chooses not to build
1406 generic trees (and relying on the folding capability of the
1407 compiler), but instead performs brute force comparison of
1408 individual predicate chains (won't be a compile time problem
1409 as the chains are pretty short). When the function returns
1410 false, it does not necessarily mean *PREDS1 is not a superset
1411 of *PREDS2, but mean it may not be so since the analysis can
1412 not prove it. In such cases, false warnings may still be
1413 emitted. */
1415 static bool
1416 is_superset_of (pred_chain_union preds1, pred_chain_union preds2)
1418 size_t i, n2;
1419 pred_chain one_pred_chain = vNULL;
1421 n2 = preds2.length ();
1423 for (i = 0; i < n2; i++)
1425 one_pred_chain = preds2[i];
1426 if (!is_included_in (one_pred_chain, preds1))
1427 return false;
1430 return true;
1433 /* Returns true if TC is AND or OR. */
1435 static inline bool
1436 is_and_or_or_p (enum tree_code tc, tree type)
1438 return (tc == BIT_IOR_EXPR
1439 || (tc == BIT_AND_EXPR
1440 && (type == 0 || TREE_CODE (type) == BOOLEAN_TYPE)));
1443 /* Returns true if X1 is the negate of X2. */
1445 static inline bool
1446 pred_neg_p (pred_info x1, pred_info x2)
1448 enum tree_code c1, c2;
1449 if (!operand_equal_p (x1.pred_lhs, x2.pred_lhs, 0)
1450 || !operand_equal_p (x1.pred_rhs, x2.pred_rhs, 0))
1451 return false;
1453 c1 = x1.cond_code;
1454 if (x1.invert == x2.invert)
1455 c2 = invert_tree_comparison (x2.cond_code, false);
1456 else
1457 c2 = x2.cond_code;
1459 return c1 == c2;
1462 /* 1) ((x IOR y) != 0) AND (x != 0) is equivalent to (x != 0);
1463 2) (X AND Y) OR (!X AND Y) is equivalent to Y;
1464 3) X OR (!X AND Y) is equivalent to (X OR Y);
1465 4) ((x IAND y) != 0) || (x != 0 AND y != 0)) is equivalent to
1466 (x != 0 AND y != 0)
1467 5) (X AND Y) OR (!X AND Z) OR (!Y AND Z) is equivalent to
1468 (X AND Y) OR Z
1470 PREDS is the predicate chains, and N is the number of chains. */
1472 /* Helper function to implement rule 1 above. ONE_CHAIN is
1473 the AND predication to be simplified. */
1475 static void
1476 simplify_pred (pred_chain *one_chain)
1478 size_t i, j, n;
1479 bool simplified = false;
1480 pred_chain s_chain = vNULL;
1482 n = one_chain->length ();
1484 for (i = 0; i < n; i++)
1486 pred_info *a_pred = &(*one_chain)[i];
1488 if (!a_pred->pred_lhs)
1489 continue;
1490 if (!is_neq_zero_form_p (*a_pred))
1491 continue;
1493 gimple *def_stmt = SSA_NAME_DEF_STMT (a_pred->pred_lhs);
1494 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1495 continue;
1496 if (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR)
1498 for (j = 0; j < n; j++)
1500 pred_info *b_pred = &(*one_chain)[j];
1502 if (!b_pred->pred_lhs)
1503 continue;
1504 if (!is_neq_zero_form_p (*b_pred))
1505 continue;
1507 if (pred_expr_equal_p (*b_pred, gimple_assign_rhs1 (def_stmt))
1508 || pred_expr_equal_p (*b_pred, gimple_assign_rhs2 (def_stmt)))
1510 /* Mark a_pred for removal. */
1511 a_pred->pred_lhs = NULL;
1512 a_pred->pred_rhs = NULL;
1513 simplified = true;
1514 break;
1520 if (!simplified)
1521 return;
1523 for (i = 0; i < n; i++)
1525 pred_info *a_pred = &(*one_chain)[i];
1526 if (!a_pred->pred_lhs)
1527 continue;
1528 s_chain.safe_push (*a_pred);
1531 one_chain->release ();
1532 *one_chain = s_chain;
1535 /* The helper function implements the rule 2 for the
1536 OR predicate PREDS.
1538 2) (X AND Y) OR (!X AND Y) is equivalent to Y. */
1540 static bool
1541 simplify_preds_2 (pred_chain_union *preds)
1543 size_t i, j, n;
1544 bool simplified = false;
1545 pred_chain_union s_preds = vNULL;
1547 /* (X AND Y) OR (!X AND Y) is equivalent to Y.
1548 (X AND Y) OR (X AND !Y) is equivalent to X. */
1550 n = preds->length ();
1551 for (i = 0; i < n; i++)
1553 pred_info x, y;
1554 pred_chain *a_chain = &(*preds)[i];
1556 if (a_chain->length () != 2)
1557 continue;
1559 x = (*a_chain)[0];
1560 y = (*a_chain)[1];
1562 for (j = 0; j < n; j++)
1564 pred_chain *b_chain;
1565 pred_info x2, y2;
1567 if (j == i)
1568 continue;
1570 b_chain = &(*preds)[j];
1571 if (b_chain->length () != 2)
1572 continue;
1574 x2 = (*b_chain)[0];
1575 y2 = (*b_chain)[1];
1577 if (pred_equal_p (x, x2) && pred_neg_p (y, y2))
1579 /* Kill a_chain. */
1580 a_chain->release ();
1581 b_chain->release ();
1582 b_chain->safe_push (x);
1583 simplified = true;
1584 break;
1586 if (pred_neg_p (x, x2) && pred_equal_p (y, y2))
1588 /* Kill a_chain. */
1589 a_chain->release ();
1590 b_chain->release ();
1591 b_chain->safe_push (y);
1592 simplified = true;
1593 break;
1597 /* Now clean up the chain. */
1598 if (simplified)
1600 for (i = 0; i < n; i++)
1602 if ((*preds)[i].is_empty ())
1603 continue;
1604 s_preds.safe_push ((*preds)[i]);
1606 preds->release ();
1607 (*preds) = s_preds;
1608 s_preds = vNULL;
1611 return simplified;
1614 /* The helper function implements the rule 2 for the
1615 OR predicate PREDS.
1617 3) x OR (!x AND y) is equivalent to x OR y. */
1619 static bool
1620 simplify_preds_3 (pred_chain_union *preds)
1622 size_t i, j, n;
1623 bool simplified = false;
1625 /* Now iteratively simplify X OR (!X AND Z ..)
1626 into X OR (Z ...). */
1628 n = preds->length ();
1629 if (n < 2)
1630 return false;
1632 for (i = 0; i < n; i++)
1634 pred_info x;
1635 pred_chain *a_chain = &(*preds)[i];
1637 if (a_chain->length () != 1)
1638 continue;
1640 x = (*a_chain)[0];
1642 for (j = 0; j < n; j++)
1644 pred_chain *b_chain;
1645 pred_info x2;
1646 size_t k;
1648 if (j == i)
1649 continue;
1651 b_chain = &(*preds)[j];
1652 if (b_chain->length () < 2)
1653 continue;
1655 for (k = 0; k < b_chain->length (); k++)
1657 x2 = (*b_chain)[k];
1658 if (pred_neg_p (x, x2))
1660 b_chain->unordered_remove (k);
1661 simplified = true;
1662 break;
1667 return simplified;
1670 /* The helper function implements the rule 4 for the
1671 OR predicate PREDS.
1673 2) ((x AND y) != 0) OR (x != 0 AND y != 0) is equivalent to
1674 (x != 0 ANd y != 0). */
1676 static bool
1677 simplify_preds_4 (pred_chain_union *preds)
1679 size_t i, j, n;
1680 bool simplified = false;
1681 pred_chain_union s_preds = vNULL;
1682 gimple *def_stmt;
1684 n = preds->length ();
1685 for (i = 0; i < n; i++)
1687 pred_info z;
1688 pred_chain *a_chain = &(*preds)[i];
1690 if (a_chain->length () != 1)
1691 continue;
1693 z = (*a_chain)[0];
1695 if (!is_neq_zero_form_p (z))
1696 continue;
1698 def_stmt = SSA_NAME_DEF_STMT (z.pred_lhs);
1699 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1700 continue;
1702 if (gimple_assign_rhs_code (def_stmt) != BIT_AND_EXPR)
1703 continue;
1705 for (j = 0; j < n; j++)
1707 pred_chain *b_chain;
1708 pred_info x2, y2;
1710 if (j == i)
1711 continue;
1713 b_chain = &(*preds)[j];
1714 if (b_chain->length () != 2)
1715 continue;
1717 x2 = (*b_chain)[0];
1718 y2 = (*b_chain)[1];
1719 if (!is_neq_zero_form_p (x2) || !is_neq_zero_form_p (y2))
1720 continue;
1722 if ((pred_expr_equal_p (x2, gimple_assign_rhs1 (def_stmt))
1723 && pred_expr_equal_p (y2, gimple_assign_rhs2 (def_stmt)))
1724 || (pred_expr_equal_p (x2, gimple_assign_rhs2 (def_stmt))
1725 && pred_expr_equal_p (y2, gimple_assign_rhs1 (def_stmt))))
1727 /* Kill a_chain. */
1728 a_chain->release ();
1729 simplified = true;
1730 break;
1734 /* Now clean up the chain. */
1735 if (simplified)
1737 for (i = 0; i < n; i++)
1739 if ((*preds)[i].is_empty ())
1740 continue;
1741 s_preds.safe_push ((*preds)[i]);
1744 destroy_predicate_vecs (preds);
1745 (*preds) = s_preds;
1746 s_preds = vNULL;
1749 return simplified;
1752 /* This function simplifies predicates in PREDS. */
1754 static void
1755 simplify_preds (pred_chain_union *preds, gimple *use_or_def, bool is_use)
1757 size_t i, n;
1758 bool changed = false;
1760 if (dump_file && dump_flags & TDF_DETAILS)
1762 fprintf (dump_file, "[BEFORE SIMPLICATION -- ");
1763 dump_predicates (use_or_def, *preds, is_use ? "[USE]:\n" : "[DEF]:\n");
1766 for (i = 0; i < preds->length (); i++)
1767 simplify_pred (&(*preds)[i]);
1769 n = preds->length ();
1770 if (n < 2)
1771 return;
1775 changed = false;
1776 if (simplify_preds_2 (preds))
1777 changed = true;
1779 /* Now iteratively simplify X OR (!X AND Z ..)
1780 into X OR (Z ...). */
1781 if (simplify_preds_3 (preds))
1782 changed = true;
1784 if (simplify_preds_4 (preds))
1785 changed = true;
1787 while (changed);
1789 return;
1792 /* This is a helper function which attempts to normalize predicate chains
1793 by following UD chains. It basically builds up a big tree of either IOR
1794 operations or AND operations, and convert the IOR tree into a
1795 pred_chain_union or BIT_AND tree into a pred_chain.
1796 Example:
1798 _3 = _2 RELOP1 _1;
1799 _6 = _5 RELOP2 _4;
1800 _9 = _8 RELOP3 _7;
1801 _10 = _3 | _6;
1802 _12 = _9 | _0;
1803 _t = _10 | _12;
1805 then _t != 0 will be normalized into a pred_chain_union
1807 (_2 RELOP1 _1) OR (_5 RELOP2 _4) OR (_8 RELOP3 _7) OR (_0 != 0)
1809 Similarly given,
1811 _3 = _2 RELOP1 _1;
1812 _6 = _5 RELOP2 _4;
1813 _9 = _8 RELOP3 _7;
1814 _10 = _3 & _6;
1815 _12 = _9 & _0;
1817 then _t != 0 will be normalized into a pred_chain:
1818 (_2 RELOP1 _1) AND (_5 RELOP2 _4) AND (_8 RELOP3 _7) AND (_0 != 0)
1822 /* This is a helper function that stores a PRED into NORM_PREDS. */
1824 inline static void
1825 push_pred (pred_chain_union *norm_preds, pred_info pred)
1827 pred_chain pred_chain = vNULL;
1828 pred_chain.safe_push (pred);
1829 norm_preds->safe_push (pred_chain);
1832 /* A helper function that creates a predicate of the form
1833 OP != 0 and push it WORK_LIST. */
1835 inline static void
1836 push_to_worklist (tree op, vec<pred_info, va_heap, vl_ptr> *work_list,
1837 hash_set<tree> *mark_set)
1839 if (mark_set->contains (op))
1840 return;
1841 mark_set->add (op);
1843 pred_info arg_pred;
1844 arg_pred.pred_lhs = op;
1845 arg_pred.pred_rhs = integer_zero_node;
1846 arg_pred.cond_code = NE_EXPR;
1847 arg_pred.invert = false;
1848 work_list->safe_push (arg_pred);
1851 /* A helper that generates a pred_info from a gimple assignment
1852 CMP_ASSIGN with comparison rhs. */
1854 static pred_info
1855 get_pred_info_from_cmp (gimple *cmp_assign)
1857 pred_info n_pred;
1858 n_pred.pred_lhs = gimple_assign_rhs1 (cmp_assign);
1859 n_pred.pred_rhs = gimple_assign_rhs2 (cmp_assign);
1860 n_pred.cond_code = gimple_assign_rhs_code (cmp_assign);
1861 n_pred.invert = false;
1862 return n_pred;
1865 /* Returns true if the PHI is a degenerated phi with
1866 all args with the same value (relop). In that case, *PRED
1867 will be updated to that value. */
1869 static bool
1870 is_degenerated_phi (gimple *phi, pred_info *pred_p)
1872 int i, n;
1873 tree op0;
1874 gimple *def0;
1875 pred_info pred0;
1877 n = gimple_phi_num_args (phi);
1878 op0 = gimple_phi_arg_def (phi, 0);
1880 if (TREE_CODE (op0) != SSA_NAME)
1881 return false;
1883 def0 = SSA_NAME_DEF_STMT (op0);
1884 if (gimple_code (def0) != GIMPLE_ASSIGN)
1885 return false;
1886 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def0)) != tcc_comparison)
1887 return false;
1888 pred0 = get_pred_info_from_cmp (def0);
1890 for (i = 1; i < n; ++i)
1892 gimple *def;
1893 pred_info pred;
1894 tree op = gimple_phi_arg_def (phi, i);
1896 if (TREE_CODE (op) != SSA_NAME)
1897 return false;
1899 def = SSA_NAME_DEF_STMT (op);
1900 if (gimple_code (def) != GIMPLE_ASSIGN)
1901 return false;
1902 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def)) != tcc_comparison)
1903 return false;
1904 pred = get_pred_info_from_cmp (def);
1905 if (!pred_equal_p (pred, pred0))
1906 return false;
1909 *pred_p = pred0;
1910 return true;
1913 /* Normalize one predicate PRED
1914 1) if PRED can no longer be normlized, put it into NORM_PREDS.
1915 2) otherwise if PRED is of the form x != 0, follow x's definition
1916 and put normalized predicates into WORK_LIST. */
1918 static void
1919 normalize_one_pred_1 (pred_chain_union *norm_preds,
1920 pred_chain *norm_chain,
1921 pred_info pred,
1922 enum tree_code and_or_code,
1923 vec<pred_info, va_heap, vl_ptr> *work_list,
1924 hash_set<tree> *mark_set)
1926 if (!is_neq_zero_form_p (pred))
1928 if (and_or_code == BIT_IOR_EXPR)
1929 push_pred (norm_preds, pred);
1930 else
1931 norm_chain->safe_push (pred);
1932 return;
1935 gimple *def_stmt = SSA_NAME_DEF_STMT (pred.pred_lhs);
1937 if (gimple_code (def_stmt) == GIMPLE_PHI
1938 && is_degenerated_phi (def_stmt, &pred))
1939 work_list->safe_push (pred);
1940 else if (gimple_code (def_stmt) == GIMPLE_PHI && and_or_code == BIT_IOR_EXPR)
1942 int i, n;
1943 n = gimple_phi_num_args (def_stmt);
1945 /* If we see non zero constant, we should punt. The predicate
1946 * should be one guarding the phi edge. */
1947 for (i = 0; i < n; ++i)
1949 tree op = gimple_phi_arg_def (def_stmt, i);
1950 if (TREE_CODE (op) == INTEGER_CST && !integer_zerop (op))
1952 push_pred (norm_preds, pred);
1953 return;
1957 for (i = 0; i < n; ++i)
1959 tree op = gimple_phi_arg_def (def_stmt, i);
1960 if (integer_zerop (op))
1961 continue;
1963 push_to_worklist (op, work_list, mark_set);
1966 else if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1968 if (and_or_code == BIT_IOR_EXPR)
1969 push_pred (norm_preds, pred);
1970 else
1971 norm_chain->safe_push (pred);
1973 else if (gimple_assign_rhs_code (def_stmt) == and_or_code)
1975 /* Avoid splitting up bit manipulations like x & 3 or y | 1. */
1976 if (is_gimple_min_invariant (gimple_assign_rhs2 (def_stmt)))
1978 /* But treat x & 3 as condition. */
1979 if (and_or_code == BIT_AND_EXPR)
1981 pred_info n_pred;
1982 n_pred.pred_lhs = gimple_assign_rhs1 (def_stmt);
1983 n_pred.pred_rhs = gimple_assign_rhs2 (def_stmt);
1984 n_pred.cond_code = and_or_code;
1985 n_pred.invert = false;
1986 norm_chain->safe_push (n_pred);
1989 else
1991 push_to_worklist (gimple_assign_rhs1 (def_stmt), work_list, mark_set);
1992 push_to_worklist (gimple_assign_rhs2 (def_stmt), work_list, mark_set);
1995 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt))
1996 == tcc_comparison)
1998 pred_info n_pred = get_pred_info_from_cmp (def_stmt);
1999 if (and_or_code == BIT_IOR_EXPR)
2000 push_pred (norm_preds, n_pred);
2001 else
2002 norm_chain->safe_push (n_pred);
2004 else
2006 if (and_or_code == BIT_IOR_EXPR)
2007 push_pred (norm_preds, pred);
2008 else
2009 norm_chain->safe_push (pred);
2013 /* Normalize PRED and store the normalized predicates into NORM_PREDS. */
2015 static void
2016 normalize_one_pred (pred_chain_union *norm_preds, pred_info pred)
2018 vec<pred_info, va_heap, vl_ptr> work_list = vNULL;
2019 enum tree_code and_or_code = ERROR_MARK;
2020 pred_chain norm_chain = vNULL;
2022 if (!is_neq_zero_form_p (pred))
2024 push_pred (norm_preds, pred);
2025 return;
2028 gimple *def_stmt = SSA_NAME_DEF_STMT (pred.pred_lhs);
2029 if (gimple_code (def_stmt) == GIMPLE_ASSIGN)
2030 and_or_code = gimple_assign_rhs_code (def_stmt);
2031 if (and_or_code != BIT_IOR_EXPR && and_or_code != BIT_AND_EXPR)
2033 if (TREE_CODE_CLASS (and_or_code) == tcc_comparison)
2035 pred_info n_pred = get_pred_info_from_cmp (def_stmt);
2036 push_pred (norm_preds, n_pred);
2038 else
2039 push_pred (norm_preds, pred);
2040 return;
2043 work_list.safe_push (pred);
2044 hash_set<tree> mark_set;
2046 while (!work_list.is_empty ())
2048 pred_info a_pred = work_list.pop ();
2049 normalize_one_pred_1 (norm_preds, &norm_chain, a_pred, and_or_code,
2050 &work_list, &mark_set);
2052 if (and_or_code == BIT_AND_EXPR)
2053 norm_preds->safe_push (norm_chain);
2055 work_list.release ();
2058 static void
2059 normalize_one_pred_chain (pred_chain_union *norm_preds, pred_chain one_chain)
2061 vec<pred_info, va_heap, vl_ptr> work_list = vNULL;
2062 hash_set<tree> mark_set;
2063 pred_chain norm_chain = vNULL;
2064 size_t i;
2066 for (i = 0; i < one_chain.length (); i++)
2068 work_list.safe_push (one_chain[i]);
2069 mark_set.add (one_chain[i].pred_lhs);
2072 while (!work_list.is_empty ())
2074 pred_info a_pred = work_list.pop ();
2075 normalize_one_pred_1 (0, &norm_chain, a_pred, BIT_AND_EXPR, &work_list,
2076 &mark_set);
2079 norm_preds->safe_push (norm_chain);
2080 work_list.release ();
2083 /* Normalize predicate chains PREDS and returns the normalized one. */
2085 static pred_chain_union
2086 normalize_preds (pred_chain_union preds, gimple *use_or_def, bool is_use)
2088 pred_chain_union norm_preds = vNULL;
2089 size_t n = preds.length ();
2090 size_t i;
2092 if (dump_file && dump_flags & TDF_DETAILS)
2094 fprintf (dump_file, "[BEFORE NORMALIZATION --");
2095 dump_predicates (use_or_def, preds, is_use ? "[USE]:\n" : "[DEF]:\n");
2098 for (i = 0; i < n; i++)
2100 if (preds[i].length () != 1)
2101 normalize_one_pred_chain (&norm_preds, preds[i]);
2102 else
2104 normalize_one_pred (&norm_preds, preds[i][0]);
2105 preds[i].release ();
2109 if (dump_file)
2111 fprintf (dump_file, "[AFTER NORMALIZATION -- ");
2112 dump_predicates (use_or_def, norm_preds,
2113 is_use ? "[USE]:\n" : "[DEF]:\n");
2116 destroy_predicate_vecs (&preds);
2117 return norm_preds;
2120 /* Computes the predicates that guard the use and checks
2121 if the incoming paths that have empty (or possibly
2122 empty) definition can be pruned/filtered. The function returns
2123 true if it can be determined that the use of PHI's def in
2124 USE_STMT is guarded with a predicate set not overlapping with
2125 predicate sets of all runtime paths that do not have a definition.
2127 Returns false if it is not or it can not be determined. USE_BB is
2128 the bb of the use (for phi operand use, the bb is not the bb of
2129 the phi stmt, but the src bb of the operand edge).
2131 UNINIT_OPNDS is a bit vector. If an operand of PHI is uninitialized, the
2132 corresponding bit in the vector is 1. VISITED_PHIS is a pointer
2133 set of phis being visited.
2135 *DEF_PREDS contains the (memoized) defining predicate chains of PHI.
2136 If *DEF_PREDS is the empty vector, the defining predicate chains of
2137 PHI will be computed and stored into *DEF_PREDS as needed.
2139 VISITED_PHIS is a pointer set of phis being visited. */
2141 static bool
2142 is_use_properly_guarded (gimple *use_stmt,
2143 basic_block use_bb,
2144 gphi *phi,
2145 unsigned uninit_opnds,
2146 pred_chain_union *def_preds,
2147 hash_set<gphi *> *visited_phis)
2149 basic_block phi_bb;
2150 pred_chain_union preds = vNULL;
2151 bool has_valid_preds = false;
2152 bool is_properly_guarded = false;
2154 if (visited_phis->add (phi))
2155 return false;
2157 phi_bb = gimple_bb (phi);
2159 if (is_non_loop_exit_postdominating (use_bb, phi_bb))
2160 return false;
2162 has_valid_preds = find_predicates (&preds, phi_bb, use_bb);
2164 if (!has_valid_preds)
2166 destroy_predicate_vecs (&preds);
2167 return false;
2170 /* Try to prune the dead incoming phi edges. */
2171 is_properly_guarded
2172 = use_pred_not_overlap_with_undef_path_pred (preds, phi, uninit_opnds,
2173 visited_phis);
2175 if (is_properly_guarded)
2177 destroy_predicate_vecs (&preds);
2178 return true;
2181 if (def_preds->is_empty ())
2183 has_valid_preds = find_def_preds (def_preds, phi);
2185 if (!has_valid_preds)
2187 destroy_predicate_vecs (&preds);
2188 return false;
2191 simplify_preds (def_preds, phi, false);
2192 *def_preds = normalize_preds (*def_preds, phi, false);
2195 simplify_preds (&preds, use_stmt, true);
2196 preds = normalize_preds (preds, use_stmt, true);
2198 is_properly_guarded = is_superset_of (*def_preds, preds);
2200 destroy_predicate_vecs (&preds);
2201 return is_properly_guarded;
2204 /* Searches through all uses of a potentially
2205 uninitialized variable defined by PHI and returns a use
2206 statement if the use is not properly guarded. It returns
2207 NULL if all uses are guarded. UNINIT_OPNDS is a bitvector
2208 holding the position(s) of uninit PHI operands. WORKLIST
2209 is the vector of candidate phis that may be updated by this
2210 function. ADDED_TO_WORKLIST is the pointer set tracking
2211 if the new phi is already in the worklist. */
2213 static gimple *
2214 find_uninit_use (gphi *phi, unsigned uninit_opnds,
2215 vec<gphi *> *worklist,
2216 hash_set<gphi *> *added_to_worklist)
2218 tree phi_result;
2219 use_operand_p use_p;
2220 gimple *use_stmt;
2221 imm_use_iterator iter;
2222 pred_chain_union def_preds = vNULL;
2223 gimple *ret = NULL;
2225 phi_result = gimple_phi_result (phi);
2227 FOR_EACH_IMM_USE_FAST (use_p, iter, phi_result)
2229 basic_block use_bb;
2231 use_stmt = USE_STMT (use_p);
2232 if (is_gimple_debug (use_stmt))
2233 continue;
2235 if (gphi *use_phi = dyn_cast<gphi *> (use_stmt))
2236 use_bb = gimple_phi_arg_edge (use_phi,
2237 PHI_ARG_INDEX_FROM_USE (use_p))->src;
2238 else
2239 use_bb = gimple_bb (use_stmt);
2241 hash_set<gphi *> visited_phis;
2242 if (is_use_properly_guarded (use_stmt, use_bb, phi, uninit_opnds,
2243 &def_preds, &visited_phis))
2244 continue;
2246 if (dump_file && (dump_flags & TDF_DETAILS))
2248 fprintf (dump_file, "[CHECK]: Found unguarded use: ");
2249 print_gimple_stmt (dump_file, use_stmt, 0, 0);
2251 /* Found one real use, return. */
2252 if (gimple_code (use_stmt) != GIMPLE_PHI)
2254 ret = use_stmt;
2255 break;
2258 /* Found a phi use that is not guarded,
2259 add the phi to the worklist. */
2260 if (!added_to_worklist->add (as_a<gphi *> (use_stmt)))
2262 if (dump_file && (dump_flags & TDF_DETAILS))
2264 fprintf (dump_file, "[WORKLIST]: Update worklist with phi: ");
2265 print_gimple_stmt (dump_file, use_stmt, 0, 0);
2268 worklist->safe_push (as_a<gphi *> (use_stmt));
2269 possibly_undefined_names->add (phi_result);
2273 destroy_predicate_vecs (&def_preds);
2274 return ret;
2277 /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions
2278 and gives warning if there exists a runtime path from the entry to a
2279 use of the PHI def that does not contain a definition. In other words,
2280 the warning is on the real use. The more dead paths that can be pruned
2281 by the compiler, the fewer false positives the warning is. WORKLIST
2282 is a vector of candidate phis to be examined. ADDED_TO_WORKLIST is
2283 a pointer set tracking if the new phi is added to the worklist or not. */
2285 static void
2286 warn_uninitialized_phi (gphi *phi, vec<gphi *> *worklist,
2287 hash_set<gphi *> *added_to_worklist)
2289 unsigned uninit_opnds;
2290 gimple *uninit_use_stmt = 0;
2291 tree uninit_op;
2292 int phiarg_index;
2293 location_t loc;
2295 /* Don't look at virtual operands. */
2296 if (virtual_operand_p (gimple_phi_result (phi)))
2297 return;
2299 uninit_opnds = compute_uninit_opnds_pos (phi);
2301 if (MASK_EMPTY (uninit_opnds))
2302 return;
2304 if (dump_file && (dump_flags & TDF_DETAILS))
2306 fprintf (dump_file, "[CHECK]: examining phi: ");
2307 print_gimple_stmt (dump_file, phi, 0, 0);
2310 /* Now check if we have any use of the value without proper guard. */
2311 uninit_use_stmt = find_uninit_use (phi, uninit_opnds,
2312 worklist, added_to_worklist);
2314 /* All uses are properly guarded. */
2315 if (!uninit_use_stmt)
2316 return;
2318 phiarg_index = MASK_FIRST_SET_BIT (uninit_opnds);
2319 uninit_op = gimple_phi_arg_def (phi, phiarg_index);
2320 if (SSA_NAME_VAR (uninit_op) == NULL_TREE)
2321 return;
2322 if (gimple_phi_arg_has_location (phi, phiarg_index))
2323 loc = gimple_phi_arg_location (phi, phiarg_index);
2324 else
2325 loc = UNKNOWN_LOCATION;
2326 warn_uninit (OPT_Wmaybe_uninitialized, uninit_op, SSA_NAME_VAR (uninit_op),
2327 SSA_NAME_VAR (uninit_op),
2328 "%qD may be used uninitialized in this function",
2329 uninit_use_stmt, loc);
2332 static bool
2333 gate_warn_uninitialized (void)
2335 return warn_uninitialized || warn_maybe_uninitialized;
2338 namespace {
2340 const pass_data pass_data_late_warn_uninitialized =
2342 GIMPLE_PASS, /* type */
2343 "uninit", /* name */
2344 OPTGROUP_NONE, /* optinfo_flags */
2345 TV_NONE, /* tv_id */
2346 PROP_ssa, /* properties_required */
2347 0, /* properties_provided */
2348 0, /* properties_destroyed */
2349 0, /* todo_flags_start */
2350 0, /* todo_flags_finish */
2353 class pass_late_warn_uninitialized : public gimple_opt_pass
2355 public:
2356 pass_late_warn_uninitialized (gcc::context *ctxt)
2357 : gimple_opt_pass (pass_data_late_warn_uninitialized, ctxt)
2360 /* opt_pass methods: */
2361 opt_pass *clone () { return new pass_late_warn_uninitialized (m_ctxt); }
2362 virtual bool gate (function *) { return gate_warn_uninitialized (); }
2363 virtual unsigned int execute (function *);
2365 }; // class pass_late_warn_uninitialized
2367 unsigned int
2368 pass_late_warn_uninitialized::execute (function *fun)
2370 basic_block bb;
2371 gphi_iterator gsi;
2372 vec<gphi *> worklist = vNULL;
2374 calculate_dominance_info (CDI_DOMINATORS);
2375 calculate_dominance_info (CDI_POST_DOMINATORS);
2376 /* Re-do the plain uninitialized variable check, as optimization may have
2377 straightened control flow. Do this first so that we don't accidentally
2378 get a "may be" warning when we'd have seen an "is" warning later. */
2379 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/1);
2381 timevar_push (TV_TREE_UNINIT);
2383 possibly_undefined_names = new hash_set<tree>;
2384 hash_set<gphi *> added_to_worklist;
2386 /* Initialize worklist */
2387 FOR_EACH_BB_FN (bb, fun)
2388 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2390 gphi *phi = gsi.phi ();
2391 size_t n, i;
2393 n = gimple_phi_num_args (phi);
2395 /* Don't look at virtual operands. */
2396 if (virtual_operand_p (gimple_phi_result (phi)))
2397 continue;
2399 for (i = 0; i < n; ++i)
2401 tree op = gimple_phi_arg_def (phi, i);
2402 if (TREE_CODE (op) == SSA_NAME && uninit_undefined_value_p (op))
2404 worklist.safe_push (phi);
2405 added_to_worklist.add (phi);
2406 if (dump_file && (dump_flags & TDF_DETAILS))
2408 fprintf (dump_file, "[WORKLIST]: add to initial list: ");
2409 print_gimple_stmt (dump_file, phi, 0, 0);
2411 break;
2416 while (worklist.length () != 0)
2418 gphi *cur_phi = 0;
2419 cur_phi = worklist.pop ();
2420 warn_uninitialized_phi (cur_phi, &worklist, &added_to_worklist);
2423 worklist.release ();
2424 delete possibly_undefined_names;
2425 possibly_undefined_names = NULL;
2426 free_dominance_info (CDI_POST_DOMINATORS);
2427 timevar_pop (TV_TREE_UNINIT);
2428 return 0;
2431 } // anon namespace
2433 gimple_opt_pass *
2434 make_pass_late_warn_uninitialized (gcc::context *ctxt)
2436 return new pass_late_warn_uninitialized (ctxt);
2439 static unsigned int
2440 execute_early_warn_uninitialized (void)
2442 /* Currently, this pass runs always but
2443 execute_late_warn_uninitialized only runs with optimization. With
2444 optimization we want to warn about possible uninitialized as late
2445 as possible, thus don't do it here. However, without
2446 optimization we need to warn here about "may be uninitialized". */
2447 calculate_dominance_info (CDI_POST_DOMINATORS);
2449 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/!optimize);
2451 /* Post-dominator information can not be reliably updated. Free it
2452 after the use. */
2454 free_dominance_info (CDI_POST_DOMINATORS);
2455 return 0;
2458 namespace {
2460 const pass_data pass_data_early_warn_uninitialized =
2462 GIMPLE_PASS, /* type */
2463 "*early_warn_uninitialized", /* name */
2464 OPTGROUP_NONE, /* optinfo_flags */
2465 TV_TREE_UNINIT, /* tv_id */
2466 PROP_ssa, /* properties_required */
2467 0, /* properties_provided */
2468 0, /* properties_destroyed */
2469 0, /* todo_flags_start */
2470 0, /* todo_flags_finish */
2473 class pass_early_warn_uninitialized : public gimple_opt_pass
2475 public:
2476 pass_early_warn_uninitialized (gcc::context *ctxt)
2477 : gimple_opt_pass (pass_data_early_warn_uninitialized, ctxt)
2480 /* opt_pass methods: */
2481 virtual bool gate (function *) { return gate_warn_uninitialized (); }
2482 virtual unsigned int execute (function *)
2484 return execute_early_warn_uninitialized ();
2487 }; // class pass_early_warn_uninitialized
2489 } // anon namespace
2491 gimple_opt_pass *
2492 make_pass_early_warn_uninitialized (gcc::context *ctxt)
2494 return new pass_early_warn_uninitialized (ctxt);