* lib/scanasm.exp (hidden-scan-for): Add XCOFF support.
[official-gcc.git] / gcc / tree-ssa-uninit.c
blob13448548e0e00d5b61cdc1ebdcd75cb168a3f58b
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 /* Anonymous SSA_NAMEs shouldn't be uninitialized, but ssa_undefined_value_p
135 can return true if the def stmt of anonymous SSA_NAME is COMPLEX_EXPR
136 created for conversion from scalar to complex. Use the underlying var of
137 the COMPLEX_EXPRs real part in that case. See PR71581. */
138 if (expr == NULL_TREE
139 && var == NULL_TREE
140 && SSA_NAME_VAR (t) == NULL_TREE
141 && is_gimple_assign (SSA_NAME_DEF_STMT (t))
142 && gimple_assign_rhs_code (SSA_NAME_DEF_STMT (t)) == COMPLEX_EXPR)
144 tree v = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (t));
145 if (TREE_CODE (v) == SSA_NAME
146 && has_undefined_value_p (v)
147 && zerop (gimple_assign_rhs2 (SSA_NAME_DEF_STMT (t))))
149 expr = SSA_NAME_VAR (v);
150 var = expr;
154 if (expr == NULL_TREE)
155 return;
157 /* TREE_NO_WARNING either means we already warned, or the front end
158 wishes to suppress the warning. */
159 if ((context
160 && (gimple_no_warning_p (context)
161 || (gimple_assign_single_p (context)
162 && TREE_NO_WARNING (gimple_assign_rhs1 (context)))))
163 || TREE_NO_WARNING (expr))
164 return;
166 if (context != NULL && gimple_has_location (context))
167 location = gimple_location (context);
168 else if (phiarg_loc != UNKNOWN_LOCATION)
169 location = phiarg_loc;
170 else
171 location = DECL_SOURCE_LOCATION (var);
172 location = linemap_resolve_location (line_table, location,
173 LRK_SPELLING_LOCATION, 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, location, cfun_loc)
185 || linemap_location_before_p (line_table, cfun->function_end_locus,
186 location))
187 inform (DECL_SOURCE_LOCATION (var), "%qD was declared here", var);
191 static unsigned int
192 warn_uninitialized_vars (bool warn_possibly_uninitialized)
194 gimple_stmt_iterator gsi;
195 basic_block bb;
197 FOR_EACH_BB_FN (bb, cfun)
199 basic_block succ = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
200 bool always_executed = dominated_by_p (CDI_POST_DOMINATORS, succ, bb);
201 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
203 gimple *stmt = gsi_stmt (gsi);
204 use_operand_p use_p;
205 ssa_op_iter op_iter;
206 tree use;
208 if (is_gimple_debug (stmt))
209 continue;
211 /* We only do data flow with SSA_NAMEs, so that's all we
212 can warn about. */
213 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, op_iter, SSA_OP_USE)
215 use = USE_FROM_PTR (use_p);
216 if (always_executed)
217 warn_uninit (OPT_Wuninitialized, use, SSA_NAME_VAR (use),
218 SSA_NAME_VAR (use),
219 "%qD is used uninitialized in this function", stmt,
220 UNKNOWN_LOCATION);
221 else if (warn_possibly_uninitialized)
222 warn_uninit (OPT_Wmaybe_uninitialized, use, SSA_NAME_VAR (use),
223 SSA_NAME_VAR (use),
224 "%qD may be used uninitialized in this function",
225 stmt, UNKNOWN_LOCATION);
228 /* For memory the only cheap thing we can do is see if we
229 have a use of the default def of the virtual operand.
230 ??? Not so cheap would be to use the alias oracle via
231 walk_aliased_vdefs, if we don't find any aliasing vdef
232 warn as is-used-uninitialized, if we don't find an aliasing
233 vdef that kills our use (stmt_kills_ref_p), warn as
234 may-be-used-uninitialized. But this walk is quadratic and
235 so must be limited which means we would miss warning
236 opportunities. */
237 use = gimple_vuse (stmt);
238 if (use
239 && gimple_assign_single_p (stmt)
240 && !gimple_vdef (stmt)
241 && SSA_NAME_IS_DEFAULT_DEF (use))
243 tree rhs = gimple_assign_rhs1 (stmt);
244 tree base = get_base_address (rhs);
246 /* Do not warn if it can be initialized outside this function. */
247 if (!VAR_P (base)
248 || DECL_HARD_REGISTER (base)
249 || is_global_var (base))
250 continue;
252 if (always_executed)
253 warn_uninit (OPT_Wuninitialized, use, gimple_assign_rhs1 (stmt),
254 base, "%qE is used uninitialized in this function",
255 stmt, UNKNOWN_LOCATION);
256 else if (warn_possibly_uninitialized)
257 warn_uninit (OPT_Wmaybe_uninitialized, use,
258 gimple_assign_rhs1 (stmt), base,
259 "%qE may be used uninitialized in this function",
260 stmt, UNKNOWN_LOCATION);
265 return 0;
268 /* Checks if the operand OPND of PHI is defined by
269 another phi with one operand defined by this PHI,
270 but the rest operands are all defined. If yes,
271 returns true to skip this operand as being
272 redundant. Can be enhanced to be more general. */
274 static bool
275 can_skip_redundant_opnd (tree opnd, gimple *phi)
277 gimple *op_def;
278 tree phi_def;
279 int i, n;
281 phi_def = gimple_phi_result (phi);
282 op_def = SSA_NAME_DEF_STMT (opnd);
283 if (gimple_code (op_def) != GIMPLE_PHI)
284 return false;
285 n = gimple_phi_num_args (op_def);
286 for (i = 0; i < n; ++i)
288 tree op = gimple_phi_arg_def (op_def, i);
289 if (TREE_CODE (op) != SSA_NAME)
290 continue;
291 if (op != phi_def && uninit_undefined_value_p (op))
292 return false;
295 return true;
298 /* Returns a bit mask holding the positions of arguments in PHI
299 that have empty (or possibly empty) definitions. */
301 static unsigned
302 compute_uninit_opnds_pos (gphi *phi)
304 size_t i, n;
305 unsigned uninit_opnds = 0;
307 n = gimple_phi_num_args (phi);
308 /* Bail out for phi with too many args. */
309 if (n > 32)
310 return 0;
312 for (i = 0; i < n; ++i)
314 tree op = gimple_phi_arg_def (phi, i);
315 if (TREE_CODE (op) == SSA_NAME
316 && uninit_undefined_value_p (op)
317 && !can_skip_redundant_opnd (op, phi))
319 if (cfun->has_nonlocal_label || cfun->calls_setjmp)
321 /* Ignore SSA_NAMEs that appear on abnormal edges
322 somewhere. */
323 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
324 continue;
326 MASK_SET_BIT (uninit_opnds, i);
329 return uninit_opnds;
332 /* Find the immediate postdominator PDOM of the specified
333 basic block BLOCK. */
335 static inline basic_block
336 find_pdom (basic_block block)
338 if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
339 return EXIT_BLOCK_PTR_FOR_FN (cfun);
340 else
342 basic_block bb = get_immediate_dominator (CDI_POST_DOMINATORS, block);
343 if (!bb)
344 return EXIT_BLOCK_PTR_FOR_FN (cfun);
345 return bb;
349 /* Find the immediate DOM of the specified basic block BLOCK. */
351 static inline basic_block
352 find_dom (basic_block block)
354 if (block == ENTRY_BLOCK_PTR_FOR_FN (cfun))
355 return ENTRY_BLOCK_PTR_FOR_FN (cfun);
356 else
358 basic_block bb = get_immediate_dominator (CDI_DOMINATORS, block);
359 if (!bb)
360 return ENTRY_BLOCK_PTR_FOR_FN (cfun);
361 return bb;
365 /* Returns true if BB1 is postdominating BB2 and BB1 is
366 not a loop exit bb. The loop exit bb check is simple and does
367 not cover all cases. */
369 static bool
370 is_non_loop_exit_postdominating (basic_block bb1, basic_block bb2)
372 if (!dominated_by_p (CDI_POST_DOMINATORS, bb2, bb1))
373 return false;
375 if (single_pred_p (bb1) && !single_succ_p (bb2))
376 return false;
378 return true;
381 /* Find the closest postdominator of a specified BB, which is control
382 equivalent to BB. */
384 static inline basic_block
385 find_control_equiv_block (basic_block bb)
387 basic_block pdom;
389 pdom = find_pdom (bb);
391 /* Skip the postdominating bb that is also loop exit. */
392 if (!is_non_loop_exit_postdominating (pdom, bb))
393 return NULL;
395 if (dominated_by_p (CDI_DOMINATORS, pdom, bb))
396 return pdom;
398 return NULL;
401 #define MAX_NUM_CHAINS 8
402 #define MAX_CHAIN_LEN 5
403 #define MAX_POSTDOM_CHECK 8
404 #define MAX_SWITCH_CASES 40
406 /* Computes the control dependence chains (paths of edges)
407 for DEP_BB up to the dominating basic block BB (the head node of a
408 chain should be dominated by it). CD_CHAINS is pointer to an
409 array holding the result chains. CUR_CD_CHAIN is the current
410 chain being computed. *NUM_CHAINS is total number of chains. The
411 function returns true if the information is successfully computed,
412 return false if there is no control dependence or not computed. */
414 static bool
415 compute_control_dep_chain (basic_block bb, basic_block dep_bb,
416 vec<edge> *cd_chains,
417 size_t *num_chains,
418 vec<edge> *cur_cd_chain,
419 int *num_calls)
421 edge_iterator ei;
422 edge e;
423 size_t i;
424 bool found_cd_chain = false;
425 size_t cur_chain_len = 0;
427 if (EDGE_COUNT (bb->succs) < 2)
428 return false;
430 if (*num_calls > PARAM_VALUE (PARAM_UNINIT_CONTROL_DEP_ATTEMPTS))
431 return false;
432 ++*num_calls;
434 /* Could use a set instead. */
435 cur_chain_len = cur_cd_chain->length ();
436 if (cur_chain_len > MAX_CHAIN_LEN)
437 return false;
439 for (i = 0; i < cur_chain_len; i++)
441 edge e = (*cur_cd_chain)[i];
442 /* Cycle detected. */
443 if (e->src == bb)
444 return false;
447 FOR_EACH_EDGE (e, ei, bb->succs)
449 basic_block cd_bb;
450 int post_dom_check = 0;
451 if (e->flags & (EDGE_FAKE | EDGE_ABNORMAL))
452 continue;
454 cd_bb = e->dest;
455 cur_cd_chain->safe_push (e);
456 while (!is_non_loop_exit_postdominating (cd_bb, bb))
458 if (cd_bb == dep_bb)
460 /* Found a direct control dependence. */
461 if (*num_chains < MAX_NUM_CHAINS)
463 cd_chains[*num_chains] = cur_cd_chain->copy ();
464 (*num_chains)++;
466 found_cd_chain = true;
467 /* Check path from next edge. */
468 break;
471 /* Now check if DEP_BB is indirectly control dependent on BB. */
472 if (compute_control_dep_chain (cd_bb, dep_bb, cd_chains, num_chains,
473 cur_cd_chain, num_calls))
475 found_cd_chain = true;
476 break;
479 cd_bb = find_pdom (cd_bb);
480 post_dom_check++;
481 if (cd_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
482 || post_dom_check > MAX_POSTDOM_CHECK)
483 break;
485 cur_cd_chain->pop ();
486 gcc_assert (cur_cd_chain->length () == cur_chain_len);
488 gcc_assert (cur_cd_chain->length () == cur_chain_len);
490 return found_cd_chain;
493 /* The type to represent a simple predicate. */
495 struct pred_info
497 tree pred_lhs;
498 tree pred_rhs;
499 enum tree_code cond_code;
500 bool invert;
503 /* The type to represent a sequence of predicates grouped
504 with .AND. operation. */
506 typedef vec<pred_info, va_heap, vl_ptr> pred_chain;
508 /* The type to represent a sequence of pred_chains grouped
509 with .OR. operation. */
511 typedef vec<pred_chain, va_heap, vl_ptr> pred_chain_union;
513 /* Converts the chains of control dependence edges into a set of
514 predicates. A control dependence chain is represented by a vector
515 edges. DEP_CHAINS points to an array of dependence chains.
516 NUM_CHAINS is the size of the chain array. One edge in a dependence
517 chain is mapped to predicate expression represented by pred_info
518 type. One dependence chain is converted to a composite predicate that
519 is the result of AND operation of pred_info mapped to each edge.
520 A composite predicate is presented by a vector of pred_info. On
521 return, *PREDS points to the resulting array of composite predicates.
522 *NUM_PREDS is the number of composite predictes. */
524 static bool
525 convert_control_dep_chain_into_preds (vec<edge> *dep_chains,
526 size_t num_chains,
527 pred_chain_union *preds)
529 bool has_valid_pred = false;
530 size_t i, j;
531 if (num_chains == 0 || num_chains >= MAX_NUM_CHAINS)
532 return false;
534 /* Now convert the control dep chain into a set
535 of predicates. */
536 preds->reserve (num_chains);
538 for (i = 0; i < num_chains; i++)
540 vec<edge> one_cd_chain = dep_chains[i];
542 has_valid_pred = false;
543 pred_chain t_chain = vNULL;
544 for (j = 0; j < one_cd_chain.length (); j++)
546 gimple *cond_stmt;
547 gimple_stmt_iterator gsi;
548 basic_block guard_bb;
549 pred_info one_pred;
550 edge e;
552 e = one_cd_chain[j];
553 guard_bb = e->src;
554 gsi = gsi_last_bb (guard_bb);
555 if (gsi_end_p (gsi))
557 has_valid_pred = false;
558 break;
560 cond_stmt = gsi_stmt (gsi);
561 if (is_gimple_call (cond_stmt) && EDGE_COUNT (e->src->succs) >= 2)
562 /* Ignore EH edge. Can add assertion on the other edge's flag. */
563 continue;
564 /* Skip if there is essentially one succesor. */
565 if (EDGE_COUNT (e->src->succs) == 2)
567 edge e1;
568 edge_iterator ei1;
569 bool skip = false;
571 FOR_EACH_EDGE (e1, ei1, e->src->succs)
573 if (EDGE_COUNT (e1->dest->succs) == 0)
575 skip = true;
576 break;
579 if (skip)
580 continue;
582 if (gimple_code (cond_stmt) == GIMPLE_COND)
584 one_pred.pred_lhs = gimple_cond_lhs (cond_stmt);
585 one_pred.pred_rhs = gimple_cond_rhs (cond_stmt);
586 one_pred.cond_code = gimple_cond_code (cond_stmt);
587 one_pred.invert = !!(e->flags & EDGE_FALSE_VALUE);
588 t_chain.safe_push (one_pred);
589 has_valid_pred = true;
591 else if (gswitch *gs = dyn_cast<gswitch *> (cond_stmt))
593 /* Avoid quadratic behavior. */
594 if (gimple_switch_num_labels (gs) > MAX_SWITCH_CASES)
596 has_valid_pred = false;
597 break;
599 /* Find the case label. */
600 tree l = NULL_TREE;
601 unsigned idx;
602 for (idx = 0; idx < gimple_switch_num_labels (gs); ++idx)
604 tree tl = gimple_switch_label (gs, idx);
605 if (e->dest == label_to_block (CASE_LABEL (tl)))
607 if (!l)
608 l = tl;
609 else
611 l = NULL_TREE;
612 break;
616 /* If more than one label reaches this block or the case
617 label doesn't have a single value (like the default one)
618 fail. */
619 if (!l
620 || !CASE_LOW (l)
621 || (CASE_HIGH (l)
622 && !operand_equal_p (CASE_LOW (l), CASE_HIGH (l), 0)))
624 has_valid_pred = false;
625 break;
627 one_pred.pred_lhs = gimple_switch_index (gs);
628 one_pred.pred_rhs = CASE_LOW (l);
629 one_pred.cond_code = EQ_EXPR;
630 one_pred.invert = false;
631 t_chain.safe_push (one_pred);
632 has_valid_pred = true;
634 else
636 has_valid_pred = false;
637 break;
641 if (!has_valid_pred)
642 break;
643 else
644 preds->safe_push (t_chain);
646 return has_valid_pred;
649 /* Computes all control dependence chains for USE_BB. The control
650 dependence chains are then converted to an array of composite
651 predicates pointed to by PREDS. PHI_BB is the basic block of
652 the phi whose result is used in USE_BB. */
654 static bool
655 find_predicates (pred_chain_union *preds,
656 basic_block phi_bb,
657 basic_block use_bb)
659 size_t num_chains = 0, i;
660 int num_calls = 0;
661 vec<edge> dep_chains[MAX_NUM_CHAINS];
662 auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
663 bool has_valid_pred = false;
664 basic_block cd_root = 0;
666 /* First find the closest bb that is control equivalent to PHI_BB
667 that also dominates USE_BB. */
668 cd_root = phi_bb;
669 while (dominated_by_p (CDI_DOMINATORS, use_bb, cd_root))
671 basic_block ctrl_eq_bb = find_control_equiv_block (cd_root);
672 if (ctrl_eq_bb && dominated_by_p (CDI_DOMINATORS, use_bb, ctrl_eq_bb))
673 cd_root = ctrl_eq_bb;
674 else
675 break;
678 compute_control_dep_chain (cd_root, use_bb, dep_chains, &num_chains,
679 &cur_chain, &num_calls);
681 has_valid_pred
682 = convert_control_dep_chain_into_preds (dep_chains, num_chains, preds);
683 for (i = 0; i < num_chains; i++)
684 dep_chains[i].release ();
685 return has_valid_pred;
688 /* Computes the set of incoming edges of PHI that have non empty
689 definitions of a phi chain. The collection will be done
690 recursively on operands that are defined by phis. CD_ROOT
691 is the control dependence root. *EDGES holds the result, and
692 VISITED_PHIS is a pointer set for detecting cycles. */
694 static void
695 collect_phi_def_edges (gphi *phi, basic_block cd_root,
696 auto_vec<edge> *edges,
697 hash_set<gimple *> *visited_phis)
699 size_t i, n;
700 edge opnd_edge;
701 tree opnd;
703 if (visited_phis->add (phi))
704 return;
706 n = gimple_phi_num_args (phi);
707 for (i = 0; i < n; i++)
709 opnd_edge = gimple_phi_arg_edge (phi, i);
710 opnd = gimple_phi_arg_def (phi, i);
712 if (TREE_CODE (opnd) != SSA_NAME)
714 if (dump_file && (dump_flags & TDF_DETAILS))
716 fprintf (dump_file, "\n[CHECK] Found def edge %d in ", (int) i);
717 print_gimple_stmt (dump_file, phi, 0, 0);
719 edges->safe_push (opnd_edge);
721 else
723 gimple *def = SSA_NAME_DEF_STMT (opnd);
725 if (gimple_code (def) == GIMPLE_PHI
726 && dominated_by_p (CDI_DOMINATORS, gimple_bb (def), cd_root))
727 collect_phi_def_edges (as_a<gphi *> (def), cd_root, edges,
728 visited_phis);
729 else if (!uninit_undefined_value_p (opnd))
731 if (dump_file && (dump_flags & TDF_DETAILS))
733 fprintf (dump_file, "\n[CHECK] Found def edge %d in ",
734 (int) i);
735 print_gimple_stmt (dump_file, phi, 0, 0);
737 edges->safe_push (opnd_edge);
743 /* For each use edge of PHI, computes all control dependence chains.
744 The control dependence chains are then converted to an array of
745 composite predicates pointed to by PREDS. */
747 static bool
748 find_def_preds (pred_chain_union *preds, gphi *phi)
750 size_t num_chains = 0, i, n;
751 vec<edge> dep_chains[MAX_NUM_CHAINS];
752 auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
753 auto_vec<edge> def_edges;
754 bool has_valid_pred = false;
755 basic_block phi_bb, cd_root = 0;
757 phi_bb = gimple_bb (phi);
758 /* First find the closest dominating bb to be
759 the control dependence root. */
760 cd_root = find_dom (phi_bb);
761 if (!cd_root)
762 return false;
764 hash_set<gimple *> visited_phis;
765 collect_phi_def_edges (phi, cd_root, &def_edges, &visited_phis);
767 n = def_edges.length ();
768 if (n == 0)
769 return false;
771 for (i = 0; i < n; i++)
773 size_t prev_nc, j;
774 int num_calls = 0;
775 edge opnd_edge;
777 opnd_edge = def_edges[i];
778 prev_nc = num_chains;
779 compute_control_dep_chain (cd_root, opnd_edge->src, dep_chains,
780 &num_chains, &cur_chain, &num_calls);
782 /* Now update the newly added chains with
783 the phi operand edge: */
784 if (EDGE_COUNT (opnd_edge->src->succs) > 1)
786 if (prev_nc == num_chains && num_chains < MAX_NUM_CHAINS)
787 dep_chains[num_chains++] = vNULL;
788 for (j = prev_nc; j < num_chains; j++)
789 dep_chains[j].safe_push (opnd_edge);
793 has_valid_pred
794 = convert_control_dep_chain_into_preds (dep_chains, num_chains, preds);
795 for (i = 0; i < num_chains; i++)
796 dep_chains[i].release ();
797 return has_valid_pred;
800 /* Dumps the predicates (PREDS) for USESTMT. */
802 static void
803 dump_predicates (gimple *usestmt, pred_chain_union preds, const char *msg)
805 size_t i, j;
806 pred_chain one_pred_chain = vNULL;
807 fprintf (dump_file, "%s", msg);
808 print_gimple_stmt (dump_file, usestmt, 0, 0);
809 fprintf (dump_file, "is guarded by :\n\n");
810 size_t num_preds = preds.length ();
811 /* Do some dumping here: */
812 for (i = 0; i < num_preds; i++)
814 size_t np;
816 one_pred_chain = preds[i];
817 np = one_pred_chain.length ();
819 for (j = 0; j < np; j++)
821 pred_info one_pred = one_pred_chain[j];
822 if (one_pred.invert)
823 fprintf (dump_file, " (.NOT.) ");
824 print_generic_expr (dump_file, one_pred.pred_lhs, 0);
825 fprintf (dump_file, " %s ", op_symbol_code (one_pred.cond_code));
826 print_generic_expr (dump_file, one_pred.pred_rhs, 0);
827 if (j < np - 1)
828 fprintf (dump_file, " (.AND.) ");
829 else
830 fprintf (dump_file, "\n");
832 if (i < num_preds - 1)
833 fprintf (dump_file, "(.OR.)\n");
834 else
835 fprintf (dump_file, "\n\n");
839 /* Destroys the predicate set *PREDS. */
841 static void
842 destroy_predicate_vecs (pred_chain_union *preds)
844 size_t i;
846 size_t n = preds->length ();
847 for (i = 0; i < n; i++)
848 (*preds)[i].release ();
849 preds->release ();
852 /* Computes the 'normalized' conditional code with operand
853 swapping and condition inversion. */
855 static enum tree_code
856 get_cmp_code (enum tree_code orig_cmp_code, bool swap_cond, bool invert)
858 enum tree_code tc = orig_cmp_code;
860 if (swap_cond)
861 tc = swap_tree_comparison (orig_cmp_code);
862 if (invert)
863 tc = invert_tree_comparison (tc, false);
865 switch (tc)
867 case LT_EXPR:
868 case LE_EXPR:
869 case GT_EXPR:
870 case GE_EXPR:
871 case EQ_EXPR:
872 case NE_EXPR:
873 break;
874 default:
875 return ERROR_MARK;
877 return tc;
880 /* Returns true if VAL falls in the range defined by BOUNDARY and CMPC, i.e.
881 all values in the range satisfies (x CMPC BOUNDARY) == true. */
883 static bool
884 is_value_included_in (tree val, tree boundary, enum tree_code cmpc)
886 bool inverted = false;
887 bool is_unsigned;
888 bool result;
890 /* Only handle integer constant here. */
891 if (TREE_CODE (val) != INTEGER_CST || TREE_CODE (boundary) != INTEGER_CST)
892 return true;
894 is_unsigned = TYPE_UNSIGNED (TREE_TYPE (val));
896 if (cmpc == GE_EXPR || cmpc == GT_EXPR || cmpc == NE_EXPR)
898 cmpc = invert_tree_comparison (cmpc, false);
899 inverted = true;
902 if (is_unsigned)
904 if (cmpc == EQ_EXPR)
905 result = tree_int_cst_equal (val, boundary);
906 else if (cmpc == LT_EXPR)
907 result = tree_int_cst_lt (val, boundary);
908 else
910 gcc_assert (cmpc == LE_EXPR);
911 result = tree_int_cst_le (val, boundary);
914 else
916 if (cmpc == EQ_EXPR)
917 result = tree_int_cst_equal (val, boundary);
918 else if (cmpc == LT_EXPR)
919 result = tree_int_cst_lt (val, boundary);
920 else
922 gcc_assert (cmpc == LE_EXPR);
923 result = (tree_int_cst_equal (val, boundary)
924 || tree_int_cst_lt (val, boundary));
928 if (inverted)
929 result ^= 1;
931 return result;
934 /* Returns true if PRED is common among all the predicate
935 chains (PREDS) (and therefore can be factored out).
936 NUM_PRED_CHAIN is the size of array PREDS. */
938 static bool
939 find_matching_predicate_in_rest_chains (pred_info pred,
940 pred_chain_union preds,
941 size_t num_pred_chains)
943 size_t i, j, n;
945 /* Trival case. */
946 if (num_pred_chains == 1)
947 return true;
949 for (i = 1; i < num_pred_chains; i++)
951 bool found = false;
952 pred_chain one_chain = preds[i];
953 n = one_chain.length ();
954 for (j = 0; j < n; j++)
956 pred_info pred2 = one_chain[j];
957 /* Can relax the condition comparison to not
958 use address comparison. However, the most common
959 case is that multiple control dependent paths share
960 a common path prefix, so address comparison should
961 be ok. */
963 if (operand_equal_p (pred2.pred_lhs, pred.pred_lhs, 0)
964 && operand_equal_p (pred2.pred_rhs, pred.pred_rhs, 0)
965 && pred2.invert == pred.invert)
967 found = true;
968 break;
971 if (!found)
972 return false;
974 return true;
977 /* Forward declaration. */
978 static bool is_use_properly_guarded (gimple *use_stmt,
979 basic_block use_bb,
980 gphi *phi,
981 unsigned uninit_opnds,
982 pred_chain_union *def_preds,
983 hash_set<gphi *> *visited_phis);
985 /* Returns true if all uninitialized opnds are pruned. Returns false
986 otherwise. PHI is the phi node with uninitialized operands,
987 UNINIT_OPNDS is the bitmap of the uninitialize operand positions,
988 FLAG_DEF is the statement defining the flag guarding the use of the
989 PHI output, BOUNDARY_CST is the const value used in the predicate
990 associated with the flag, CMP_CODE is the comparison code used in
991 the predicate, VISITED_PHIS is the pointer set of phis visited, and
992 VISITED_FLAG_PHIS is the pointer to the pointer set of flag definitions
993 that are also phis.
995 Example scenario:
997 BB1:
998 flag_1 = phi <0, 1> // (1)
999 var_1 = phi <undef, some_val>
1002 BB2:
1003 flag_2 = phi <0, flag_1, flag_1> // (2)
1004 var_2 = phi <undef, var_1, var_1>
1005 if (flag_2 == 1)
1006 goto BB3;
1008 BB3:
1009 use of var_2 // (3)
1011 Because some flag arg in (1) is not constant, if we do not look into the
1012 flag phis recursively, it is conservatively treated as unknown and var_1
1013 is thought to be flowed into use at (3). Since var_1 is potentially
1014 uninitialized a false warning will be emitted.
1015 Checking recursively into (1), the compiler can find out that only some_val
1016 (which is defined) can flow into (3) which is OK. */
1018 static bool
1019 prune_uninit_phi_opnds (gphi *phi, unsigned uninit_opnds, gphi *flag_def,
1020 tree boundary_cst, enum tree_code cmp_code,
1021 hash_set<gphi *> *visited_phis,
1022 bitmap *visited_flag_phis)
1024 unsigned i;
1026 for (i = 0; i < MIN (32, gimple_phi_num_args (flag_def)); i++)
1028 tree flag_arg;
1030 if (!MASK_TEST_BIT (uninit_opnds, i))
1031 continue;
1033 flag_arg = gimple_phi_arg_def (flag_def, i);
1034 if (!is_gimple_constant (flag_arg))
1036 gphi *flag_arg_def, *phi_arg_def;
1037 tree phi_arg;
1038 unsigned uninit_opnds_arg_phi;
1040 if (TREE_CODE (flag_arg) != SSA_NAME)
1041 return false;
1042 flag_arg_def = dyn_cast<gphi *> (SSA_NAME_DEF_STMT (flag_arg));
1043 if (!flag_arg_def)
1044 return false;
1046 phi_arg = gimple_phi_arg_def (phi, i);
1047 if (TREE_CODE (phi_arg) != SSA_NAME)
1048 return false;
1050 phi_arg_def = dyn_cast<gphi *> (SSA_NAME_DEF_STMT (phi_arg));
1051 if (!phi_arg_def)
1052 return false;
1054 if (gimple_bb (phi_arg_def) != gimple_bb (flag_arg_def))
1055 return false;
1057 if (!*visited_flag_phis)
1058 *visited_flag_phis = BITMAP_ALLOC (NULL);
1060 tree phi_result = gimple_phi_result (flag_arg_def);
1061 if (bitmap_bit_p (*visited_flag_phis, SSA_NAME_VERSION (phi_result)))
1062 return false;
1064 bitmap_set_bit (*visited_flag_phis,
1065 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def)));
1067 /* Now recursively prune the uninitialized phi args. */
1068 uninit_opnds_arg_phi = compute_uninit_opnds_pos (phi_arg_def);
1069 if (!prune_uninit_phi_opnds
1070 (phi_arg_def, uninit_opnds_arg_phi, flag_arg_def, boundary_cst,
1071 cmp_code, visited_phis, visited_flag_phis))
1072 return false;
1074 phi_result = gimple_phi_result (flag_arg_def);
1075 bitmap_clear_bit (*visited_flag_phis, SSA_NAME_VERSION (phi_result));
1076 continue;
1079 /* Now check if the constant is in the guarded range. */
1080 if (is_value_included_in (flag_arg, boundary_cst, cmp_code))
1082 tree opnd;
1083 gimple *opnd_def;
1085 /* Now that we know that this undefined edge is not
1086 pruned. If the operand is defined by another phi,
1087 we can further prune the incoming edges of that
1088 phi by checking the predicates of this operands. */
1090 opnd = gimple_phi_arg_def (phi, i);
1091 opnd_def = SSA_NAME_DEF_STMT (opnd);
1092 if (gphi *opnd_def_phi = dyn_cast <gphi *> (opnd_def))
1094 edge opnd_edge;
1095 unsigned uninit_opnds2 = compute_uninit_opnds_pos (opnd_def_phi);
1096 if (!MASK_EMPTY (uninit_opnds2))
1098 pred_chain_union def_preds = vNULL;
1099 bool ok;
1100 opnd_edge = gimple_phi_arg_edge (phi, i);
1101 ok = is_use_properly_guarded (phi,
1102 opnd_edge->src,
1103 opnd_def_phi,
1104 uninit_opnds2,
1105 &def_preds,
1106 visited_phis);
1107 destroy_predicate_vecs (&def_preds);
1108 if (!ok)
1109 return false;
1112 else
1113 return false;
1117 return true;
1120 /* A helper function that determines if the predicate set
1121 of the use is not overlapping with that of the uninit paths.
1122 The most common senario of guarded use is in Example 1:
1123 Example 1:
1124 if (some_cond)
1126 x = ...;
1127 flag = true;
1130 ... some code ...
1132 if (flag)
1133 use (x);
1135 The real world examples are usually more complicated, but similar
1136 and usually result from inlining:
1138 bool init_func (int * x)
1140 if (some_cond)
1141 return false;
1142 *x = ..
1143 return true;
1146 void foo (..)
1148 int x;
1150 if (!init_func (&x))
1151 return;
1153 .. some_code ...
1154 use (x);
1157 Another possible use scenario is in the following trivial example:
1159 Example 2:
1160 if (n > 0)
1161 x = 1;
1163 if (n > 0)
1165 if (m < 2)
1166 .. = x;
1169 Predicate analysis needs to compute the composite predicate:
1171 1) 'x' use predicate: (n > 0) .AND. (m < 2)
1172 2) 'x' default value (non-def) predicate: .NOT. (n > 0)
1173 (the predicate chain for phi operand defs can be computed
1174 starting from a bb that is control equivalent to the phi's
1175 bb and is dominating the operand def.)
1177 and check overlapping:
1178 (n > 0) .AND. (m < 2) .AND. (.NOT. (n > 0))
1179 <==> false
1181 This implementation provides framework that can handle
1182 scenarios. (Note that many simple cases are handled properly
1183 without the predicate analysis -- this is due to jump threading
1184 transformation which eliminates the merge point thus makes
1185 path sensitive analysis unnecessary.)
1187 NUM_PREDS is the number is the number predicate chains, PREDS is
1188 the array of chains, PHI is the phi node whose incoming (undefined)
1189 paths need to be pruned, and UNINIT_OPNDS is the bitmap holding
1190 uninit operand positions. VISITED_PHIS is the pointer set of phi
1191 stmts being checked. */
1193 static bool
1194 use_pred_not_overlap_with_undef_path_pred (pred_chain_union preds,
1195 gphi *phi, unsigned uninit_opnds,
1196 hash_set<gphi *> *visited_phis)
1198 unsigned int i, n;
1199 gimple *flag_def = 0;
1200 tree boundary_cst = 0;
1201 enum tree_code cmp_code;
1202 bool swap_cond = false;
1203 bool invert = false;
1204 pred_chain the_pred_chain = vNULL;
1205 bitmap visited_flag_phis = NULL;
1206 bool all_pruned = false;
1207 size_t num_preds = preds.length ();
1209 gcc_assert (num_preds > 0);
1210 /* Find within the common prefix of multiple predicate chains
1211 a predicate that is a comparison of a flag variable against
1212 a constant. */
1213 the_pred_chain = preds[0];
1214 n = the_pred_chain.length ();
1215 for (i = 0; i < n; i++)
1217 tree cond_lhs, cond_rhs, flag = 0;
1219 pred_info the_pred = the_pred_chain[i];
1221 invert = the_pred.invert;
1222 cond_lhs = the_pred.pred_lhs;
1223 cond_rhs = the_pred.pred_rhs;
1224 cmp_code = the_pred.cond_code;
1226 if (cond_lhs != NULL_TREE && TREE_CODE (cond_lhs) == SSA_NAME
1227 && cond_rhs != NULL_TREE && is_gimple_constant (cond_rhs))
1229 boundary_cst = cond_rhs;
1230 flag = cond_lhs;
1232 else if (cond_rhs != NULL_TREE && TREE_CODE (cond_rhs) == SSA_NAME
1233 && cond_lhs != NULL_TREE && is_gimple_constant (cond_lhs))
1235 boundary_cst = cond_lhs;
1236 flag = cond_rhs;
1237 swap_cond = true;
1240 if (!flag)
1241 continue;
1243 flag_def = SSA_NAME_DEF_STMT (flag);
1245 if (!flag_def)
1246 continue;
1248 if ((gimple_code (flag_def) == GIMPLE_PHI)
1249 && (gimple_bb (flag_def) == gimple_bb (phi))
1250 && find_matching_predicate_in_rest_chains (the_pred, preds,
1251 num_preds))
1252 break;
1254 flag_def = 0;
1257 if (!flag_def)
1258 return false;
1260 /* Now check all the uninit incoming edge has a constant flag value
1261 that is in conflict with the use guard/predicate. */
1262 cmp_code = get_cmp_code (cmp_code, swap_cond, invert);
1264 if (cmp_code == ERROR_MARK)
1265 return false;
1267 all_pruned = prune_uninit_phi_opnds
1268 (phi, uninit_opnds, as_a<gphi *> (flag_def), boundary_cst, cmp_code,
1269 visited_phis, &visited_flag_phis);
1271 if (visited_flag_phis)
1272 BITMAP_FREE (visited_flag_phis);
1274 return all_pruned;
1277 /* The helper function returns true if two predicates X1 and X2
1278 are equivalent. It assumes the expressions have already
1279 properly re-associated. */
1281 static inline bool
1282 pred_equal_p (pred_info x1, pred_info x2)
1284 enum tree_code c1, c2;
1285 if (!operand_equal_p (x1.pred_lhs, x2.pred_lhs, 0)
1286 || !operand_equal_p (x1.pred_rhs, x2.pred_rhs, 0))
1287 return false;
1289 c1 = x1.cond_code;
1290 if (x1.invert != x2.invert
1291 && TREE_CODE_CLASS (x2.cond_code) == tcc_comparison)
1292 c2 = invert_tree_comparison (x2.cond_code, false);
1293 else
1294 c2 = x2.cond_code;
1296 return c1 == c2;
1299 /* Returns true if the predication is testing !=. */
1301 static inline bool
1302 is_neq_relop_p (pred_info pred)
1305 return ((pred.cond_code == NE_EXPR && !pred.invert)
1306 || (pred.cond_code == EQ_EXPR && pred.invert));
1309 /* Returns true if pred is of the form X != 0. */
1311 static inline bool
1312 is_neq_zero_form_p (pred_info pred)
1314 if (!is_neq_relop_p (pred) || !integer_zerop (pred.pred_rhs)
1315 || TREE_CODE (pred.pred_lhs) != SSA_NAME)
1316 return false;
1317 return true;
1320 /* The helper function returns true if two predicates X1
1321 is equivalent to X2 != 0. */
1323 static inline bool
1324 pred_expr_equal_p (pred_info x1, tree x2)
1326 if (!is_neq_zero_form_p (x1))
1327 return false;
1329 return operand_equal_p (x1.pred_lhs, x2, 0);
1332 /* Returns true of the domain of single predicate expression
1333 EXPR1 is a subset of that of EXPR2. Returns false if it
1334 can not be proved. */
1336 static bool
1337 is_pred_expr_subset_of (pred_info expr1, pred_info expr2)
1339 enum tree_code code1, code2;
1341 if (pred_equal_p (expr1, expr2))
1342 return true;
1344 if ((TREE_CODE (expr1.pred_rhs) != INTEGER_CST)
1345 || (TREE_CODE (expr2.pred_rhs) != INTEGER_CST))
1346 return false;
1348 if (!operand_equal_p (expr1.pred_lhs, expr2.pred_lhs, 0))
1349 return false;
1351 code1 = expr1.cond_code;
1352 if (expr1.invert)
1353 code1 = invert_tree_comparison (code1, false);
1354 code2 = expr2.cond_code;
1355 if (expr2.invert)
1356 code2 = invert_tree_comparison (code2, false);
1358 if ((code1 == EQ_EXPR || code1 == BIT_AND_EXPR) && code2 == BIT_AND_EXPR)
1359 return wi::eq_p (expr1.pred_rhs,
1360 wi::bit_and (expr1.pred_rhs, expr2.pred_rhs));
1362 if (code1 != code2 && code2 != NE_EXPR)
1363 return false;
1365 if (is_value_included_in (expr1.pred_rhs, expr2.pred_rhs, code2))
1366 return true;
1368 return false;
1371 /* Returns true if the domain of PRED1 is a subset
1372 of that of PRED2. Returns false if it can not be proved so. */
1374 static bool
1375 is_pred_chain_subset_of (pred_chain pred1, pred_chain pred2)
1377 size_t np1, np2, i1, i2;
1379 np1 = pred1.length ();
1380 np2 = pred2.length ();
1382 for (i2 = 0; i2 < np2; i2++)
1384 bool found = false;
1385 pred_info info2 = pred2[i2];
1386 for (i1 = 0; i1 < np1; i1++)
1388 pred_info info1 = pred1[i1];
1389 if (is_pred_expr_subset_of (info1, info2))
1391 found = true;
1392 break;
1395 if (!found)
1396 return false;
1398 return true;
1401 /* Returns true if the domain defined by
1402 one pred chain ONE_PRED is a subset of the domain
1403 of *PREDS. It returns false if ONE_PRED's domain is
1404 not a subset of any of the sub-domains of PREDS
1405 (corresponding to each individual chains in it), even
1406 though it may be still be a subset of whole domain
1407 of PREDS which is the union (ORed) of all its subdomains.
1408 In other words, the result is conservative. */
1410 static bool
1411 is_included_in (pred_chain one_pred, pred_chain_union preds)
1413 size_t i;
1414 size_t n = preds.length ();
1416 for (i = 0; i < n; i++)
1418 if (is_pred_chain_subset_of (one_pred, preds[i]))
1419 return true;
1422 return false;
1425 /* Compares two predicate sets PREDS1 and PREDS2 and returns
1426 true if the domain defined by PREDS1 is a superset
1427 of PREDS2's domain. N1 and N2 are array sizes of PREDS1 and
1428 PREDS2 respectively. The implementation chooses not to build
1429 generic trees (and relying on the folding capability of the
1430 compiler), but instead performs brute force comparison of
1431 individual predicate chains (won't be a compile time problem
1432 as the chains are pretty short). When the function returns
1433 false, it does not necessarily mean *PREDS1 is not a superset
1434 of *PREDS2, but mean it may not be so since the analysis can
1435 not prove it. In such cases, false warnings may still be
1436 emitted. */
1438 static bool
1439 is_superset_of (pred_chain_union preds1, pred_chain_union preds2)
1441 size_t i, n2;
1442 pred_chain one_pred_chain = vNULL;
1444 n2 = preds2.length ();
1446 for (i = 0; i < n2; i++)
1448 one_pred_chain = preds2[i];
1449 if (!is_included_in (one_pred_chain, preds1))
1450 return false;
1453 return true;
1456 /* Returns true if TC is AND or OR. */
1458 static inline bool
1459 is_and_or_or_p (enum tree_code tc, tree type)
1461 return (tc == BIT_IOR_EXPR
1462 || (tc == BIT_AND_EXPR
1463 && (type == 0 || TREE_CODE (type) == BOOLEAN_TYPE)));
1466 /* Returns true if X1 is the negate of X2. */
1468 static inline bool
1469 pred_neg_p (pred_info x1, pred_info x2)
1471 enum tree_code c1, c2;
1472 if (!operand_equal_p (x1.pred_lhs, x2.pred_lhs, 0)
1473 || !operand_equal_p (x1.pred_rhs, x2.pred_rhs, 0))
1474 return false;
1476 c1 = x1.cond_code;
1477 if (x1.invert == x2.invert)
1478 c2 = invert_tree_comparison (x2.cond_code, false);
1479 else
1480 c2 = x2.cond_code;
1482 return c1 == c2;
1485 /* 1) ((x IOR y) != 0) AND (x != 0) is equivalent to (x != 0);
1486 2) (X AND Y) OR (!X AND Y) is equivalent to Y;
1487 3) X OR (!X AND Y) is equivalent to (X OR Y);
1488 4) ((x IAND y) != 0) || (x != 0 AND y != 0)) is equivalent to
1489 (x != 0 AND y != 0)
1490 5) (X AND Y) OR (!X AND Z) OR (!Y AND Z) is equivalent to
1491 (X AND Y) OR Z
1493 PREDS is the predicate chains, and N is the number of chains. */
1495 /* Helper function to implement rule 1 above. ONE_CHAIN is
1496 the AND predication to be simplified. */
1498 static void
1499 simplify_pred (pred_chain *one_chain)
1501 size_t i, j, n;
1502 bool simplified = false;
1503 pred_chain s_chain = vNULL;
1505 n = one_chain->length ();
1507 for (i = 0; i < n; i++)
1509 pred_info *a_pred = &(*one_chain)[i];
1511 if (!a_pred->pred_lhs)
1512 continue;
1513 if (!is_neq_zero_form_p (*a_pred))
1514 continue;
1516 gimple *def_stmt = SSA_NAME_DEF_STMT (a_pred->pred_lhs);
1517 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1518 continue;
1519 if (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR)
1521 for (j = 0; j < n; j++)
1523 pred_info *b_pred = &(*one_chain)[j];
1525 if (!b_pred->pred_lhs)
1526 continue;
1527 if (!is_neq_zero_form_p (*b_pred))
1528 continue;
1530 if (pred_expr_equal_p (*b_pred, gimple_assign_rhs1 (def_stmt))
1531 || pred_expr_equal_p (*b_pred, gimple_assign_rhs2 (def_stmt)))
1533 /* Mark a_pred for removal. */
1534 a_pred->pred_lhs = NULL;
1535 a_pred->pred_rhs = NULL;
1536 simplified = true;
1537 break;
1543 if (!simplified)
1544 return;
1546 for (i = 0; i < n; i++)
1548 pred_info *a_pred = &(*one_chain)[i];
1549 if (!a_pred->pred_lhs)
1550 continue;
1551 s_chain.safe_push (*a_pred);
1554 one_chain->release ();
1555 *one_chain = s_chain;
1558 /* The helper function implements the rule 2 for the
1559 OR predicate PREDS.
1561 2) (X AND Y) OR (!X AND Y) is equivalent to Y. */
1563 static bool
1564 simplify_preds_2 (pred_chain_union *preds)
1566 size_t i, j, n;
1567 bool simplified = false;
1568 pred_chain_union s_preds = vNULL;
1570 /* (X AND Y) OR (!X AND Y) is equivalent to Y.
1571 (X AND Y) OR (X AND !Y) is equivalent to X. */
1573 n = preds->length ();
1574 for (i = 0; i < n; i++)
1576 pred_info x, y;
1577 pred_chain *a_chain = &(*preds)[i];
1579 if (a_chain->length () != 2)
1580 continue;
1582 x = (*a_chain)[0];
1583 y = (*a_chain)[1];
1585 for (j = 0; j < n; j++)
1587 pred_chain *b_chain;
1588 pred_info x2, y2;
1590 if (j == i)
1591 continue;
1593 b_chain = &(*preds)[j];
1594 if (b_chain->length () != 2)
1595 continue;
1597 x2 = (*b_chain)[0];
1598 y2 = (*b_chain)[1];
1600 if (pred_equal_p (x, x2) && pred_neg_p (y, y2))
1602 /* Kill a_chain. */
1603 a_chain->release ();
1604 b_chain->release ();
1605 b_chain->safe_push (x);
1606 simplified = true;
1607 break;
1609 if (pred_neg_p (x, x2) && pred_equal_p (y, y2))
1611 /* Kill a_chain. */
1612 a_chain->release ();
1613 b_chain->release ();
1614 b_chain->safe_push (y);
1615 simplified = true;
1616 break;
1620 /* Now clean up the chain. */
1621 if (simplified)
1623 for (i = 0; i < n; i++)
1625 if ((*preds)[i].is_empty ())
1626 continue;
1627 s_preds.safe_push ((*preds)[i]);
1629 preds->release ();
1630 (*preds) = s_preds;
1631 s_preds = vNULL;
1634 return simplified;
1637 /* The helper function implements the rule 2 for the
1638 OR predicate PREDS.
1640 3) x OR (!x AND y) is equivalent to x OR y. */
1642 static bool
1643 simplify_preds_3 (pred_chain_union *preds)
1645 size_t i, j, n;
1646 bool simplified = false;
1648 /* Now iteratively simplify X OR (!X AND Z ..)
1649 into X OR (Z ...). */
1651 n = preds->length ();
1652 if (n < 2)
1653 return false;
1655 for (i = 0; i < n; i++)
1657 pred_info x;
1658 pred_chain *a_chain = &(*preds)[i];
1660 if (a_chain->length () != 1)
1661 continue;
1663 x = (*a_chain)[0];
1665 for (j = 0; j < n; j++)
1667 pred_chain *b_chain;
1668 pred_info x2;
1669 size_t k;
1671 if (j == i)
1672 continue;
1674 b_chain = &(*preds)[j];
1675 if (b_chain->length () < 2)
1676 continue;
1678 for (k = 0; k < b_chain->length (); k++)
1680 x2 = (*b_chain)[k];
1681 if (pred_neg_p (x, x2))
1683 b_chain->unordered_remove (k);
1684 simplified = true;
1685 break;
1690 return simplified;
1693 /* The helper function implements the rule 4 for the
1694 OR predicate PREDS.
1696 2) ((x AND y) != 0) OR (x != 0 AND y != 0) is equivalent to
1697 (x != 0 ANd y != 0). */
1699 static bool
1700 simplify_preds_4 (pred_chain_union *preds)
1702 size_t i, j, n;
1703 bool simplified = false;
1704 pred_chain_union s_preds = vNULL;
1705 gimple *def_stmt;
1707 n = preds->length ();
1708 for (i = 0; i < n; i++)
1710 pred_info z;
1711 pred_chain *a_chain = &(*preds)[i];
1713 if (a_chain->length () != 1)
1714 continue;
1716 z = (*a_chain)[0];
1718 if (!is_neq_zero_form_p (z))
1719 continue;
1721 def_stmt = SSA_NAME_DEF_STMT (z.pred_lhs);
1722 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1723 continue;
1725 if (gimple_assign_rhs_code (def_stmt) != BIT_AND_EXPR)
1726 continue;
1728 for (j = 0; j < n; j++)
1730 pred_chain *b_chain;
1731 pred_info x2, y2;
1733 if (j == i)
1734 continue;
1736 b_chain = &(*preds)[j];
1737 if (b_chain->length () != 2)
1738 continue;
1740 x2 = (*b_chain)[0];
1741 y2 = (*b_chain)[1];
1742 if (!is_neq_zero_form_p (x2) || !is_neq_zero_form_p (y2))
1743 continue;
1745 if ((pred_expr_equal_p (x2, gimple_assign_rhs1 (def_stmt))
1746 && pred_expr_equal_p (y2, gimple_assign_rhs2 (def_stmt)))
1747 || (pred_expr_equal_p (x2, gimple_assign_rhs2 (def_stmt))
1748 && pred_expr_equal_p (y2, gimple_assign_rhs1 (def_stmt))))
1750 /* Kill a_chain. */
1751 a_chain->release ();
1752 simplified = true;
1753 break;
1757 /* Now clean up the chain. */
1758 if (simplified)
1760 for (i = 0; i < n; i++)
1762 if ((*preds)[i].is_empty ())
1763 continue;
1764 s_preds.safe_push ((*preds)[i]);
1767 destroy_predicate_vecs (preds);
1768 (*preds) = s_preds;
1769 s_preds = vNULL;
1772 return simplified;
1775 /* This function simplifies predicates in PREDS. */
1777 static void
1778 simplify_preds (pred_chain_union *preds, gimple *use_or_def, bool is_use)
1780 size_t i, n;
1781 bool changed = false;
1783 if (dump_file && dump_flags & TDF_DETAILS)
1785 fprintf (dump_file, "[BEFORE SIMPLICATION -- ");
1786 dump_predicates (use_or_def, *preds, is_use ? "[USE]:\n" : "[DEF]:\n");
1789 for (i = 0; i < preds->length (); i++)
1790 simplify_pred (&(*preds)[i]);
1792 n = preds->length ();
1793 if (n < 2)
1794 return;
1798 changed = false;
1799 if (simplify_preds_2 (preds))
1800 changed = true;
1802 /* Now iteratively simplify X OR (!X AND Z ..)
1803 into X OR (Z ...). */
1804 if (simplify_preds_3 (preds))
1805 changed = true;
1807 if (simplify_preds_4 (preds))
1808 changed = true;
1810 while (changed);
1812 return;
1815 /* This is a helper function which attempts to normalize predicate chains
1816 by following UD chains. It basically builds up a big tree of either IOR
1817 operations or AND operations, and convert the IOR tree into a
1818 pred_chain_union or BIT_AND tree into a pred_chain.
1819 Example:
1821 _3 = _2 RELOP1 _1;
1822 _6 = _5 RELOP2 _4;
1823 _9 = _8 RELOP3 _7;
1824 _10 = _3 | _6;
1825 _12 = _9 | _0;
1826 _t = _10 | _12;
1828 then _t != 0 will be normalized into a pred_chain_union
1830 (_2 RELOP1 _1) OR (_5 RELOP2 _4) OR (_8 RELOP3 _7) OR (_0 != 0)
1832 Similarly given,
1834 _3 = _2 RELOP1 _1;
1835 _6 = _5 RELOP2 _4;
1836 _9 = _8 RELOP3 _7;
1837 _10 = _3 & _6;
1838 _12 = _9 & _0;
1840 then _t != 0 will be normalized into a pred_chain:
1841 (_2 RELOP1 _1) AND (_5 RELOP2 _4) AND (_8 RELOP3 _7) AND (_0 != 0)
1845 /* This is a helper function that stores a PRED into NORM_PREDS. */
1847 inline static void
1848 push_pred (pred_chain_union *norm_preds, pred_info pred)
1850 pred_chain pred_chain = vNULL;
1851 pred_chain.safe_push (pred);
1852 norm_preds->safe_push (pred_chain);
1855 /* A helper function that creates a predicate of the form
1856 OP != 0 and push it WORK_LIST. */
1858 inline static void
1859 push_to_worklist (tree op, vec<pred_info, va_heap, vl_ptr> *work_list,
1860 hash_set<tree> *mark_set)
1862 if (mark_set->contains (op))
1863 return;
1864 mark_set->add (op);
1866 pred_info arg_pred;
1867 arg_pred.pred_lhs = op;
1868 arg_pred.pred_rhs = integer_zero_node;
1869 arg_pred.cond_code = NE_EXPR;
1870 arg_pred.invert = false;
1871 work_list->safe_push (arg_pred);
1874 /* A helper that generates a pred_info from a gimple assignment
1875 CMP_ASSIGN with comparison rhs. */
1877 static pred_info
1878 get_pred_info_from_cmp (gimple *cmp_assign)
1880 pred_info n_pred;
1881 n_pred.pred_lhs = gimple_assign_rhs1 (cmp_assign);
1882 n_pred.pred_rhs = gimple_assign_rhs2 (cmp_assign);
1883 n_pred.cond_code = gimple_assign_rhs_code (cmp_assign);
1884 n_pred.invert = false;
1885 return n_pred;
1888 /* Returns true if the PHI is a degenerated phi with
1889 all args with the same value (relop). In that case, *PRED
1890 will be updated to that value. */
1892 static bool
1893 is_degenerated_phi (gimple *phi, pred_info *pred_p)
1895 int i, n;
1896 tree op0;
1897 gimple *def0;
1898 pred_info pred0;
1900 n = gimple_phi_num_args (phi);
1901 op0 = gimple_phi_arg_def (phi, 0);
1903 if (TREE_CODE (op0) != SSA_NAME)
1904 return false;
1906 def0 = SSA_NAME_DEF_STMT (op0);
1907 if (gimple_code (def0) != GIMPLE_ASSIGN)
1908 return false;
1909 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def0)) != tcc_comparison)
1910 return false;
1911 pred0 = get_pred_info_from_cmp (def0);
1913 for (i = 1; i < n; ++i)
1915 gimple *def;
1916 pred_info pred;
1917 tree op = gimple_phi_arg_def (phi, i);
1919 if (TREE_CODE (op) != SSA_NAME)
1920 return false;
1922 def = SSA_NAME_DEF_STMT (op);
1923 if (gimple_code (def) != GIMPLE_ASSIGN)
1924 return false;
1925 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def)) != tcc_comparison)
1926 return false;
1927 pred = get_pred_info_from_cmp (def);
1928 if (!pred_equal_p (pred, pred0))
1929 return false;
1932 *pred_p = pred0;
1933 return true;
1936 /* Normalize one predicate PRED
1937 1) if PRED can no longer be normlized, put it into NORM_PREDS.
1938 2) otherwise if PRED is of the form x != 0, follow x's definition
1939 and put normalized predicates into WORK_LIST. */
1941 static void
1942 normalize_one_pred_1 (pred_chain_union *norm_preds,
1943 pred_chain *norm_chain,
1944 pred_info pred,
1945 enum tree_code and_or_code,
1946 vec<pred_info, va_heap, vl_ptr> *work_list,
1947 hash_set<tree> *mark_set)
1949 if (!is_neq_zero_form_p (pred))
1951 if (and_or_code == BIT_IOR_EXPR)
1952 push_pred (norm_preds, pred);
1953 else
1954 norm_chain->safe_push (pred);
1955 return;
1958 gimple *def_stmt = SSA_NAME_DEF_STMT (pred.pred_lhs);
1960 if (gimple_code (def_stmt) == GIMPLE_PHI
1961 && is_degenerated_phi (def_stmt, &pred))
1962 work_list->safe_push (pred);
1963 else if (gimple_code (def_stmt) == GIMPLE_PHI && and_or_code == BIT_IOR_EXPR)
1965 int i, n;
1966 n = gimple_phi_num_args (def_stmt);
1968 /* If we see non zero constant, we should punt. The predicate
1969 * should be one guarding the phi edge. */
1970 for (i = 0; i < n; ++i)
1972 tree op = gimple_phi_arg_def (def_stmt, i);
1973 if (TREE_CODE (op) == INTEGER_CST && !integer_zerop (op))
1975 push_pred (norm_preds, pred);
1976 return;
1980 for (i = 0; i < n; ++i)
1982 tree op = gimple_phi_arg_def (def_stmt, i);
1983 if (integer_zerop (op))
1984 continue;
1986 push_to_worklist (op, work_list, mark_set);
1989 else if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1991 if (and_or_code == BIT_IOR_EXPR)
1992 push_pred (norm_preds, pred);
1993 else
1994 norm_chain->safe_push (pred);
1996 else if (gimple_assign_rhs_code (def_stmt) == and_or_code)
1998 /* Avoid splitting up bit manipulations like x & 3 or y | 1. */
1999 if (is_gimple_min_invariant (gimple_assign_rhs2 (def_stmt)))
2001 /* But treat x & 3 as condition. */
2002 if (and_or_code == BIT_AND_EXPR)
2004 pred_info n_pred;
2005 n_pred.pred_lhs = gimple_assign_rhs1 (def_stmt);
2006 n_pred.pred_rhs = gimple_assign_rhs2 (def_stmt);
2007 n_pred.cond_code = and_or_code;
2008 n_pred.invert = false;
2009 norm_chain->safe_push (n_pred);
2012 else
2014 push_to_worklist (gimple_assign_rhs1 (def_stmt), work_list, mark_set);
2015 push_to_worklist (gimple_assign_rhs2 (def_stmt), work_list, mark_set);
2018 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt))
2019 == tcc_comparison)
2021 pred_info n_pred = get_pred_info_from_cmp (def_stmt);
2022 if (and_or_code == BIT_IOR_EXPR)
2023 push_pred (norm_preds, n_pred);
2024 else
2025 norm_chain->safe_push (n_pred);
2027 else
2029 if (and_or_code == BIT_IOR_EXPR)
2030 push_pred (norm_preds, pred);
2031 else
2032 norm_chain->safe_push (pred);
2036 /* Normalize PRED and store the normalized predicates into NORM_PREDS. */
2038 static void
2039 normalize_one_pred (pred_chain_union *norm_preds, pred_info pred)
2041 vec<pred_info, va_heap, vl_ptr> work_list = vNULL;
2042 enum tree_code and_or_code = ERROR_MARK;
2043 pred_chain norm_chain = vNULL;
2045 if (!is_neq_zero_form_p (pred))
2047 push_pred (norm_preds, pred);
2048 return;
2051 gimple *def_stmt = SSA_NAME_DEF_STMT (pred.pred_lhs);
2052 if (gimple_code (def_stmt) == GIMPLE_ASSIGN)
2053 and_or_code = gimple_assign_rhs_code (def_stmt);
2054 if (and_or_code != BIT_IOR_EXPR && and_or_code != BIT_AND_EXPR)
2056 if (TREE_CODE_CLASS (and_or_code) == tcc_comparison)
2058 pred_info n_pred = get_pred_info_from_cmp (def_stmt);
2059 push_pred (norm_preds, n_pred);
2061 else
2062 push_pred (norm_preds, pred);
2063 return;
2066 work_list.safe_push (pred);
2067 hash_set<tree> mark_set;
2069 while (!work_list.is_empty ())
2071 pred_info a_pred = work_list.pop ();
2072 normalize_one_pred_1 (norm_preds, &norm_chain, a_pred, and_or_code,
2073 &work_list, &mark_set);
2075 if (and_or_code == BIT_AND_EXPR)
2076 norm_preds->safe_push (norm_chain);
2078 work_list.release ();
2081 static void
2082 normalize_one_pred_chain (pred_chain_union *norm_preds, pred_chain one_chain)
2084 vec<pred_info, va_heap, vl_ptr> work_list = vNULL;
2085 hash_set<tree> mark_set;
2086 pred_chain norm_chain = vNULL;
2087 size_t i;
2089 for (i = 0; i < one_chain.length (); i++)
2091 work_list.safe_push (one_chain[i]);
2092 mark_set.add (one_chain[i].pred_lhs);
2095 while (!work_list.is_empty ())
2097 pred_info a_pred = work_list.pop ();
2098 normalize_one_pred_1 (0, &norm_chain, a_pred, BIT_AND_EXPR, &work_list,
2099 &mark_set);
2102 norm_preds->safe_push (norm_chain);
2103 work_list.release ();
2106 /* Normalize predicate chains PREDS and returns the normalized one. */
2108 static pred_chain_union
2109 normalize_preds (pred_chain_union preds, gimple *use_or_def, bool is_use)
2111 pred_chain_union norm_preds = vNULL;
2112 size_t n = preds.length ();
2113 size_t i;
2115 if (dump_file && dump_flags & TDF_DETAILS)
2117 fprintf (dump_file, "[BEFORE NORMALIZATION --");
2118 dump_predicates (use_or_def, preds, is_use ? "[USE]:\n" : "[DEF]:\n");
2121 for (i = 0; i < n; i++)
2123 if (preds[i].length () != 1)
2124 normalize_one_pred_chain (&norm_preds, preds[i]);
2125 else
2127 normalize_one_pred (&norm_preds, preds[i][0]);
2128 preds[i].release ();
2132 if (dump_file)
2134 fprintf (dump_file, "[AFTER NORMALIZATION -- ");
2135 dump_predicates (use_or_def, norm_preds,
2136 is_use ? "[USE]:\n" : "[DEF]:\n");
2139 destroy_predicate_vecs (&preds);
2140 return norm_preds;
2143 /* Computes the predicates that guard the use and checks
2144 if the incoming paths that have empty (or possibly
2145 empty) definition can be pruned/filtered. The function returns
2146 true if it can be determined that the use of PHI's def in
2147 USE_STMT is guarded with a predicate set not overlapping with
2148 predicate sets of all runtime paths that do not have a definition.
2150 Returns false if it is not or it can not be determined. USE_BB is
2151 the bb of the use (for phi operand use, the bb is not the bb of
2152 the phi stmt, but the src bb of the operand edge).
2154 UNINIT_OPNDS is a bit vector. If an operand of PHI is uninitialized, the
2155 corresponding bit in the vector is 1. VISITED_PHIS is a pointer
2156 set of phis being visited.
2158 *DEF_PREDS contains the (memoized) defining predicate chains of PHI.
2159 If *DEF_PREDS is the empty vector, the defining predicate chains of
2160 PHI will be computed and stored into *DEF_PREDS as needed.
2162 VISITED_PHIS is a pointer set of phis being visited. */
2164 static bool
2165 is_use_properly_guarded (gimple *use_stmt,
2166 basic_block use_bb,
2167 gphi *phi,
2168 unsigned uninit_opnds,
2169 pred_chain_union *def_preds,
2170 hash_set<gphi *> *visited_phis)
2172 basic_block phi_bb;
2173 pred_chain_union preds = vNULL;
2174 bool has_valid_preds = false;
2175 bool is_properly_guarded = false;
2177 if (visited_phis->add (phi))
2178 return false;
2180 phi_bb = gimple_bb (phi);
2182 if (is_non_loop_exit_postdominating (use_bb, phi_bb))
2183 return false;
2185 has_valid_preds = find_predicates (&preds, phi_bb, use_bb);
2187 if (!has_valid_preds)
2189 destroy_predicate_vecs (&preds);
2190 return false;
2193 /* Try to prune the dead incoming phi edges. */
2194 is_properly_guarded
2195 = use_pred_not_overlap_with_undef_path_pred (preds, phi, uninit_opnds,
2196 visited_phis);
2198 if (is_properly_guarded)
2200 destroy_predicate_vecs (&preds);
2201 return true;
2204 if (def_preds->is_empty ())
2206 has_valid_preds = find_def_preds (def_preds, phi);
2208 if (!has_valid_preds)
2210 destroy_predicate_vecs (&preds);
2211 return false;
2214 simplify_preds (def_preds, phi, false);
2215 *def_preds = normalize_preds (*def_preds, phi, false);
2218 simplify_preds (&preds, use_stmt, true);
2219 preds = normalize_preds (preds, use_stmt, true);
2221 is_properly_guarded = is_superset_of (*def_preds, preds);
2223 destroy_predicate_vecs (&preds);
2224 return is_properly_guarded;
2227 /* Searches through all uses of a potentially
2228 uninitialized variable defined by PHI and returns a use
2229 statement if the use is not properly guarded. It returns
2230 NULL if all uses are guarded. UNINIT_OPNDS is a bitvector
2231 holding the position(s) of uninit PHI operands. WORKLIST
2232 is the vector of candidate phis that may be updated by this
2233 function. ADDED_TO_WORKLIST is the pointer set tracking
2234 if the new phi is already in the worklist. */
2236 static gimple *
2237 find_uninit_use (gphi *phi, unsigned uninit_opnds,
2238 vec<gphi *> *worklist,
2239 hash_set<gphi *> *added_to_worklist)
2241 tree phi_result;
2242 use_operand_p use_p;
2243 gimple *use_stmt;
2244 imm_use_iterator iter;
2245 pred_chain_union def_preds = vNULL;
2246 gimple *ret = NULL;
2248 phi_result = gimple_phi_result (phi);
2250 FOR_EACH_IMM_USE_FAST (use_p, iter, phi_result)
2252 basic_block use_bb;
2254 use_stmt = USE_STMT (use_p);
2255 if (is_gimple_debug (use_stmt))
2256 continue;
2258 if (gphi *use_phi = dyn_cast<gphi *> (use_stmt))
2259 use_bb = gimple_phi_arg_edge (use_phi,
2260 PHI_ARG_INDEX_FROM_USE (use_p))->src;
2261 else
2262 use_bb = gimple_bb (use_stmt);
2264 hash_set<gphi *> visited_phis;
2265 if (is_use_properly_guarded (use_stmt, use_bb, phi, uninit_opnds,
2266 &def_preds, &visited_phis))
2267 continue;
2269 if (dump_file && (dump_flags & TDF_DETAILS))
2271 fprintf (dump_file, "[CHECK]: Found unguarded use: ");
2272 print_gimple_stmt (dump_file, use_stmt, 0, 0);
2274 /* Found one real use, return. */
2275 if (gimple_code (use_stmt) != GIMPLE_PHI)
2277 ret = use_stmt;
2278 break;
2281 /* Found a phi use that is not guarded,
2282 add the phi to the worklist. */
2283 if (!added_to_worklist->add (as_a<gphi *> (use_stmt)))
2285 if (dump_file && (dump_flags & TDF_DETAILS))
2287 fprintf (dump_file, "[WORKLIST]: Update worklist with phi: ");
2288 print_gimple_stmt (dump_file, use_stmt, 0, 0);
2291 worklist->safe_push (as_a<gphi *> (use_stmt));
2292 possibly_undefined_names->add (phi_result);
2296 destroy_predicate_vecs (&def_preds);
2297 return ret;
2300 /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions
2301 and gives warning if there exists a runtime path from the entry to a
2302 use of the PHI def that does not contain a definition. In other words,
2303 the warning is on the real use. The more dead paths that can be pruned
2304 by the compiler, the fewer false positives the warning is. WORKLIST
2305 is a vector of candidate phis to be examined. ADDED_TO_WORKLIST is
2306 a pointer set tracking if the new phi is added to the worklist or not. */
2308 static void
2309 warn_uninitialized_phi (gphi *phi, vec<gphi *> *worklist,
2310 hash_set<gphi *> *added_to_worklist)
2312 unsigned uninit_opnds;
2313 gimple *uninit_use_stmt = 0;
2314 tree uninit_op;
2315 int phiarg_index;
2316 location_t loc;
2318 /* Don't look at virtual operands. */
2319 if (virtual_operand_p (gimple_phi_result (phi)))
2320 return;
2322 uninit_opnds = compute_uninit_opnds_pos (phi);
2324 if (MASK_EMPTY (uninit_opnds))
2325 return;
2327 if (dump_file && (dump_flags & TDF_DETAILS))
2329 fprintf (dump_file, "[CHECK]: examining phi: ");
2330 print_gimple_stmt (dump_file, phi, 0, 0);
2333 /* Now check if we have any use of the value without proper guard. */
2334 uninit_use_stmt = find_uninit_use (phi, uninit_opnds,
2335 worklist, added_to_worklist);
2337 /* All uses are properly guarded. */
2338 if (!uninit_use_stmt)
2339 return;
2341 phiarg_index = MASK_FIRST_SET_BIT (uninit_opnds);
2342 uninit_op = gimple_phi_arg_def (phi, phiarg_index);
2343 if (SSA_NAME_VAR (uninit_op) == NULL_TREE)
2344 return;
2345 if (gimple_phi_arg_has_location (phi, phiarg_index))
2346 loc = gimple_phi_arg_location (phi, phiarg_index);
2347 else
2348 loc = UNKNOWN_LOCATION;
2349 warn_uninit (OPT_Wmaybe_uninitialized, uninit_op, SSA_NAME_VAR (uninit_op),
2350 SSA_NAME_VAR (uninit_op),
2351 "%qD may be used uninitialized in this function",
2352 uninit_use_stmt, loc);
2355 static bool
2356 gate_warn_uninitialized (void)
2358 return warn_uninitialized || warn_maybe_uninitialized;
2361 namespace {
2363 const pass_data pass_data_late_warn_uninitialized =
2365 GIMPLE_PASS, /* type */
2366 "uninit", /* name */
2367 OPTGROUP_NONE, /* optinfo_flags */
2368 TV_NONE, /* tv_id */
2369 PROP_ssa, /* properties_required */
2370 0, /* properties_provided */
2371 0, /* properties_destroyed */
2372 0, /* todo_flags_start */
2373 0, /* todo_flags_finish */
2376 class pass_late_warn_uninitialized : public gimple_opt_pass
2378 public:
2379 pass_late_warn_uninitialized (gcc::context *ctxt)
2380 : gimple_opt_pass (pass_data_late_warn_uninitialized, ctxt)
2383 /* opt_pass methods: */
2384 opt_pass *clone () { return new pass_late_warn_uninitialized (m_ctxt); }
2385 virtual bool gate (function *) { return gate_warn_uninitialized (); }
2386 virtual unsigned int execute (function *);
2388 }; // class pass_late_warn_uninitialized
2390 unsigned int
2391 pass_late_warn_uninitialized::execute (function *fun)
2393 basic_block bb;
2394 gphi_iterator gsi;
2395 vec<gphi *> worklist = vNULL;
2397 calculate_dominance_info (CDI_DOMINATORS);
2398 calculate_dominance_info (CDI_POST_DOMINATORS);
2399 /* Re-do the plain uninitialized variable check, as optimization may have
2400 straightened control flow. Do this first so that we don't accidentally
2401 get a "may be" warning when we'd have seen an "is" warning later. */
2402 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/1);
2404 timevar_push (TV_TREE_UNINIT);
2406 possibly_undefined_names = new hash_set<tree>;
2407 hash_set<gphi *> added_to_worklist;
2409 /* Initialize worklist */
2410 FOR_EACH_BB_FN (bb, fun)
2411 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2413 gphi *phi = gsi.phi ();
2414 size_t n, i;
2416 n = gimple_phi_num_args (phi);
2418 /* Don't look at virtual operands. */
2419 if (virtual_operand_p (gimple_phi_result (phi)))
2420 continue;
2422 for (i = 0; i < n; ++i)
2424 tree op = gimple_phi_arg_def (phi, i);
2425 if (TREE_CODE (op) == SSA_NAME && uninit_undefined_value_p (op))
2427 worklist.safe_push (phi);
2428 added_to_worklist.add (phi);
2429 if (dump_file && (dump_flags & TDF_DETAILS))
2431 fprintf (dump_file, "[WORKLIST]: add to initial list: ");
2432 print_gimple_stmt (dump_file, phi, 0, 0);
2434 break;
2439 while (worklist.length () != 0)
2441 gphi *cur_phi = 0;
2442 cur_phi = worklist.pop ();
2443 warn_uninitialized_phi (cur_phi, &worklist, &added_to_worklist);
2446 worklist.release ();
2447 delete possibly_undefined_names;
2448 possibly_undefined_names = NULL;
2449 free_dominance_info (CDI_POST_DOMINATORS);
2450 timevar_pop (TV_TREE_UNINIT);
2451 return 0;
2454 } // anon namespace
2456 gimple_opt_pass *
2457 make_pass_late_warn_uninitialized (gcc::context *ctxt)
2459 return new pass_late_warn_uninitialized (ctxt);
2462 static unsigned int
2463 execute_early_warn_uninitialized (void)
2465 /* Currently, this pass runs always but
2466 execute_late_warn_uninitialized only runs with optimization. With
2467 optimization we want to warn about possible uninitialized as late
2468 as possible, thus don't do it here. However, without
2469 optimization we need to warn here about "may be uninitialized". */
2470 calculate_dominance_info (CDI_POST_DOMINATORS);
2472 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/!optimize);
2474 /* Post-dominator information can not be reliably updated. Free it
2475 after the use. */
2477 free_dominance_info (CDI_POST_DOMINATORS);
2478 return 0;
2481 namespace {
2483 const pass_data pass_data_early_warn_uninitialized =
2485 GIMPLE_PASS, /* type */
2486 "*early_warn_uninitialized", /* name */
2487 OPTGROUP_NONE, /* optinfo_flags */
2488 TV_TREE_UNINIT, /* tv_id */
2489 PROP_ssa, /* properties_required */
2490 0, /* properties_provided */
2491 0, /* properties_destroyed */
2492 0, /* todo_flags_start */
2493 0, /* todo_flags_finish */
2496 class pass_early_warn_uninitialized : public gimple_opt_pass
2498 public:
2499 pass_early_warn_uninitialized (gcc::context *ctxt)
2500 : gimple_opt_pass (pass_data_early_warn_uninitialized, ctxt)
2503 /* opt_pass methods: */
2504 virtual bool gate (function *) { return gate_warn_uninitialized (); }
2505 virtual unsigned int execute (function *)
2507 return execute_early_warn_uninitialized ();
2510 }; // class pass_early_warn_uninitialized
2512 } // anon namespace
2514 gimple_opt_pass *
2515 make_pass_early_warn_uninitialized (gcc::context *ctxt)
2517 return new pass_early_warn_uninitialized (ctxt);