2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / tree-ssa-uninit.c
blob95d1e9cb586e834a841085b410a5414033a33ba1
1 /* Predicate aware uninitialized variable warning.
2 Copyright (C) 2001-2015 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 "input.h"
26 #include "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "flags.h"
31 #include "tm_p.h"
32 #include "predict.h"
33 #include "hard-reg-set.h"
34 #include "input.h"
35 #include "function.h"
36 #include "dominance.h"
37 #include "cfg.h"
38 #include "basic-block.h"
39 #include "gimple-pretty-print.h"
40 #include "bitmap.h"
41 #include "tree-ssa-alias.h"
42 #include "internal-fn.h"
43 #include "gimple-expr.h"
44 #include "is-a.h"
45 #include "gimple.h"
46 #include "gimple-iterator.h"
47 #include "gimple-ssa.h"
48 #include "tree-phinodes.h"
49 #include "ssa-iterators.h"
50 #include "tree-ssa.h"
51 #include "tree-inline.h"
52 #include "tree-pass.h"
53 #include "diagnostic-core.h"
54 #include "params.h"
55 #include "tree-cfg.h"
57 /* This implements the pass that does predicate aware warning on uses of
58 possibly uninitialized variables. The pass first collects the set of
59 possibly uninitialized SSA names. For each such name, it walks through
60 all its immediate uses. For each immediate use, it rebuilds the condition
61 expression (the predicate) that guards the use. The predicate is then
62 examined to see if the variable is always defined under that same condition.
63 This is done either by pruning the unrealizable paths that lead to the
64 default definitions or by checking if the predicate set that guards the
65 defining paths is a superset of the use predicate. */
68 /* Pointer set of potentially undefined ssa names, i.e.,
69 ssa names that are defined by phi with operands that
70 are not defined or potentially undefined. */
71 static hash_set<tree> *possibly_undefined_names = 0;
73 /* Bit mask handling macros. */
74 #define MASK_SET_BIT(mask, pos) mask |= (1 << pos)
75 #define MASK_TEST_BIT(mask, pos) (mask & (1 << pos))
76 #define MASK_EMPTY(mask) (mask == 0)
78 /* Returns the first bit position (starting from LSB)
79 in mask that is non zero. Returns -1 if the mask is empty. */
80 static int
81 get_mask_first_set_bit (unsigned mask)
83 int pos = 0;
84 if (mask == 0)
85 return -1;
87 while ((mask & (1 << pos)) == 0)
88 pos++;
90 return pos;
92 #define MASK_FIRST_SET_BIT(mask) get_mask_first_set_bit (mask)
94 /* Return true if T, an SSA_NAME, has an undefined value. */
95 static bool
96 has_undefined_value_p (tree t)
98 return (ssa_undefined_value_p (t)
99 || (possibly_undefined_names
100 && possibly_undefined_names->contains (t)));
105 /* Like has_undefined_value_p, but don't return true if TREE_NO_WARNING
106 is set on SSA_NAME_VAR. */
108 static inline bool
109 uninit_undefined_value_p (tree t) {
110 if (!has_undefined_value_p (t))
111 return false;
112 if (SSA_NAME_VAR (t) && TREE_NO_WARNING (SSA_NAME_VAR (t)))
113 return false;
114 return true;
117 /* Emit warnings for uninitialized variables. This is done in two passes.
119 The first pass notices real uses of SSA names with undefined values.
120 Such uses are unconditionally uninitialized, and we can be certain that
121 such a use is a mistake. This pass is run before most optimizations,
122 so that we catch as many as we can.
124 The second pass follows PHI nodes to find uses that are potentially
125 uninitialized. In this case we can't necessarily prove that the use
126 is really uninitialized. This pass is run after most optimizations,
127 so that we thread as many jumps and possible, and delete as much dead
128 code as possible, in order to reduce false positives. We also look
129 again for plain uninitialized variables, since optimization may have
130 changed conditionally uninitialized to unconditionally uninitialized. */
132 /* Emit a warning for EXPR based on variable VAR at the point in the
133 program T, an SSA_NAME, is used being uninitialized. The exact
134 warning text is in MSGID and DATA is the gimple stmt with info about
135 the location in source code. When DATA is a GIMPLE_PHI, PHIARG_IDX
136 gives which argument of the phi node to take the location from. WC
137 is the warning code. */
139 static void
140 warn_uninit (enum opt_code wc, tree t, tree expr, tree var,
141 const char *gmsgid, void *data, location_t phiarg_loc)
143 gimple context = (gimple) data;
144 location_t location, cfun_loc;
145 expanded_location xloc, floc;
147 /* Ignore COMPLEX_EXPR as initializing only a part of a complex
148 turns in a COMPLEX_EXPR with the not initialized part being
149 set to its previous (undefined) value. */
150 if (is_gimple_assign (context)
151 && gimple_assign_rhs_code (context) == COMPLEX_EXPR)
152 return;
153 if (!has_undefined_value_p (t))
154 return;
156 /* TREE_NO_WARNING either means we already warned, or the front end
157 wishes to suppress the warning. */
158 if ((context
159 && (gimple_no_warning_p (context)
160 || (gimple_assign_single_p (context)
161 && TREE_NO_WARNING (gimple_assign_rhs1 (context)))))
162 || TREE_NO_WARNING (expr))
163 return;
165 if (context != NULL && gimple_has_location (context))
166 location = gimple_location (context);
167 else if (phiarg_loc != UNKNOWN_LOCATION)
168 location = phiarg_loc;
169 else
170 location = DECL_SOURCE_LOCATION (var);
171 location = linemap_resolve_location (line_table, location,
172 LRK_SPELLING_LOCATION,
173 NULL);
174 cfun_loc = DECL_SOURCE_LOCATION (cfun->decl);
175 xloc = expand_location (location);
176 floc = expand_location (cfun_loc);
177 if (warning_at (location, wc, gmsgid, expr))
179 TREE_NO_WARNING (expr) = 1;
181 if (location == DECL_SOURCE_LOCATION (var))
182 return;
183 if (xloc.file != floc.file
184 || linemap_location_before_p (line_table,
185 location, cfun_loc)
186 || linemap_location_before_p (line_table,
187 cfun->function_end_locus,
188 location))
189 inform (DECL_SOURCE_LOCATION (var), "%qD was declared here", var);
193 static unsigned int
194 warn_uninitialized_vars (bool warn_possibly_uninitialized)
196 gimple_stmt_iterator gsi;
197 basic_block bb;
199 FOR_EACH_BB_FN (bb, cfun)
201 bool always_executed = dominated_by_p (CDI_POST_DOMINATORS,
202 single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)), bb);
203 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
205 gimple stmt = gsi_stmt (gsi);
206 use_operand_p use_p;
207 ssa_op_iter op_iter;
208 tree use;
210 if (is_gimple_debug (stmt))
211 continue;
213 /* We only do data flow with SSA_NAMEs, so that's all we
214 can warn about. */
215 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, op_iter, SSA_OP_USE)
217 use = USE_FROM_PTR (use_p);
218 if (always_executed)
219 warn_uninit (OPT_Wuninitialized, use,
220 SSA_NAME_VAR (use), SSA_NAME_VAR (use),
221 "%qD is used uninitialized in this function",
222 stmt, UNKNOWN_LOCATION);
223 else if (warn_possibly_uninitialized)
224 warn_uninit (OPT_Wmaybe_uninitialized, use,
225 SSA_NAME_VAR (use), SSA_NAME_VAR (use),
226 "%qD may be used uninitialized in this function",
227 stmt, UNKNOWN_LOCATION);
230 /* For memory the only cheap thing we can do is see if we
231 have a use of the default def of the virtual operand.
232 ??? Not so cheap would be to use the alias oracle via
233 walk_aliased_vdefs, if we don't find any aliasing vdef
234 warn as is-used-uninitialized, if we don't find an aliasing
235 vdef that kills our use (stmt_kills_ref_p), warn as
236 may-be-used-uninitialized. But this walk is quadratic and
237 so must be limited which means we would miss warning
238 opportunities. */
239 use = gimple_vuse (stmt);
240 if (use
241 && gimple_assign_single_p (stmt)
242 && !gimple_vdef (stmt)
243 && SSA_NAME_IS_DEFAULT_DEF (use))
245 tree rhs = gimple_assign_rhs1 (stmt);
246 tree base = get_base_address (rhs);
248 /* Do not warn if it can be initialized outside this function. */
249 if (TREE_CODE (base) != VAR_DECL
250 || DECL_HARD_REGISTER (base)
251 || is_global_var (base))
252 continue;
254 if (always_executed)
255 warn_uninit (OPT_Wuninitialized, use,
256 gimple_assign_rhs1 (stmt), base,
257 "%qE is used uninitialized in this function",
258 stmt, UNKNOWN_LOCATION);
259 else if (warn_possibly_uninitialized)
260 warn_uninit (OPT_Wmaybe_uninitialized, use,
261 gimple_assign_rhs1 (stmt), base,
262 "%qE may be used uninitialized in this function",
263 stmt, UNKNOWN_LOCATION);
268 return 0;
271 /* Checks if the operand OPND of PHI is defined by
272 another phi with one operand defined by this PHI,
273 but the rest operands are all defined. If yes,
274 returns true to skip this this operand as being
275 redundant. Can be enhanced to be more general. */
277 static bool
278 can_skip_redundant_opnd (tree opnd, gimple phi)
280 gimple op_def;
281 tree phi_def;
282 int i, n;
284 phi_def = gimple_phi_result (phi);
285 op_def = SSA_NAME_DEF_STMT (opnd);
286 if (gimple_code (op_def) != GIMPLE_PHI)
287 return false;
288 n = gimple_phi_num_args (op_def);
289 for (i = 0; i < n; ++i)
291 tree op = gimple_phi_arg_def (op_def, i);
292 if (TREE_CODE (op) != SSA_NAME)
293 continue;
294 if (op != phi_def && uninit_undefined_value_p (op))
295 return false;
298 return true;
301 /* Returns a bit mask holding the positions of arguments in PHI
302 that have empty (or possibly empty) definitions. */
304 static unsigned
305 compute_uninit_opnds_pos (gphi *phi)
307 size_t i, n;
308 unsigned uninit_opnds = 0;
310 n = gimple_phi_num_args (phi);
311 /* Bail out for phi with too many args. */
312 if (n > 32)
313 return 0;
315 for (i = 0; i < n; ++i)
317 tree op = gimple_phi_arg_def (phi, i);
318 if (TREE_CODE (op) == SSA_NAME
319 && uninit_undefined_value_p (op)
320 && !can_skip_redundant_opnd (op, phi))
322 if (cfun->has_nonlocal_label || cfun->calls_setjmp)
324 /* Ignore SSA_NAMEs that appear on abnormal edges
325 somewhere. */
326 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
327 continue;
329 MASK_SET_BIT (uninit_opnds, i);
332 return uninit_opnds;
335 /* Find the immediate postdominator PDOM of the specified
336 basic block BLOCK. */
338 static inline basic_block
339 find_pdom (basic_block block)
341 if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
342 return EXIT_BLOCK_PTR_FOR_FN (cfun);
343 else
345 basic_block bb
346 = get_immediate_dominator (CDI_POST_DOMINATORS, block);
347 if (! bb)
348 return EXIT_BLOCK_PTR_FOR_FN (cfun);
349 return bb;
353 /* Find the immediate DOM of the specified
354 basic block BLOCK. */
356 static inline basic_block
357 find_dom (basic_block block)
359 if (block == ENTRY_BLOCK_PTR_FOR_FN (cfun))
360 return ENTRY_BLOCK_PTR_FOR_FN (cfun);
361 else
363 basic_block bb = get_immediate_dominator (CDI_DOMINATORS, block);
364 if (! bb)
365 return ENTRY_BLOCK_PTR_FOR_FN (cfun);
366 return bb;
370 /* Returns true if BB1 is postdominating BB2 and BB1 is
371 not a loop exit bb. The loop exit bb check is simple and does
372 not cover all cases. */
374 static bool
375 is_non_loop_exit_postdominating (basic_block bb1, basic_block bb2)
377 if (!dominated_by_p (CDI_POST_DOMINATORS, bb2, bb1))
378 return false;
380 if (single_pred_p (bb1) && !single_succ_p (bb2))
381 return false;
383 return true;
386 /* Find the closest postdominator of a specified BB, which is control
387 equivalent to BB. */
389 static inline basic_block
390 find_control_equiv_block (basic_block bb)
392 basic_block pdom;
394 pdom = find_pdom (bb);
396 /* Skip the postdominating bb that is also loop exit. */
397 if (!is_non_loop_exit_postdominating (pdom, bb))
398 return NULL;
400 if (dominated_by_p (CDI_DOMINATORS, pdom, bb))
401 return pdom;
403 return NULL;
406 #define MAX_NUM_CHAINS 8
407 #define MAX_CHAIN_LEN 5
408 #define MAX_POSTDOM_CHECK 8
409 #define MAX_SWITCH_CASES 40
411 /* Computes the control dependence chains (paths of edges)
412 for DEP_BB up to the dominating basic block BB (the head node of a
413 chain should be dominated by it). CD_CHAINS is pointer to an
414 array holding the result chains. CUR_CD_CHAIN is the current
415 chain being computed. *NUM_CHAINS is total number of chains. The
416 function returns true if the information is successfully computed,
417 return false if there is no control dependence or not computed. */
419 static bool
420 compute_control_dep_chain (basic_block bb, basic_block dep_bb,
421 vec<edge> *cd_chains,
422 size_t *num_chains,
423 vec<edge> *cur_cd_chain,
424 int *num_calls)
426 edge_iterator ei;
427 edge e;
428 size_t i;
429 bool found_cd_chain = false;
430 size_t cur_chain_len = 0;
432 if (EDGE_COUNT (bb->succs) < 2)
433 return false;
435 if (*num_calls > PARAM_VALUE (PARAM_UNINIT_CONTROL_DEP_ATTEMPTS))
436 return false;
437 ++*num_calls;
439 /* Could use a set instead. */
440 cur_chain_len = cur_cd_chain->length ();
441 if (cur_chain_len > MAX_CHAIN_LEN)
442 return false;
444 for (i = 0; i < cur_chain_len; i++)
446 edge e = (*cur_cd_chain)[i];
447 /* Cycle detected. */
448 if (e->src == bb)
449 return false;
452 FOR_EACH_EDGE (e, ei, bb->succs)
454 basic_block cd_bb;
455 int post_dom_check = 0;
456 if (e->flags & (EDGE_FAKE | EDGE_ABNORMAL))
457 continue;
459 cd_bb = e->dest;
460 cur_cd_chain->safe_push (e);
461 while (!is_non_loop_exit_postdominating (cd_bb, bb))
463 if (cd_bb == dep_bb)
465 /* Found a direct control dependence. */
466 if (*num_chains < MAX_NUM_CHAINS)
468 cd_chains[*num_chains] = cur_cd_chain->copy ();
469 (*num_chains)++;
471 found_cd_chain = true;
472 /* Check path from next edge. */
473 break;
476 /* Now check if DEP_BB is indirectly control dependent on BB. */
477 if (compute_control_dep_chain (cd_bb, dep_bb, cd_chains,
478 num_chains, cur_cd_chain, num_calls))
480 found_cd_chain = true;
481 break;
484 cd_bb = find_pdom (cd_bb);
485 post_dom_check++;
486 if (cd_bb == EXIT_BLOCK_PTR_FOR_FN (cfun) || post_dom_check >
487 MAX_POSTDOM_CHECK)
488 break;
490 cur_cd_chain->pop ();
491 gcc_assert (cur_cd_chain->length () == cur_chain_len);
493 gcc_assert (cur_cd_chain->length () == cur_chain_len);
495 return found_cd_chain;
498 /* The type to represent a simple predicate */
500 typedef struct use_def_pred_info
502 tree pred_lhs;
503 tree pred_rhs;
504 enum tree_code cond_code;
505 bool invert;
506 } pred_info;
508 /* The type to represent a sequence of predicates grouped
509 with .AND. operation. */
511 typedef vec<pred_info, va_heap, vl_ptr> pred_chain;
513 /* The type to represent a sequence of pred_chains grouped
514 with .OR. operation. */
516 typedef vec<pred_chain, va_heap, vl_ptr> pred_chain_union;
518 /* Converts the chains of control dependence edges into a set of
519 predicates. A control dependence chain is represented by a vector
520 edges. DEP_CHAINS points to an array of dependence chains.
521 NUM_CHAINS is the size of the chain array. One edge in a dependence
522 chain is mapped to predicate expression represented by pred_info
523 type. One dependence chain is converted to a composite predicate that
524 is the result of AND operation of pred_info mapped to each edge.
525 A composite predicate is presented by a vector of pred_info. On
526 return, *PREDS points to the resulting array of composite predicates.
527 *NUM_PREDS is the number of composite predictes. */
529 static bool
530 convert_control_dep_chain_into_preds (vec<edge> *dep_chains,
531 size_t num_chains,
532 pred_chain_union *preds)
534 bool has_valid_pred = false;
535 size_t i, j;
536 if (num_chains == 0 || num_chains >= MAX_NUM_CHAINS)
537 return false;
539 /* Now convert the control dep chain into a set
540 of predicates. */
541 preds->reserve (num_chains);
543 for (i = 0; i < num_chains; i++)
545 vec<edge> one_cd_chain = dep_chains[i];
547 has_valid_pred = false;
548 pred_chain t_chain = vNULL;
549 for (j = 0; j < one_cd_chain.length (); j++)
551 gimple cond_stmt;
552 gimple_stmt_iterator gsi;
553 basic_block guard_bb;
554 pred_info one_pred;
555 edge e;
557 e = one_cd_chain[j];
558 guard_bb = e->src;
559 gsi = gsi_last_bb (guard_bb);
560 if (gsi_end_p (gsi))
562 has_valid_pred = false;
563 break;
565 cond_stmt = gsi_stmt (gsi);
566 if (is_gimple_call (cond_stmt)
567 && EDGE_COUNT (e->src->succs) >= 2)
569 /* Ignore EH edge. Can add assertion
570 on the other edge's flag. */
571 continue;
573 /* Skip if there is essentially one succesor. */
574 if (EDGE_COUNT (e->src->succs) == 2)
576 edge e1;
577 edge_iterator ei1;
578 bool skip = false;
580 FOR_EACH_EDGE (e1, ei1, e->src->succs)
582 if (EDGE_COUNT (e1->dest->succs) == 0)
584 skip = true;
585 break;
588 if (skip)
589 continue;
591 if (gimple_code (cond_stmt) == GIMPLE_COND)
593 one_pred.pred_lhs = gimple_cond_lhs (cond_stmt);
594 one_pred.pred_rhs = gimple_cond_rhs (cond_stmt);
595 one_pred.cond_code = gimple_cond_code (cond_stmt);
596 one_pred.invert = !!(e->flags & EDGE_FALSE_VALUE);
597 t_chain.safe_push (one_pred);
598 has_valid_pred = true;
600 else if (gswitch *gs = dyn_cast <gswitch *> (cond_stmt))
602 /* Avoid quadratic behavior. */
603 if (gimple_switch_num_labels (gs) > MAX_SWITCH_CASES)
605 has_valid_pred = false;
606 break;
608 /* Find the case label. */
609 tree l = NULL_TREE;
610 unsigned idx;
611 for (idx = 0; idx < gimple_switch_num_labels (gs); ++idx)
613 tree tl = gimple_switch_label (gs, idx);
614 if (e->dest == label_to_block (CASE_LABEL (tl)))
616 if (!l)
617 l = tl;
618 else
620 l = NULL_TREE;
621 break;
625 /* If more than one label reaches this block or the case
626 label doesn't have a single value (like the default one)
627 fail. */
628 if (!l
629 || !CASE_LOW (l)
630 || (CASE_HIGH (l) && !operand_equal_p (CASE_LOW (l),
631 CASE_HIGH (l), 0)))
633 has_valid_pred = false;
634 break;
636 one_pred.pred_lhs = gimple_switch_index (gs);
637 one_pred.pred_rhs = CASE_LOW (l);
638 one_pred.cond_code = EQ_EXPR;
639 one_pred.invert = false;
640 t_chain.safe_push (one_pred);
641 has_valid_pred = true;
643 else
645 has_valid_pred = false;
646 break;
650 if (!has_valid_pred)
651 break;
652 else
653 preds->safe_push (t_chain);
655 return has_valid_pred;
658 /* Computes all control dependence chains for USE_BB. The control
659 dependence chains are then converted to an array of composite
660 predicates pointed to by PREDS. PHI_BB is the basic block of
661 the phi whose result is used in USE_BB. */
663 static bool
664 find_predicates (pred_chain_union *preds,
665 basic_block phi_bb,
666 basic_block use_bb)
668 size_t num_chains = 0, i;
669 int num_calls = 0;
670 vec<edge> dep_chains[MAX_NUM_CHAINS];
671 auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
672 bool has_valid_pred = false;
673 basic_block cd_root = 0;
675 /* First find the closest bb that is control equivalent to PHI_BB
676 that also dominates USE_BB. */
677 cd_root = phi_bb;
678 while (dominated_by_p (CDI_DOMINATORS, use_bb, cd_root))
680 basic_block ctrl_eq_bb = find_control_equiv_block (cd_root);
681 if (ctrl_eq_bb && dominated_by_p (CDI_DOMINATORS, use_bb, ctrl_eq_bb))
682 cd_root = ctrl_eq_bb;
683 else
684 break;
687 compute_control_dep_chain (cd_root, use_bb, dep_chains, &num_chains,
688 &cur_chain, &num_calls);
690 has_valid_pred
691 = convert_control_dep_chain_into_preds (dep_chains, num_chains, preds);
692 for (i = 0; i < num_chains; i++)
693 dep_chains[i].release ();
694 return has_valid_pred;
697 /* Computes the set of incoming edges of PHI that have non empty
698 definitions of a phi chain. The collection will be done
699 recursively on operands that are defined by phis. CD_ROOT
700 is the control dependence root. *EDGES holds the result, and
701 VISITED_PHIS is a pointer set for detecting cycles. */
703 static void
704 collect_phi_def_edges (gphi *phi, basic_block cd_root,
705 vec<edge> *edges,
706 hash_set<gimple> *visited_phis)
708 size_t i, n;
709 edge opnd_edge;
710 tree opnd;
712 if (visited_phis->add (phi))
713 return;
715 n = gimple_phi_num_args (phi);
716 for (i = 0; i < n; i++)
718 opnd_edge = gimple_phi_arg_edge (phi, i);
719 opnd = gimple_phi_arg_def (phi, i);
721 if (TREE_CODE (opnd) != SSA_NAME)
723 if (dump_file && (dump_flags & TDF_DETAILS))
725 fprintf (dump_file, "\n[CHECK] Found def edge %d in ", (int)i);
726 print_gimple_stmt (dump_file, phi, 0, 0);
728 edges->safe_push (opnd_edge);
730 else
732 gimple def = SSA_NAME_DEF_STMT (opnd);
734 if (gimple_code (def) == GIMPLE_PHI
735 && dominated_by_p (CDI_DOMINATORS,
736 gimple_bb (def), cd_root))
737 collect_phi_def_edges (as_a <gphi *> (def), cd_root, edges,
738 visited_phis);
739 else if (!uninit_undefined_value_p (opnd))
741 if (dump_file && (dump_flags & TDF_DETAILS))
743 fprintf (dump_file, "\n[CHECK] Found def edge %d in ", (int)i);
744 print_gimple_stmt (dump_file, phi, 0, 0);
746 edges->safe_push (opnd_edge);
752 /* For each use edge of PHI, computes all control dependence chains.
753 The control dependence chains are then converted to an array of
754 composite predicates pointed to by PREDS. */
756 static bool
757 find_def_preds (pred_chain_union *preds, gphi *phi)
759 size_t num_chains = 0, i, n;
760 vec<edge> dep_chains[MAX_NUM_CHAINS];
761 auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
762 vec<edge> def_edges = vNULL;
763 bool has_valid_pred = false;
764 basic_block phi_bb, cd_root = 0;
766 phi_bb = gimple_bb (phi);
767 /* First find the closest dominating bb to be
768 the control dependence root */
769 cd_root = find_dom (phi_bb);
770 if (!cd_root)
771 return false;
773 hash_set<gimple> visited_phis;
774 collect_phi_def_edges (phi, cd_root, &def_edges, &visited_phis);
776 n = def_edges.length ();
777 if (n == 0)
778 return false;
780 for (i = 0; i < n; i++)
782 size_t prev_nc, j;
783 int num_calls = 0;
784 edge opnd_edge;
786 opnd_edge = def_edges[i];
787 prev_nc = num_chains;
788 compute_control_dep_chain (cd_root, opnd_edge->src, dep_chains,
789 &num_chains, &cur_chain, &num_calls);
791 /* Now update the newly added chains with
792 the phi operand edge: */
793 if (EDGE_COUNT (opnd_edge->src->succs) > 1)
795 if (prev_nc == num_chains && num_chains < MAX_NUM_CHAINS)
796 dep_chains[num_chains++] = vNULL;
797 for (j = prev_nc; j < num_chains; j++)
798 dep_chains[j].safe_push (opnd_edge);
802 has_valid_pred
803 = convert_control_dep_chain_into_preds (dep_chains, num_chains, preds);
804 for (i = 0; i < num_chains; i++)
805 dep_chains[i].release ();
806 return has_valid_pred;
809 /* Dumps the predicates (PREDS) for USESTMT. */
811 static void
812 dump_predicates (gimple usestmt, pred_chain_union preds,
813 const char* msg)
815 size_t i, j;
816 pred_chain one_pred_chain = vNULL;
817 fprintf (dump_file, "%s", msg);
818 print_gimple_stmt (dump_file, usestmt, 0, 0);
819 fprintf (dump_file, "is guarded by :\n\n");
820 size_t num_preds = preds.length ();
821 /* Do some dumping here: */
822 for (i = 0; i < num_preds; i++)
824 size_t np;
826 one_pred_chain = preds[i];
827 np = one_pred_chain.length ();
829 for (j = 0; j < np; j++)
831 pred_info one_pred = one_pred_chain[j];
832 if (one_pred.invert)
833 fprintf (dump_file, " (.NOT.) ");
834 print_generic_expr (dump_file, one_pred.pred_lhs, 0);
835 fprintf (dump_file, " %s ", op_symbol_code (one_pred.cond_code));
836 print_generic_expr (dump_file, one_pred.pred_rhs, 0);
837 if (j < np - 1)
838 fprintf (dump_file, " (.AND.) ");
839 else
840 fprintf (dump_file, "\n");
842 if (i < num_preds - 1)
843 fprintf (dump_file, "(.OR.)\n");
844 else
845 fprintf (dump_file, "\n\n");
849 /* Destroys the predicate set *PREDS. */
851 static void
852 destroy_predicate_vecs (pred_chain_union preds)
854 size_t i;
856 size_t n = preds.length ();
857 for (i = 0; i < n; i++)
858 preds[i].release ();
859 preds.release ();
863 /* Computes the 'normalized' conditional code with operand
864 swapping and condition inversion. */
866 static enum tree_code
867 get_cmp_code (enum tree_code orig_cmp_code,
868 bool swap_cond, bool invert)
870 enum tree_code tc = orig_cmp_code;
872 if (swap_cond)
873 tc = swap_tree_comparison (orig_cmp_code);
874 if (invert)
875 tc = invert_tree_comparison (tc, false);
877 switch (tc)
879 case LT_EXPR:
880 case LE_EXPR:
881 case GT_EXPR:
882 case GE_EXPR:
883 case EQ_EXPR:
884 case NE_EXPR:
885 break;
886 default:
887 return ERROR_MARK;
889 return tc;
892 /* Returns true if VAL falls in the range defined by BOUNDARY and CMPC, i.e.
893 all values in the range satisfies (x CMPC BOUNDARY) == true. */
895 static bool
896 is_value_included_in (tree val, tree boundary, enum tree_code cmpc)
898 bool inverted = false;
899 bool is_unsigned;
900 bool result;
902 /* Only handle integer constant here. */
903 if (TREE_CODE (val) != INTEGER_CST
904 || TREE_CODE (boundary) != INTEGER_CST)
905 return true;
907 is_unsigned = TYPE_UNSIGNED (TREE_TYPE (val));
909 if (cmpc == GE_EXPR || cmpc == GT_EXPR
910 || cmpc == NE_EXPR)
912 cmpc = invert_tree_comparison (cmpc, false);
913 inverted = true;
916 if (is_unsigned)
918 if (cmpc == EQ_EXPR)
919 result = tree_int_cst_equal (val, boundary);
920 else if (cmpc == LT_EXPR)
921 result = tree_int_cst_lt (val, boundary);
922 else
924 gcc_assert (cmpc == LE_EXPR);
925 result = tree_int_cst_le (val, boundary);
928 else
930 if (cmpc == EQ_EXPR)
931 result = tree_int_cst_equal (val, boundary);
932 else if (cmpc == LT_EXPR)
933 result = tree_int_cst_lt (val, boundary);
934 else
936 gcc_assert (cmpc == LE_EXPR);
937 result = (tree_int_cst_equal (val, boundary)
938 || tree_int_cst_lt (val, boundary));
942 if (inverted)
943 result ^= 1;
945 return result;
948 /* Returns true if PRED is common among all the predicate
949 chains (PREDS) (and therefore can be factored out).
950 NUM_PRED_CHAIN is the size of array PREDS. */
952 static bool
953 find_matching_predicate_in_rest_chains (pred_info pred,
954 pred_chain_union preds,
955 size_t num_pred_chains)
957 size_t i, j, n;
959 /* Trival case. */
960 if (num_pred_chains == 1)
961 return true;
963 for (i = 1; i < num_pred_chains; i++)
965 bool found = false;
966 pred_chain one_chain = preds[i];
967 n = one_chain.length ();
968 for (j = 0; j < n; j++)
970 pred_info pred2 = one_chain[j];
971 /* Can relax the condition comparison to not
972 use address comparison. However, the most common
973 case is that multiple control dependent paths share
974 a common path prefix, so address comparison should
975 be ok. */
977 if (operand_equal_p (pred2.pred_lhs, pred.pred_lhs, 0)
978 && operand_equal_p (pred2.pred_rhs, pred.pred_rhs, 0)
979 && pred2.invert == pred.invert)
981 found = true;
982 break;
985 if (!found)
986 return false;
988 return true;
991 /* Forward declaration. */
992 static bool
993 is_use_properly_guarded (gimple use_stmt,
994 basic_block use_bb,
995 gphi *phi,
996 unsigned uninit_opnds,
997 hash_set<gphi *> *visited_phis);
999 /* Returns true if all uninitialized opnds are pruned. Returns false
1000 otherwise. PHI is the phi node with uninitialized operands,
1001 UNINIT_OPNDS is the bitmap of the uninitialize operand positions,
1002 FLAG_DEF is the statement defining the flag guarding the use of the
1003 PHI output, BOUNDARY_CST is the const value used in the predicate
1004 associated with the flag, CMP_CODE is the comparison code used in
1005 the predicate, VISITED_PHIS is the pointer set of phis visited, and
1006 VISITED_FLAG_PHIS is the pointer to the pointer set of flag definitions
1007 that are also phis.
1009 Example scenario:
1011 BB1:
1012 flag_1 = phi <0, 1> // (1)
1013 var_1 = phi <undef, some_val>
1016 BB2:
1017 flag_2 = phi <0, flag_1, flag_1> // (2)
1018 var_2 = phi <undef, var_1, var_1>
1019 if (flag_2 == 1)
1020 goto BB3;
1022 BB3:
1023 use of var_2 // (3)
1025 Because some flag arg in (1) is not constant, if we do not look into the
1026 flag phis recursively, it is conservatively treated as unknown and var_1
1027 is thought to be flowed into use at (3). Since var_1 is potentially uninitialized
1028 a false warning will be emitted. Checking recursively into (1), the compiler can
1029 find out that only some_val (which is defined) can flow into (3) which is OK.
1033 static bool
1034 prune_uninit_phi_opnds_in_unrealizable_paths (gphi *phi,
1035 unsigned uninit_opnds,
1036 gphi *flag_def,
1037 tree boundary_cst,
1038 enum tree_code cmp_code,
1039 hash_set<gphi *> *visited_phis,
1040 bitmap *visited_flag_phis)
1042 unsigned i;
1044 for (i = 0; i < MIN (32, gimple_phi_num_args (flag_def)); i++)
1046 tree flag_arg;
1048 if (!MASK_TEST_BIT (uninit_opnds, i))
1049 continue;
1051 flag_arg = gimple_phi_arg_def (flag_def, i);
1052 if (!is_gimple_constant (flag_arg))
1054 gphi *flag_arg_def, *phi_arg_def;
1055 tree phi_arg;
1056 unsigned uninit_opnds_arg_phi;
1058 if (TREE_CODE (flag_arg) != SSA_NAME)
1059 return false;
1060 flag_arg_def = dyn_cast <gphi *> (SSA_NAME_DEF_STMT (flag_arg));
1061 if (!flag_arg_def)
1062 return false;
1064 phi_arg = gimple_phi_arg_def (phi, i);
1065 if (TREE_CODE (phi_arg) != SSA_NAME)
1066 return false;
1068 phi_arg_def = dyn_cast <gphi *> (SSA_NAME_DEF_STMT (phi_arg));
1069 if (!phi_arg_def)
1070 return false;
1072 if (gimple_bb (phi_arg_def) != gimple_bb (flag_arg_def))
1073 return false;
1075 if (!*visited_flag_phis)
1076 *visited_flag_phis = BITMAP_ALLOC (NULL);
1078 if (bitmap_bit_p (*visited_flag_phis,
1079 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def))))
1080 return false;
1082 bitmap_set_bit (*visited_flag_phis,
1083 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def)));
1085 /* Now recursively prune the uninitialized phi args. */
1086 uninit_opnds_arg_phi = compute_uninit_opnds_pos (phi_arg_def);
1087 if (!prune_uninit_phi_opnds_in_unrealizable_paths
1088 (phi_arg_def, uninit_opnds_arg_phi, flag_arg_def,
1089 boundary_cst, cmp_code, visited_phis, visited_flag_phis))
1090 return false;
1092 bitmap_clear_bit (*visited_flag_phis,
1093 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def)));
1094 continue;
1097 /* Now check if the constant is in the guarded range. */
1098 if (is_value_included_in (flag_arg, boundary_cst, cmp_code))
1100 tree opnd;
1101 gimple opnd_def;
1103 /* Now that we know that this undefined edge is not
1104 pruned. If the operand is defined by another phi,
1105 we can further prune the incoming edges of that
1106 phi by checking the predicates of this operands. */
1108 opnd = gimple_phi_arg_def (phi, i);
1109 opnd_def = SSA_NAME_DEF_STMT (opnd);
1110 if (gphi *opnd_def_phi = dyn_cast <gphi *> (opnd_def))
1112 edge opnd_edge;
1113 unsigned uninit_opnds2
1114 = compute_uninit_opnds_pos (opnd_def_phi);
1115 gcc_assert (!MASK_EMPTY (uninit_opnds2));
1116 opnd_edge = gimple_phi_arg_edge (phi, i);
1117 if (!is_use_properly_guarded (phi,
1118 opnd_edge->src,
1119 opnd_def_phi,
1120 uninit_opnds2,
1121 visited_phis))
1122 return false;
1124 else
1125 return false;
1129 return true;
1132 /* A helper function that determines if the predicate set
1133 of the use is not overlapping with that of the uninit paths.
1134 The most common senario of guarded use is in Example 1:
1135 Example 1:
1136 if (some_cond)
1138 x = ...;
1139 flag = true;
1142 ... some code ...
1144 if (flag)
1145 use (x);
1147 The real world examples are usually more complicated, but similar
1148 and usually result from inlining:
1150 bool init_func (int * x)
1152 if (some_cond)
1153 return false;
1154 *x = ..
1155 return true;
1158 void foo(..)
1160 int x;
1162 if (!init_func(&x))
1163 return;
1165 .. some_code ...
1166 use (x);
1169 Another possible use scenario is in the following trivial example:
1171 Example 2:
1172 if (n > 0)
1173 x = 1;
1175 if (n > 0)
1177 if (m < 2)
1178 .. = x;
1181 Predicate analysis needs to compute the composite predicate:
1183 1) 'x' use predicate: (n > 0) .AND. (m < 2)
1184 2) 'x' default value (non-def) predicate: .NOT. (n > 0)
1185 (the predicate chain for phi operand defs can be computed
1186 starting from a bb that is control equivalent to the phi's
1187 bb and is dominating the operand def.)
1189 and check overlapping:
1190 (n > 0) .AND. (m < 2) .AND. (.NOT. (n > 0))
1191 <==> false
1193 This implementation provides framework that can handle
1194 scenarios. (Note that many simple cases are handled properly
1195 without the predicate analysis -- this is due to jump threading
1196 transformation which eliminates the merge point thus makes
1197 path sensitive analysis unnecessary.)
1199 NUM_PREDS is the number is the number predicate chains, PREDS is
1200 the array of chains, PHI is the phi node whose incoming (undefined)
1201 paths need to be pruned, and UNINIT_OPNDS is the bitmap holding
1202 uninit operand positions. VISITED_PHIS is the pointer set of phi
1203 stmts being checked. */
1206 static bool
1207 use_pred_not_overlap_with_undef_path_pred (pred_chain_union preds,
1208 gphi *phi, unsigned uninit_opnds,
1209 hash_set<gphi *> *visited_phis)
1211 unsigned int i, n;
1212 gimple flag_def = 0;
1213 tree boundary_cst = 0;
1214 enum tree_code cmp_code;
1215 bool swap_cond = false;
1216 bool invert = false;
1217 pred_chain the_pred_chain = vNULL;
1218 bitmap visited_flag_phis = NULL;
1219 bool all_pruned = false;
1220 size_t num_preds = preds.length ();
1222 gcc_assert (num_preds > 0);
1223 /* Find within the common prefix of multiple predicate chains
1224 a predicate that is a comparison of a flag variable against
1225 a constant. */
1226 the_pred_chain = preds[0];
1227 n = the_pred_chain.length ();
1228 for (i = 0; i < n; i++)
1230 tree cond_lhs, cond_rhs, flag = 0;
1232 pred_info the_pred = the_pred_chain[i];
1234 invert = the_pred.invert;
1235 cond_lhs = the_pred.pred_lhs;
1236 cond_rhs = the_pred.pred_rhs;
1237 cmp_code = the_pred.cond_code;
1239 if (cond_lhs != NULL_TREE && TREE_CODE (cond_lhs) == SSA_NAME
1240 && cond_rhs != NULL_TREE && is_gimple_constant (cond_rhs))
1242 boundary_cst = cond_rhs;
1243 flag = cond_lhs;
1245 else if (cond_rhs != NULL_TREE && TREE_CODE (cond_rhs) == SSA_NAME
1246 && cond_lhs != NULL_TREE && is_gimple_constant (cond_lhs))
1248 boundary_cst = cond_lhs;
1249 flag = cond_rhs;
1250 swap_cond = true;
1253 if (!flag)
1254 continue;
1256 flag_def = SSA_NAME_DEF_STMT (flag);
1258 if (!flag_def)
1259 continue;
1261 if ((gimple_code (flag_def) == GIMPLE_PHI)
1262 && (gimple_bb (flag_def) == gimple_bb (phi))
1263 && find_matching_predicate_in_rest_chains (the_pred, preds,
1264 num_preds))
1265 break;
1267 flag_def = 0;
1270 if (!flag_def)
1271 return false;
1273 /* Now check all the uninit incoming edge has a constant flag value
1274 that is in conflict with the use guard/predicate. */
1275 cmp_code = get_cmp_code (cmp_code, swap_cond, invert);
1277 if (cmp_code == ERROR_MARK)
1278 return false;
1280 all_pruned = prune_uninit_phi_opnds_in_unrealizable_paths (phi,
1281 uninit_opnds,
1282 as_a <gphi *> (flag_def),
1283 boundary_cst,
1284 cmp_code,
1285 visited_phis,
1286 &visited_flag_phis);
1288 if (visited_flag_phis)
1289 BITMAP_FREE (visited_flag_phis);
1291 return all_pruned;
1294 /* The helper function returns true if two predicates X1 and X2
1295 are equivalent. It assumes the expressions have already
1296 properly re-associated. */
1298 static inline bool
1299 pred_equal_p (pred_info x1, pred_info x2)
1301 enum tree_code c1, c2;
1302 if (!operand_equal_p (x1.pred_lhs, x2.pred_lhs, 0)
1303 || !operand_equal_p (x1.pred_rhs, x2.pred_rhs, 0))
1304 return false;
1306 c1 = x1.cond_code;
1307 if (x1.invert != x2.invert)
1308 c2 = invert_tree_comparison (x2.cond_code, false);
1309 else
1310 c2 = x2.cond_code;
1312 return c1 == c2;
1315 /* Returns true if the predication is testing !=. */
1317 static inline bool
1318 is_neq_relop_p (pred_info pred)
1321 return (pred.cond_code == NE_EXPR && !pred.invert)
1322 || (pred.cond_code == EQ_EXPR && pred.invert);
1325 /* Returns true if pred is of the form X != 0. */
1327 static inline bool
1328 is_neq_zero_form_p (pred_info pred)
1330 if (!is_neq_relop_p (pred) || !integer_zerop (pred.pred_rhs)
1331 || TREE_CODE (pred.pred_lhs) != SSA_NAME)
1332 return false;
1333 return true;
1336 /* The helper function returns true if two predicates X1
1337 is equivalent to X2 != 0. */
1339 static inline bool
1340 pred_expr_equal_p (pred_info x1, tree x2)
1342 if (!is_neq_zero_form_p (x1))
1343 return false;
1345 return operand_equal_p (x1.pred_lhs, x2, 0);
1348 /* Returns true of the domain of single predicate expression
1349 EXPR1 is a subset of that of EXPR2. Returns false if it
1350 can not be proved. */
1352 static bool
1353 is_pred_expr_subset_of (pred_info expr1, pred_info expr2)
1355 enum tree_code code1, code2;
1357 if (pred_equal_p (expr1, expr2))
1358 return true;
1360 if ((TREE_CODE (expr1.pred_rhs) != INTEGER_CST)
1361 || (TREE_CODE (expr2.pred_rhs) != INTEGER_CST))
1362 return false;
1364 if (!operand_equal_p (expr1.pred_lhs, expr2.pred_lhs, 0))
1365 return false;
1367 code1 = expr1.cond_code;
1368 if (expr1.invert)
1369 code1 = invert_tree_comparison (code1, false);
1370 code2 = expr2.cond_code;
1371 if (expr2.invert)
1372 code2 = invert_tree_comparison (code2, false);
1374 if ((code1 == EQ_EXPR || code1 == BIT_AND_EXPR)
1375 && code2 == BIT_AND_EXPR)
1376 return wi::eq_p (expr1.pred_rhs,
1377 wi::bit_and (expr1.pred_rhs, expr2.pred_rhs));
1379 if (code1 != code2 && code2 != NE_EXPR)
1380 return false;
1382 if (is_value_included_in (expr1.pred_rhs, expr2.pred_rhs, code2))
1383 return true;
1385 return false;
1388 /* Returns true if the domain of PRED1 is a subset
1389 of that of PRED2. Returns false if it can not be proved so. */
1391 static bool
1392 is_pred_chain_subset_of (pred_chain pred1,
1393 pred_chain pred2)
1395 size_t np1, np2, i1, i2;
1397 np1 = pred1.length ();
1398 np2 = pred2.length ();
1400 for (i2 = 0; i2 < np2; i2++)
1402 bool found = false;
1403 pred_info info2 = pred2[i2];
1404 for (i1 = 0; i1 < np1; i1++)
1406 pred_info info1 = pred1[i1];
1407 if (is_pred_expr_subset_of (info1, info2))
1409 found = true;
1410 break;
1413 if (!found)
1414 return false;
1416 return true;
1419 /* Returns true if the domain defined by
1420 one pred chain ONE_PRED is a subset of the domain
1421 of *PREDS. It returns false if ONE_PRED's domain is
1422 not a subset of any of the sub-domains of PREDS
1423 (corresponding to each individual chains in it), even
1424 though it may be still be a subset of whole domain
1425 of PREDS which is the union (ORed) of all its subdomains.
1426 In other words, the result is conservative. */
1428 static bool
1429 is_included_in (pred_chain one_pred, pred_chain_union preds)
1431 size_t i;
1432 size_t n = preds.length ();
1434 for (i = 0; i < n; i++)
1436 if (is_pred_chain_subset_of (one_pred, preds[i]))
1437 return true;
1440 return false;
1443 /* Compares two predicate sets PREDS1 and PREDS2 and returns
1444 true if the domain defined by PREDS1 is a superset
1445 of PREDS2's domain. N1 and N2 are array sizes of PREDS1 and
1446 PREDS2 respectively. The implementation chooses not to build
1447 generic trees (and relying on the folding capability of the
1448 compiler), but instead performs brute force comparison of
1449 individual predicate chains (won't be a compile time problem
1450 as the chains are pretty short). When the function returns
1451 false, it does not necessarily mean *PREDS1 is not a superset
1452 of *PREDS2, but mean it may not be so since the analysis can
1453 not prove it. In such cases, false warnings may still be
1454 emitted. */
1456 static bool
1457 is_superset_of (pred_chain_union preds1, pred_chain_union preds2)
1459 size_t i, n2;
1460 pred_chain one_pred_chain = vNULL;
1462 n2 = preds2.length ();
1464 for (i = 0; i < n2; i++)
1466 one_pred_chain = preds2[i];
1467 if (!is_included_in (one_pred_chain, preds1))
1468 return false;
1471 return true;
1474 /* Returns true if TC is AND or OR. */
1476 static inline bool
1477 is_and_or_or_p (enum tree_code tc, tree type)
1479 return (tc == BIT_IOR_EXPR
1480 || (tc == BIT_AND_EXPR
1481 && (type == 0 || TREE_CODE (type) == BOOLEAN_TYPE)));
1484 /* Returns true if X1 is the negate of X2. */
1486 static inline bool
1487 pred_neg_p (pred_info x1, pred_info x2)
1489 enum tree_code c1, c2;
1490 if (!operand_equal_p (x1.pred_lhs, x2.pred_lhs, 0)
1491 || !operand_equal_p (x1.pred_rhs, x2.pred_rhs, 0))
1492 return false;
1494 c1 = x1.cond_code;
1495 if (x1.invert == x2.invert)
1496 c2 = invert_tree_comparison (x2.cond_code, false);
1497 else
1498 c2 = x2.cond_code;
1500 return c1 == c2;
1503 /* 1) ((x IOR y) != 0) AND (x != 0) is equivalent to (x != 0);
1504 2) (X AND Y) OR (!X AND Y) is equivalent to Y;
1505 3) X OR (!X AND Y) is equivalent to (X OR Y);
1506 4) ((x IAND y) != 0) || (x != 0 AND y != 0)) is equivalent to
1507 (x != 0 AND y != 0)
1508 5) (X AND Y) OR (!X AND Z) OR (!Y AND Z) is equivalent to
1509 (X AND Y) OR Z
1511 PREDS is the predicate chains, and N is the number of chains. */
1513 /* Helper function to implement rule 1 above. ONE_CHAIN is
1514 the AND predication to be simplified. */
1516 static void
1517 simplify_pred (pred_chain *one_chain)
1519 size_t i, j, n;
1520 bool simplified = false;
1521 pred_chain s_chain = vNULL;
1523 n = one_chain->length ();
1525 for (i = 0; i < n; i++)
1527 pred_info *a_pred = &(*one_chain)[i];
1529 if (!a_pred->pred_lhs)
1530 continue;
1531 if (!is_neq_zero_form_p (*a_pred))
1532 continue;
1534 gimple def_stmt = SSA_NAME_DEF_STMT (a_pred->pred_lhs);
1535 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1536 continue;
1537 if (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR)
1539 for (j = 0; j < n; j++)
1541 pred_info *b_pred = &(*one_chain)[j];
1543 if (!b_pred->pred_lhs)
1544 continue;
1545 if (!is_neq_zero_form_p (*b_pred))
1546 continue;
1548 if (pred_expr_equal_p (*b_pred, gimple_assign_rhs1 (def_stmt))
1549 || pred_expr_equal_p (*b_pred, gimple_assign_rhs2 (def_stmt)))
1551 /* Mark a_pred for removal. */
1552 a_pred->pred_lhs = NULL;
1553 a_pred->pred_rhs = NULL;
1554 simplified = true;
1555 break;
1561 if (!simplified)
1562 return;
1564 for (i = 0; i < n; i++)
1566 pred_info *a_pred = &(*one_chain)[i];
1567 if (!a_pred->pred_lhs)
1568 continue;
1569 s_chain.safe_push (*a_pred);
1572 one_chain->release ();
1573 *one_chain = s_chain;
1576 /* The helper function implements the rule 2 for the
1577 OR predicate PREDS.
1579 2) (X AND Y) OR (!X AND Y) is equivalent to Y. */
1581 static bool
1582 simplify_preds_2 (pred_chain_union *preds)
1584 size_t i, j, n;
1585 bool simplified = false;
1586 pred_chain_union s_preds = vNULL;
1588 /* (X AND Y) OR (!X AND Y) is equivalent to Y.
1589 (X AND Y) OR (X AND !Y) is equivalent to X. */
1591 n = preds->length ();
1592 for (i = 0; i < n; i++)
1594 pred_info x, y;
1595 pred_chain *a_chain = &(*preds)[i];
1597 if (a_chain->length () != 2)
1598 continue;
1600 x = (*a_chain)[0];
1601 y = (*a_chain)[1];
1603 for (j = 0; j < n; j++)
1605 pred_chain *b_chain;
1606 pred_info x2, y2;
1608 if (j == i)
1609 continue;
1611 b_chain = &(*preds)[j];
1612 if (b_chain->length () != 2)
1613 continue;
1615 x2 = (*b_chain)[0];
1616 y2 = (*b_chain)[1];
1618 if (pred_equal_p (x, x2) && pred_neg_p (y, y2))
1620 /* Kill a_chain. */
1621 a_chain->release ();
1622 b_chain->release ();
1623 b_chain->safe_push (x);
1624 simplified = true;
1625 break;
1627 if (pred_neg_p (x, x2) && pred_equal_p (y, y2))
1629 /* Kill a_chain. */
1630 a_chain->release ();
1631 b_chain->release ();
1632 b_chain->safe_push (y);
1633 simplified = true;
1634 break;
1638 /* Now clean up the chain. */
1639 if (simplified)
1641 for (i = 0; i < n; i++)
1643 if ((*preds)[i].is_empty ())
1644 continue;
1645 s_preds.safe_push ((*preds)[i]);
1647 preds->release ();
1648 (*preds) = s_preds;
1649 s_preds = vNULL;
1652 return simplified;
1655 /* The helper function implements the rule 2 for the
1656 OR predicate PREDS.
1658 3) x OR (!x AND y) is equivalent to x OR y. */
1660 static bool
1661 simplify_preds_3 (pred_chain_union *preds)
1663 size_t i, j, n;
1664 bool simplified = false;
1666 /* Now iteratively simplify X OR (!X AND Z ..)
1667 into X OR (Z ...). */
1669 n = preds->length ();
1670 if (n < 2)
1671 return false;
1673 for (i = 0; i < n; i++)
1675 pred_info x;
1676 pred_chain *a_chain = &(*preds)[i];
1678 if (a_chain->length () != 1)
1679 continue;
1681 x = (*a_chain)[0];
1683 for (j = 0; j < n; j++)
1685 pred_chain *b_chain;
1686 pred_info x2;
1687 size_t k;
1689 if (j == i)
1690 continue;
1692 b_chain = &(*preds)[j];
1693 if (b_chain->length () < 2)
1694 continue;
1696 for (k = 0; k < b_chain->length (); k++)
1698 x2 = (*b_chain)[k];
1699 if (pred_neg_p (x, x2))
1701 b_chain->unordered_remove (k);
1702 simplified = true;
1703 break;
1708 return simplified;
1711 /* The helper function implements the rule 4 for the
1712 OR predicate PREDS.
1714 2) ((x AND y) != 0) OR (x != 0 AND y != 0) is equivalent to
1715 (x != 0 ANd y != 0). */
1717 static bool
1718 simplify_preds_4 (pred_chain_union *preds)
1720 size_t i, j, n;
1721 bool simplified = false;
1722 pred_chain_union s_preds = vNULL;
1723 gimple def_stmt;
1725 n = preds->length ();
1726 for (i = 0; i < n; i++)
1728 pred_info z;
1729 pred_chain *a_chain = &(*preds)[i];
1731 if (a_chain->length () != 1)
1732 continue;
1734 z = (*a_chain)[0];
1736 if (!is_neq_zero_form_p (z))
1737 continue;
1739 def_stmt = SSA_NAME_DEF_STMT (z.pred_lhs);
1740 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1741 continue;
1743 if (gimple_assign_rhs_code (def_stmt) != BIT_AND_EXPR)
1744 continue;
1746 for (j = 0; j < n; j++)
1748 pred_chain *b_chain;
1749 pred_info x2, y2;
1751 if (j == i)
1752 continue;
1754 b_chain = &(*preds)[j];
1755 if (b_chain->length () != 2)
1756 continue;
1758 x2 = (*b_chain)[0];
1759 y2 = (*b_chain)[1];
1760 if (!is_neq_zero_form_p (x2)
1761 || !is_neq_zero_form_p (y2))
1762 continue;
1764 if ((pred_expr_equal_p (x2, gimple_assign_rhs1 (def_stmt))
1765 && pred_expr_equal_p (y2, gimple_assign_rhs2 (def_stmt)))
1766 || (pred_expr_equal_p (x2, gimple_assign_rhs2 (def_stmt))
1767 && pred_expr_equal_p (y2, gimple_assign_rhs1 (def_stmt))))
1769 /* Kill a_chain. */
1770 a_chain->release ();
1771 simplified = true;
1772 break;
1776 /* Now clean up the chain. */
1777 if (simplified)
1779 for (i = 0; i < n; i++)
1781 if ((*preds)[i].is_empty ())
1782 continue;
1783 s_preds.safe_push ((*preds)[i]);
1785 preds->release ();
1786 (*preds) = s_preds;
1787 s_preds = vNULL;
1790 return simplified;
1794 /* This function simplifies predicates in PREDS. */
1796 static void
1797 simplify_preds (pred_chain_union *preds, gimple use_or_def, bool is_use)
1799 size_t i, n;
1800 bool changed = false;
1802 if (dump_file && dump_flags & TDF_DETAILS)
1804 fprintf (dump_file, "[BEFORE SIMPLICATION -- ");
1805 dump_predicates (use_or_def, *preds, is_use ? "[USE]:\n" : "[DEF]:\n");
1808 for (i = 0; i < preds->length (); i++)
1809 simplify_pred (&(*preds)[i]);
1811 n = preds->length ();
1812 if (n < 2)
1813 return;
1817 changed = false;
1818 if (simplify_preds_2 (preds))
1819 changed = true;
1821 /* Now iteratively simplify X OR (!X AND Z ..)
1822 into X OR (Z ...). */
1823 if (simplify_preds_3 (preds))
1824 changed = true;
1826 if (simplify_preds_4 (preds))
1827 changed = true;
1829 } while (changed);
1831 return;
1834 /* This is a helper function which attempts to normalize predicate chains
1835 by following UD chains. It basically builds up a big tree of either IOR
1836 operations or AND operations, and convert the IOR tree into a
1837 pred_chain_union or BIT_AND tree into a pred_chain.
1838 Example:
1840 _3 = _2 RELOP1 _1;
1841 _6 = _5 RELOP2 _4;
1842 _9 = _8 RELOP3 _7;
1843 _10 = _3 | _6;
1844 _12 = _9 | _0;
1845 _t = _10 | _12;
1847 then _t != 0 will be normalized into a pred_chain_union
1849 (_2 RELOP1 _1) OR (_5 RELOP2 _4) OR (_8 RELOP3 _7) OR (_0 != 0)
1851 Similarly given,
1853 _3 = _2 RELOP1 _1;
1854 _6 = _5 RELOP2 _4;
1855 _9 = _8 RELOP3 _7;
1856 _10 = _3 & _6;
1857 _12 = _9 & _0;
1859 then _t != 0 will be normalized into a pred_chain:
1860 (_2 RELOP1 _1) AND (_5 RELOP2 _4) AND (_8 RELOP3 _7) AND (_0 != 0)
1864 /* This is a helper function that stores a PRED into NORM_PREDS. */
1866 inline static void
1867 push_pred (pred_chain_union *norm_preds, pred_info pred)
1869 pred_chain pred_chain = vNULL;
1870 pred_chain.safe_push (pred);
1871 norm_preds->safe_push (pred_chain);
1874 /* A helper function that creates a predicate of the form
1875 OP != 0 and push it WORK_LIST. */
1877 inline static void
1878 push_to_worklist (tree op, vec<pred_info, va_heap, vl_ptr> *work_list,
1879 hash_set<tree> *mark_set)
1881 if (mark_set->contains (op))
1882 return;
1883 mark_set->add (op);
1885 pred_info arg_pred;
1886 arg_pred.pred_lhs = op;
1887 arg_pred.pred_rhs = integer_zero_node;
1888 arg_pred.cond_code = NE_EXPR;
1889 arg_pred.invert = false;
1890 work_list->safe_push (arg_pred);
1893 /* A helper that generates a pred_info from a gimple assignment
1894 CMP_ASSIGN with comparison rhs. */
1896 static pred_info
1897 get_pred_info_from_cmp (gimple cmp_assign)
1899 pred_info n_pred;
1900 n_pred.pred_lhs = gimple_assign_rhs1 (cmp_assign);
1901 n_pred.pred_rhs = gimple_assign_rhs2 (cmp_assign);
1902 n_pred.cond_code = gimple_assign_rhs_code (cmp_assign);
1903 n_pred.invert = false;
1904 return n_pred;
1907 /* Returns true if the PHI is a degenerated phi with
1908 all args with the same value (relop). In that case, *PRED
1909 will be updated to that value. */
1911 static bool
1912 is_degenerated_phi (gimple phi, pred_info *pred_p)
1914 int i, n;
1915 tree op0;
1916 gimple def0;
1917 pred_info pred0;
1919 n = gimple_phi_num_args (phi);
1920 op0 = gimple_phi_arg_def (phi, 0);
1922 if (TREE_CODE (op0) != SSA_NAME)
1923 return false;
1925 def0 = SSA_NAME_DEF_STMT (op0);
1926 if (gimple_code (def0) != GIMPLE_ASSIGN)
1927 return false;
1928 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def0))
1929 != tcc_comparison)
1930 return false;
1931 pred0 = get_pred_info_from_cmp (def0);
1933 for (i = 1; i < n; ++i)
1935 gimple def;
1936 pred_info pred;
1937 tree op = gimple_phi_arg_def (phi, i);
1939 if (TREE_CODE (op) != SSA_NAME)
1940 return false;
1942 def = SSA_NAME_DEF_STMT (op);
1943 if (gimple_code (def) != GIMPLE_ASSIGN)
1944 return false;
1945 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def))
1946 != tcc_comparison)
1947 return false;
1948 pred = get_pred_info_from_cmp (def);
1949 if (!pred_equal_p (pred, pred0))
1950 return false;
1953 *pred_p = pred0;
1954 return true;
1957 /* Normalize one predicate PRED
1958 1) if PRED can no longer be normlized, put it into NORM_PREDS.
1959 2) otherwise if PRED is of the form x != 0, follow x's definition
1960 and put normalized predicates into WORK_LIST. */
1962 static void
1963 normalize_one_pred_1 (pred_chain_union *norm_preds,
1964 pred_chain *norm_chain,
1965 pred_info pred,
1966 enum tree_code and_or_code,
1967 vec<pred_info, va_heap, vl_ptr> *work_list,
1968 hash_set<tree> *mark_set)
1970 if (!is_neq_zero_form_p (pred))
1972 if (and_or_code == BIT_IOR_EXPR)
1973 push_pred (norm_preds, pred);
1974 else
1975 norm_chain->safe_push (pred);
1976 return;
1979 gimple def_stmt = SSA_NAME_DEF_STMT (pred.pred_lhs);
1981 if (gimple_code (def_stmt) == GIMPLE_PHI
1982 && is_degenerated_phi (def_stmt, &pred))
1983 work_list->safe_push (pred);
1984 else if (gimple_code (def_stmt) == GIMPLE_PHI
1985 && and_or_code == BIT_IOR_EXPR)
1987 int i, n;
1988 n = gimple_phi_num_args (def_stmt);
1990 /* If we see non zero constant, we should punt. The predicate
1991 * should be one guarding the phi edge. */
1992 for (i = 0; i < n; ++i)
1994 tree op = gimple_phi_arg_def (def_stmt, i);
1995 if (TREE_CODE (op) == INTEGER_CST && !integer_zerop (op))
1997 push_pred (norm_preds, pred);
1998 return;
2002 for (i = 0; i < n; ++i)
2004 tree op = gimple_phi_arg_def (def_stmt, i);
2005 if (integer_zerop (op))
2006 continue;
2008 push_to_worklist (op, work_list, mark_set);
2011 else if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
2013 if (and_or_code == BIT_IOR_EXPR)
2014 push_pred (norm_preds, pred);
2015 else
2016 norm_chain->safe_push (pred);
2018 else if (gimple_assign_rhs_code (def_stmt) == and_or_code)
2020 /* Avoid splitting up bit manipulations like x & 3 or y | 1. */
2021 if (is_gimple_min_invariant (gimple_assign_rhs2 (def_stmt)))
2023 /* But treat x & 3 as condition. */
2024 if (and_or_code == BIT_AND_EXPR)
2026 pred_info n_pred;
2027 n_pred.pred_lhs = gimple_assign_rhs1 (def_stmt);
2028 n_pred.pred_rhs = gimple_assign_rhs2 (def_stmt);
2029 n_pred.cond_code = and_or_code;
2030 n_pred.invert = false;
2031 norm_chain->safe_push (n_pred);
2034 else
2036 push_to_worklist (gimple_assign_rhs1 (def_stmt), work_list, mark_set);
2037 push_to_worklist (gimple_assign_rhs2 (def_stmt), work_list, mark_set);
2040 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt))
2041 == tcc_comparison)
2043 pred_info n_pred = get_pred_info_from_cmp (def_stmt);
2044 if (and_or_code == BIT_IOR_EXPR)
2045 push_pred (norm_preds, n_pred);
2046 else
2047 norm_chain->safe_push (n_pred);
2049 else
2051 if (and_or_code == BIT_IOR_EXPR)
2052 push_pred (norm_preds, pred);
2053 else
2054 norm_chain->safe_push (pred);
2058 /* Normalize PRED and store the normalized predicates into NORM_PREDS. */
2060 static void
2061 normalize_one_pred (pred_chain_union *norm_preds,
2062 pred_info pred)
2064 vec<pred_info, va_heap, vl_ptr> work_list = vNULL;
2065 enum tree_code and_or_code = ERROR_MARK;
2066 pred_chain norm_chain = vNULL;
2068 if (!is_neq_zero_form_p (pred))
2070 push_pred (norm_preds, pred);
2071 return;
2074 gimple def_stmt = SSA_NAME_DEF_STMT (pred.pred_lhs);
2075 if (gimple_code (def_stmt) == GIMPLE_ASSIGN)
2076 and_or_code = gimple_assign_rhs_code (def_stmt);
2077 if (and_or_code != BIT_IOR_EXPR
2078 && and_or_code != BIT_AND_EXPR)
2080 if (TREE_CODE_CLASS (and_or_code)
2081 == tcc_comparison)
2083 pred_info n_pred = get_pred_info_from_cmp (def_stmt);
2084 push_pred (norm_preds, n_pred);
2086 else
2087 push_pred (norm_preds, pred);
2088 return;
2091 work_list.safe_push (pred);
2092 hash_set<tree> mark_set;
2094 while (!work_list.is_empty ())
2096 pred_info a_pred = work_list.pop ();
2097 normalize_one_pred_1 (norm_preds, &norm_chain, a_pred,
2098 and_or_code, &work_list, &mark_set);
2100 if (and_or_code == BIT_AND_EXPR)
2101 norm_preds->safe_push (norm_chain);
2103 work_list.release ();
2106 static void
2107 normalize_one_pred_chain (pred_chain_union *norm_preds,
2108 pred_chain one_chain)
2110 vec<pred_info, va_heap, vl_ptr> work_list = vNULL;
2111 hash_set<tree> mark_set;
2112 pred_chain norm_chain = vNULL;
2113 size_t i;
2115 for (i = 0; i < one_chain.length (); i++)
2117 work_list.safe_push (one_chain[i]);
2118 mark_set.add (one_chain[i].pred_lhs);
2121 while (!work_list.is_empty ())
2123 pred_info a_pred = work_list.pop ();
2124 normalize_one_pred_1 (0, &norm_chain, a_pred,
2125 BIT_AND_EXPR, &work_list, &mark_set);
2128 norm_preds->safe_push (norm_chain);
2129 work_list.release ();
2132 /* Normalize predicate chains PREDS and returns the normalized one. */
2134 static pred_chain_union
2135 normalize_preds (pred_chain_union preds, gimple use_or_def, bool is_use)
2137 pred_chain_union norm_preds = vNULL;
2138 size_t n = preds.length ();
2139 size_t i;
2141 if (dump_file && dump_flags & TDF_DETAILS)
2143 fprintf (dump_file, "[BEFORE NORMALIZATION --");
2144 dump_predicates (use_or_def, preds, is_use ? "[USE]:\n" : "[DEF]:\n");
2147 for (i = 0; i < n; i++)
2149 if (preds[i].length () != 1)
2150 normalize_one_pred_chain (&norm_preds, preds[i]);
2151 else
2153 normalize_one_pred (&norm_preds, preds[i][0]);
2154 preds[i].release ();
2158 if (dump_file)
2160 fprintf (dump_file, "[AFTER NORMALIZATION -- ");
2161 dump_predicates (use_or_def, norm_preds, is_use ? "[USE]:\n" : "[DEF]:\n");
2164 preds.release ();
2165 return norm_preds;
2169 /* Computes the predicates that guard the use and checks
2170 if the incoming paths that have empty (or possibly
2171 empty) definition can be pruned/filtered. The function returns
2172 true if it can be determined that the use of PHI's def in
2173 USE_STMT is guarded with a predicate set not overlapping with
2174 predicate sets of all runtime paths that do not have a definition.
2175 Returns false if it is not or it can not be determined. USE_BB is
2176 the bb of the use (for phi operand use, the bb is not the bb of
2177 the phi stmt, but the src bb of the operand edge). UNINIT_OPNDS
2178 is a bit vector. If an operand of PHI is uninitialized, the
2179 corresponding bit in the vector is 1. VISIED_PHIS is a pointer
2180 set of phis being visted. */
2182 static bool
2183 is_use_properly_guarded (gimple use_stmt,
2184 basic_block use_bb,
2185 gphi *phi,
2186 unsigned uninit_opnds,
2187 hash_set<gphi *> *visited_phis)
2189 basic_block phi_bb;
2190 pred_chain_union preds = vNULL;
2191 pred_chain_union def_preds = vNULL;
2192 bool has_valid_preds = false;
2193 bool is_properly_guarded = false;
2195 if (visited_phis->add (phi))
2196 return false;
2198 phi_bb = gimple_bb (phi);
2200 if (is_non_loop_exit_postdominating (use_bb, phi_bb))
2201 return false;
2203 has_valid_preds = find_predicates (&preds, phi_bb, use_bb);
2205 if (!has_valid_preds)
2207 destroy_predicate_vecs (preds);
2208 return false;
2211 /* Try to prune the dead incoming phi edges. */
2212 is_properly_guarded
2213 = use_pred_not_overlap_with_undef_path_pred (preds, phi, uninit_opnds,
2214 visited_phis);
2216 if (is_properly_guarded)
2218 destroy_predicate_vecs (preds);
2219 return true;
2222 has_valid_preds = find_def_preds (&def_preds, phi);
2224 if (!has_valid_preds)
2226 destroy_predicate_vecs (preds);
2227 destroy_predicate_vecs (def_preds);
2228 return false;
2231 simplify_preds (&preds, use_stmt, true);
2232 preds = normalize_preds (preds, use_stmt, true);
2234 simplify_preds (&def_preds, phi, false);
2235 def_preds = normalize_preds (def_preds, phi, false);
2237 is_properly_guarded = is_superset_of (def_preds, preds);
2239 destroy_predicate_vecs (preds);
2240 destroy_predicate_vecs (def_preds);
2241 return is_properly_guarded;
2244 /* Searches through all uses of a potentially
2245 uninitialized variable defined by PHI and returns a use
2246 statement if the use is not properly guarded. It returns
2247 NULL if all uses are guarded. UNINIT_OPNDS is a bitvector
2248 holding the position(s) of uninit PHI operands. WORKLIST
2249 is the vector of candidate phis that may be updated by this
2250 function. ADDED_TO_WORKLIST is the pointer set tracking
2251 if the new phi is already in the worklist. */
2253 static gimple
2254 find_uninit_use (gphi *phi, unsigned uninit_opnds,
2255 vec<gphi *> *worklist,
2256 hash_set<gphi *> *added_to_worklist)
2258 tree phi_result;
2259 use_operand_p use_p;
2260 gimple use_stmt;
2261 imm_use_iterator iter;
2263 phi_result = gimple_phi_result (phi);
2265 FOR_EACH_IMM_USE_FAST (use_p, iter, phi_result)
2267 basic_block use_bb;
2269 use_stmt = USE_STMT (use_p);
2270 if (is_gimple_debug (use_stmt))
2271 continue;
2273 if (gphi *use_phi = dyn_cast <gphi *> (use_stmt))
2274 use_bb = gimple_phi_arg_edge (use_phi,
2275 PHI_ARG_INDEX_FROM_USE (use_p))->src;
2276 else
2277 use_bb = gimple_bb (use_stmt);
2279 hash_set<gphi *> visited_phis;
2280 if (is_use_properly_guarded (use_stmt, use_bb, phi, uninit_opnds,
2281 &visited_phis))
2282 continue;
2284 if (dump_file && (dump_flags & TDF_DETAILS))
2286 fprintf (dump_file, "[CHECK]: Found unguarded use: ");
2287 print_gimple_stmt (dump_file, use_stmt, 0, 0);
2289 /* Found one real use, return. */
2290 if (gimple_code (use_stmt) != GIMPLE_PHI)
2291 return use_stmt;
2293 /* Found a phi use that is not guarded,
2294 add the phi to the worklist. */
2295 if (!added_to_worklist->add (as_a <gphi *> (use_stmt)))
2297 if (dump_file && (dump_flags & TDF_DETAILS))
2299 fprintf (dump_file, "[WORKLIST]: Update worklist with phi: ");
2300 print_gimple_stmt (dump_file, use_stmt, 0, 0);
2303 worklist->safe_push (as_a <gphi *> (use_stmt));
2304 possibly_undefined_names->add (phi_result);
2308 return NULL;
2311 /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions
2312 and gives warning if there exists a runtime path from the entry to a
2313 use of the PHI def that does not contain a definition. In other words,
2314 the warning is on the real use. The more dead paths that can be pruned
2315 by the compiler, the fewer false positives the warning is. WORKLIST
2316 is a vector of candidate phis to be examined. ADDED_TO_WORKLIST is
2317 a pointer set tracking if the new phi is added to the worklist or not. */
2319 static void
2320 warn_uninitialized_phi (gphi *phi, vec<gphi *> *worklist,
2321 hash_set<gphi *> *added_to_worklist)
2323 unsigned uninit_opnds;
2324 gimple uninit_use_stmt = 0;
2325 tree uninit_op;
2326 int phiarg_index;
2327 location_t loc;
2329 /* Don't look at virtual operands. */
2330 if (virtual_operand_p (gimple_phi_result (phi)))
2331 return;
2333 uninit_opnds = compute_uninit_opnds_pos (phi);
2335 if (MASK_EMPTY (uninit_opnds))
2336 return;
2338 if (dump_file && (dump_flags & TDF_DETAILS))
2340 fprintf (dump_file, "[CHECK]: examining phi: ");
2341 print_gimple_stmt (dump_file, phi, 0, 0);
2344 /* Now check if we have any use of the value without proper guard. */
2345 uninit_use_stmt = find_uninit_use (phi, uninit_opnds,
2346 worklist, added_to_worklist);
2348 /* All uses are properly guarded. */
2349 if (!uninit_use_stmt)
2350 return;
2352 phiarg_index = MASK_FIRST_SET_BIT (uninit_opnds);
2353 uninit_op = gimple_phi_arg_def (phi, phiarg_index);
2354 if (SSA_NAME_VAR (uninit_op) == NULL_TREE)
2355 return;
2356 if (gimple_phi_arg_has_location (phi, phiarg_index))
2357 loc = gimple_phi_arg_location (phi, phiarg_index);
2358 else
2359 loc = UNKNOWN_LOCATION;
2360 warn_uninit (OPT_Wmaybe_uninitialized, uninit_op, SSA_NAME_VAR (uninit_op),
2361 SSA_NAME_VAR (uninit_op),
2362 "%qD may be used uninitialized in this function",
2363 uninit_use_stmt, loc);
2367 static bool
2368 gate_warn_uninitialized (void)
2370 return warn_uninitialized || warn_maybe_uninitialized;
2373 namespace {
2375 const pass_data pass_data_late_warn_uninitialized =
2377 GIMPLE_PASS, /* type */
2378 "uninit", /* name */
2379 OPTGROUP_NONE, /* optinfo_flags */
2380 TV_NONE, /* tv_id */
2381 PROP_ssa, /* properties_required */
2382 0, /* properties_provided */
2383 0, /* properties_destroyed */
2384 0, /* todo_flags_start */
2385 0, /* todo_flags_finish */
2388 class pass_late_warn_uninitialized : public gimple_opt_pass
2390 public:
2391 pass_late_warn_uninitialized (gcc::context *ctxt)
2392 : gimple_opt_pass (pass_data_late_warn_uninitialized, ctxt)
2395 /* opt_pass methods: */
2396 opt_pass * clone () { return new pass_late_warn_uninitialized (m_ctxt); }
2397 virtual bool gate (function *) { return gate_warn_uninitialized (); }
2398 virtual unsigned int execute (function *);
2400 }; // class pass_late_warn_uninitialized
2402 unsigned int
2403 pass_late_warn_uninitialized::execute (function *fun)
2405 basic_block bb;
2406 gphi_iterator gsi;
2407 vec<gphi *> worklist = vNULL;
2409 calculate_dominance_info (CDI_DOMINATORS);
2410 calculate_dominance_info (CDI_POST_DOMINATORS);
2411 /* Re-do the plain uninitialized variable check, as optimization may have
2412 straightened control flow. Do this first so that we don't accidentally
2413 get a "may be" warning when we'd have seen an "is" warning later. */
2414 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/1);
2416 timevar_push (TV_TREE_UNINIT);
2418 possibly_undefined_names = new hash_set<tree>;
2419 hash_set<gphi *> added_to_worklist;
2421 /* Initialize worklist */
2422 FOR_EACH_BB_FN (bb, fun)
2423 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2425 gphi *phi = gsi.phi ();
2426 size_t n, i;
2428 n = gimple_phi_num_args (phi);
2430 /* Don't look at virtual operands. */
2431 if (virtual_operand_p (gimple_phi_result (phi)))
2432 continue;
2434 for (i = 0; i < n; ++i)
2436 tree op = gimple_phi_arg_def (phi, i);
2437 if (TREE_CODE (op) == SSA_NAME
2438 && uninit_undefined_value_p (op))
2440 worklist.safe_push (phi);
2441 added_to_worklist.add (phi);
2442 if (dump_file && (dump_flags & TDF_DETAILS))
2444 fprintf (dump_file, "[WORKLIST]: add to initial list: ");
2445 print_gimple_stmt (dump_file, phi, 0, 0);
2447 break;
2452 while (worklist.length () != 0)
2454 gphi *cur_phi = 0;
2455 cur_phi = worklist.pop ();
2456 warn_uninitialized_phi (cur_phi, &worklist, &added_to_worklist);
2459 worklist.release ();
2460 delete possibly_undefined_names;
2461 possibly_undefined_names = NULL;
2462 free_dominance_info (CDI_POST_DOMINATORS);
2463 timevar_pop (TV_TREE_UNINIT);
2464 return 0;
2467 } // anon namespace
2469 gimple_opt_pass *
2470 make_pass_late_warn_uninitialized (gcc::context *ctxt)
2472 return new pass_late_warn_uninitialized (ctxt);
2476 static unsigned int
2477 execute_early_warn_uninitialized (void)
2479 /* Currently, this pass runs always but
2480 execute_late_warn_uninitialized only runs with optimization. With
2481 optimization we want to warn about possible uninitialized as late
2482 as possible, thus don't do it here. However, without
2483 optimization we need to warn here about "may be uninitialized". */
2484 calculate_dominance_info (CDI_POST_DOMINATORS);
2486 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/!optimize);
2488 /* Post-dominator information can not be reliably updated. Free it
2489 after the use. */
2491 free_dominance_info (CDI_POST_DOMINATORS);
2492 return 0;
2496 namespace {
2498 const pass_data pass_data_early_warn_uninitialized =
2500 GIMPLE_PASS, /* type */
2501 "*early_warn_uninitialized", /* name */
2502 OPTGROUP_NONE, /* optinfo_flags */
2503 TV_TREE_UNINIT, /* tv_id */
2504 PROP_ssa, /* properties_required */
2505 0, /* properties_provided */
2506 0, /* properties_destroyed */
2507 0, /* todo_flags_start */
2508 0, /* todo_flags_finish */
2511 class pass_early_warn_uninitialized : public gimple_opt_pass
2513 public:
2514 pass_early_warn_uninitialized (gcc::context *ctxt)
2515 : gimple_opt_pass (pass_data_early_warn_uninitialized, ctxt)
2518 /* opt_pass methods: */
2519 virtual bool gate (function *) { return gate_warn_uninitialized (); }
2520 virtual unsigned int execute (function *)
2522 return execute_early_warn_uninitialized ();
2525 }; // class pass_early_warn_uninitialized
2527 } // anon namespace
2529 gimple_opt_pass *
2530 make_pass_early_warn_uninitialized (gcc::context *ctxt)
2532 return new pass_early_warn_uninitialized (ctxt);