Only allow e500 double in SPE_SIMD_REGNO_P registers.
[official-gcc.git] / gcc / tree-ssa-uninit.c
blobbe93d5fd31808b63737cc8543473b0173f3dee4c
1 /* Predicate aware uninitialized variable warning.
2 Copyright (C) 2001-2014 Free Software Foundation, Inc.
3 Contributed by Xinliang David Li <davidxl@google.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "tm_p.h"
28 #include "basic-block.h"
29 #include "hashtab.h"
30 #include "hash-set.h"
31 #include "vec.h"
32 #include "machmode.h"
33 #include "hard-reg-set.h"
34 #include "input.h"
35 #include "function.h"
36 #include "gimple-pretty-print.h"
37 #include "bitmap.h"
38 #include "tree-ssa-alias.h"
39 #include "internal-fn.h"
40 #include "gimple-expr.h"
41 #include "is-a.h"
42 #include "gimple.h"
43 #include "gimple-iterator.h"
44 #include "gimple-ssa.h"
45 #include "tree-phinodes.h"
46 #include "ssa-iterators.h"
47 #include "tree-ssa.h"
48 #include "tree-inline.h"
49 #include "tree-pass.h"
50 #include "diagnostic-core.h"
51 #include "params.h"
53 /* This implements the pass that does predicate aware warning on uses of
54 possibly uninitialized variables. The pass first collects the set of
55 possibly uninitialized SSA names. For each such name, it walks through
56 all its immediate uses. For each immediate use, it rebuilds the condition
57 expression (the predicate) that guards the use. The predicate is then
58 examined to see if the variable is always defined under that same condition.
59 This is done either by pruning the unrealizable paths that lead to the
60 default definitions or by checking if the predicate set that guards the
61 defining paths is a superset of the use predicate. */
64 /* Pointer set of potentially undefined ssa names, i.e.,
65 ssa names that are defined by phi with operands that
66 are not defined or potentially undefined. */
67 static hash_set<tree> *possibly_undefined_names = 0;
69 /* Bit mask handling macros. */
70 #define MASK_SET_BIT(mask, pos) mask |= (1 << pos)
71 #define MASK_TEST_BIT(mask, pos) (mask & (1 << pos))
72 #define MASK_EMPTY(mask) (mask == 0)
74 /* Returns the first bit position (starting from LSB)
75 in mask that is non zero. Returns -1 if the mask is empty. */
76 static int
77 get_mask_first_set_bit (unsigned mask)
79 int pos = 0;
80 if (mask == 0)
81 return -1;
83 while ((mask & (1 << pos)) == 0)
84 pos++;
86 return pos;
88 #define MASK_FIRST_SET_BIT(mask) get_mask_first_set_bit (mask)
90 /* Return true if T, an SSA_NAME, has an undefined value. */
91 static bool
92 has_undefined_value_p (tree t)
94 return (ssa_undefined_value_p (t)
95 || (possibly_undefined_names
96 && possibly_undefined_names->contains (t)));
101 /* Like has_undefined_value_p, but don't return true if TREE_NO_WARNING
102 is set on SSA_NAME_VAR. */
104 static inline bool
105 uninit_undefined_value_p (tree t) {
106 if (!has_undefined_value_p (t))
107 return false;
108 if (SSA_NAME_VAR (t) && TREE_NO_WARNING (SSA_NAME_VAR (t)))
109 return false;
110 return true;
113 /* Emit warnings for uninitialized variables. This is done in two passes.
115 The first pass notices real uses of SSA names with undefined values.
116 Such uses are unconditionally uninitialized, and we can be certain that
117 such a use is a mistake. This pass is run before most optimizations,
118 so that we catch as many as we can.
120 The second pass follows PHI nodes to find uses that are potentially
121 uninitialized. In this case we can't necessarily prove that the use
122 is really uninitialized. This pass is run after most optimizations,
123 so that we thread as many jumps and possible, and delete as much dead
124 code as possible, in order to reduce false positives. We also look
125 again for plain uninitialized variables, since optimization may have
126 changed conditionally uninitialized to unconditionally uninitialized. */
128 /* Emit a warning for EXPR based on variable VAR at the point in the
129 program T, an SSA_NAME, is used being uninitialized. The exact
130 warning text is in MSGID and DATA is the gimple stmt with info about
131 the location in source code. When DATA is a GIMPLE_PHI, PHIARG_IDX
132 gives which argument of the phi node to take the location from. WC
133 is the warning code. */
135 static void
136 warn_uninit (enum opt_code wc, tree t, tree expr, tree var,
137 const char *gmsgid, void *data, location_t phiarg_loc)
139 gimple context = (gimple) data;
140 location_t location, cfun_loc;
141 expanded_location xloc, floc;
143 /* Ignore COMPLEX_EXPR as initializing only a part of a complex
144 turns in a COMPLEX_EXPR with the not initialized part being
145 set to its previous (undefined) value. */
146 if (is_gimple_assign (context)
147 && gimple_assign_rhs_code (context) == COMPLEX_EXPR)
148 return;
149 if (!has_undefined_value_p (t))
150 return;
152 /* TREE_NO_WARNING either means we already warned, or the front end
153 wishes to suppress the warning. */
154 if ((context
155 && (gimple_no_warning_p (context)
156 || (gimple_assign_single_p (context)
157 && TREE_NO_WARNING (gimple_assign_rhs1 (context)))))
158 || TREE_NO_WARNING (expr))
159 return;
161 if (context != NULL && gimple_has_location (context))
162 location = gimple_location (context);
163 else if (phiarg_loc != UNKNOWN_LOCATION)
164 location = phiarg_loc;
165 else
166 location = DECL_SOURCE_LOCATION (var);
167 location = linemap_resolve_location (line_table, location,
168 LRK_SPELLING_LOCATION,
169 NULL);
170 cfun_loc = DECL_SOURCE_LOCATION (cfun->decl);
171 xloc = expand_location (location);
172 floc = expand_location (cfun_loc);
173 if (warning_at (location, wc, gmsgid, expr))
175 TREE_NO_WARNING (expr) = 1;
177 if (location == DECL_SOURCE_LOCATION (var))
178 return;
179 if (xloc.file != floc.file
180 || linemap_location_before_p (line_table,
181 location, cfun_loc)
182 || linemap_location_before_p (line_table,
183 cfun->function_end_locus,
184 location))
185 inform (DECL_SOURCE_LOCATION (var), "%qD was declared here", var);
189 static unsigned int
190 warn_uninitialized_vars (bool warn_possibly_uninitialized)
192 gimple_stmt_iterator gsi;
193 basic_block bb;
195 FOR_EACH_BB_FN (bb, cfun)
197 bool always_executed = dominated_by_p (CDI_POST_DOMINATORS,
198 single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)), bb);
199 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
201 gimple stmt = gsi_stmt (gsi);
202 use_operand_p use_p;
203 ssa_op_iter op_iter;
204 tree use;
206 if (is_gimple_debug (stmt))
207 continue;
209 /* We only do data flow with SSA_NAMEs, so that's all we
210 can warn about. */
211 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, op_iter, SSA_OP_USE)
213 use = USE_FROM_PTR (use_p);
214 if (always_executed)
215 warn_uninit (OPT_Wuninitialized, use,
216 SSA_NAME_VAR (use), SSA_NAME_VAR (use),
217 "%qD is used uninitialized in this function",
218 stmt, UNKNOWN_LOCATION);
219 else if (warn_possibly_uninitialized)
220 warn_uninit (OPT_Wmaybe_uninitialized, use,
221 SSA_NAME_VAR (use), SSA_NAME_VAR (use),
222 "%qD may be used uninitialized in this function",
223 stmt, UNKNOWN_LOCATION);
226 /* For memory the only cheap thing we can do is see if we
227 have a use of the default def of the virtual operand.
228 ??? Not so cheap would be to use the alias oracle via
229 walk_aliased_vdefs, if we don't find any aliasing vdef
230 warn as is-used-uninitialized, if we don't find an aliasing
231 vdef that kills our use (stmt_kills_ref_p), warn as
232 may-be-used-uninitialized. But this walk is quadratic and
233 so must be limited which means we would miss warning
234 opportunities. */
235 use = gimple_vuse (stmt);
236 if (use
237 && gimple_assign_single_p (stmt)
238 && !gimple_vdef (stmt)
239 && SSA_NAME_IS_DEFAULT_DEF (use))
241 tree rhs = gimple_assign_rhs1 (stmt);
242 tree base = get_base_address (rhs);
244 /* Do not warn if it can be initialized outside this function. */
245 if (TREE_CODE (base) != VAR_DECL
246 || DECL_HARD_REGISTER (base)
247 || is_global_var (base))
248 continue;
250 if (always_executed)
251 warn_uninit (OPT_Wuninitialized, use,
252 gimple_assign_rhs1 (stmt), base,
253 "%qE is used uninitialized in this function",
254 stmt, UNKNOWN_LOCATION);
255 else if (warn_possibly_uninitialized)
256 warn_uninit (OPT_Wmaybe_uninitialized, use,
257 gimple_assign_rhs1 (stmt), base,
258 "%qE may be used uninitialized in this function",
259 stmt, UNKNOWN_LOCATION);
264 return 0;
267 /* Checks if the operand OPND of PHI is defined by
268 another phi with one operand defined by this PHI,
269 but the rest operands are all defined. If yes,
270 returns true to skip this this operand as being
271 redundant. Can be enhanced to be more general. */
273 static bool
274 can_skip_redundant_opnd (tree opnd, gimple phi)
276 gimple op_def;
277 tree phi_def;
278 int i, n;
280 phi_def = gimple_phi_result (phi);
281 op_def = SSA_NAME_DEF_STMT (opnd);
282 if (gimple_code (op_def) != GIMPLE_PHI)
283 return false;
284 n = gimple_phi_num_args (op_def);
285 for (i = 0; i < n; ++i)
287 tree op = gimple_phi_arg_def (op_def, i);
288 if (TREE_CODE (op) != SSA_NAME)
289 continue;
290 if (op != phi_def && uninit_undefined_value_p (op))
291 return false;
294 return true;
297 /* Returns a bit mask holding the positions of arguments in PHI
298 that have empty (or possibly empty) definitions. */
300 static unsigned
301 compute_uninit_opnds_pos (gimple phi)
303 size_t i, n;
304 unsigned uninit_opnds = 0;
306 n = gimple_phi_num_args (phi);
307 /* Bail out for phi with too many args. */
308 if (n > 32)
309 return 0;
311 for (i = 0; i < n; ++i)
313 tree op = gimple_phi_arg_def (phi, i);
314 if (TREE_CODE (op) == SSA_NAME
315 && uninit_undefined_value_p (op)
316 && !can_skip_redundant_opnd (op, phi))
318 if (cfun->has_nonlocal_label || cfun->calls_setjmp)
320 /* Ignore SSA_NAMEs that appear on abnormal edges
321 somewhere. */
322 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
323 continue;
325 MASK_SET_BIT (uninit_opnds, i);
328 return uninit_opnds;
331 /* Find the immediate postdominator PDOM of the specified
332 basic block BLOCK. */
334 static inline basic_block
335 find_pdom (basic_block block)
337 if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
338 return EXIT_BLOCK_PTR_FOR_FN (cfun);
339 else
341 basic_block bb
342 = 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
350 basic block BLOCK. */
352 static inline basic_block
353 find_dom (basic_block block)
355 if (block == ENTRY_BLOCK_PTR_FOR_FN (cfun))
356 return ENTRY_BLOCK_PTR_FOR_FN (cfun);
357 else
359 basic_block bb = get_immediate_dominator (CDI_DOMINATORS, block);
360 if (! bb)
361 return ENTRY_BLOCK_PTR_FOR_FN (cfun);
362 return bb;
366 /* Returns true if BB1 is postdominating BB2 and BB1 is
367 not a loop exit bb. The loop exit bb check is simple and does
368 not cover all cases. */
370 static bool
371 is_non_loop_exit_postdominating (basic_block bb1, basic_block bb2)
373 if (!dominated_by_p (CDI_POST_DOMINATORS, bb2, bb1))
374 return false;
376 if (single_pred_p (bb1) && !single_succ_p (bb2))
377 return false;
379 return true;
382 /* Find the closest postdominator of a specified BB, which is control
383 equivalent to BB. */
385 static inline basic_block
386 find_control_equiv_block (basic_block bb)
388 basic_block pdom;
390 pdom = find_pdom (bb);
392 /* Skip the postdominating bb that is also loop exit. */
393 if (!is_non_loop_exit_postdominating (pdom, bb))
394 return NULL;
396 if (dominated_by_p (CDI_DOMINATORS, pdom, bb))
397 return pdom;
399 return NULL;
402 #define MAX_NUM_CHAINS 8
403 #define MAX_CHAIN_LEN 5
404 #define MAX_POSTDOM_CHECK 8
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,
473 num_chains, 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) || post_dom_check >
482 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 typedef struct use_def_pred_info
497 tree pred_lhs;
498 tree pred_rhs;
499 enum tree_code cond_code;
500 bool invert;
501 } pred_info;
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)
562 && EDGE_COUNT (e->src->succs) >= 2)
564 /* Ignore EH edge. Can add assertion
565 on the other edge's flag. */
566 continue;
568 /* Skip if there is essentially one succesor. */
569 if (EDGE_COUNT (e->src->succs) == 2)
571 edge e1;
572 edge_iterator ei1;
573 bool skip = false;
575 FOR_EACH_EDGE (e1, ei1, e->src->succs)
577 if (EDGE_COUNT (e1->dest->succs) == 0)
579 skip = true;
580 break;
583 if (skip)
584 continue;
586 if (gimple_code (cond_stmt) != GIMPLE_COND)
588 has_valid_pred = false;
589 break;
591 one_pred.pred_lhs = gimple_cond_lhs (cond_stmt);
592 one_pred.pred_rhs = gimple_cond_rhs (cond_stmt);
593 one_pred.cond_code = gimple_cond_code (cond_stmt);
594 one_pred.invert = !!(e->flags & EDGE_FALSE_VALUE);
595 t_chain.safe_push (one_pred);
596 has_valid_pred = true;
599 if (!has_valid_pred)
600 break;
601 else
602 preds->safe_push (t_chain);
604 return has_valid_pred;
607 /* Computes all control dependence chains for USE_BB. The control
608 dependence chains are then converted to an array of composite
609 predicates pointed to by PREDS. PHI_BB is the basic block of
610 the phi whose result is used in USE_BB. */
612 static bool
613 find_predicates (pred_chain_union *preds,
614 basic_block phi_bb,
615 basic_block use_bb)
617 size_t num_chains = 0, i;
618 int num_calls = 0;
619 vec<edge> dep_chains[MAX_NUM_CHAINS];
620 auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
621 bool has_valid_pred = false;
622 basic_block cd_root = 0;
624 /* First find the closest bb that is control equivalent to PHI_BB
625 that also dominates USE_BB. */
626 cd_root = phi_bb;
627 while (dominated_by_p (CDI_DOMINATORS, use_bb, cd_root))
629 basic_block ctrl_eq_bb = find_control_equiv_block (cd_root);
630 if (ctrl_eq_bb && dominated_by_p (CDI_DOMINATORS, use_bb, ctrl_eq_bb))
631 cd_root = ctrl_eq_bb;
632 else
633 break;
636 compute_control_dep_chain (cd_root, use_bb, dep_chains, &num_chains,
637 &cur_chain, &num_calls);
639 has_valid_pred
640 = convert_control_dep_chain_into_preds (dep_chains, num_chains, preds);
641 for (i = 0; i < num_chains; i++)
642 dep_chains[i].release ();
643 return has_valid_pred;
646 /* Computes the set of incoming edges of PHI that have non empty
647 definitions of a phi chain. The collection will be done
648 recursively on operands that are defined by phis. CD_ROOT
649 is the control dependence root. *EDGES holds the result, and
650 VISITED_PHIS is a pointer set for detecting cycles. */
652 static void
653 collect_phi_def_edges (gimple phi, basic_block cd_root,
654 vec<edge> *edges,
655 hash_set<gimple> *visited_phis)
657 size_t i, n;
658 edge opnd_edge;
659 tree opnd;
661 if (visited_phis->add (phi))
662 return;
664 n = gimple_phi_num_args (phi);
665 for (i = 0; i < n; i++)
667 opnd_edge = gimple_phi_arg_edge (phi, i);
668 opnd = gimple_phi_arg_def (phi, i);
670 if (TREE_CODE (opnd) != SSA_NAME)
672 if (dump_file && (dump_flags & TDF_DETAILS))
674 fprintf (dump_file, "\n[CHECK] Found def edge %d in ", (int)i);
675 print_gimple_stmt (dump_file, phi, 0, 0);
677 edges->safe_push (opnd_edge);
679 else
681 gimple def = SSA_NAME_DEF_STMT (opnd);
683 if (gimple_code (def) == GIMPLE_PHI
684 && dominated_by_p (CDI_DOMINATORS,
685 gimple_bb (def), cd_root))
686 collect_phi_def_edges (def, cd_root, edges,
687 visited_phis);
688 else if (!uninit_undefined_value_p (opnd))
690 if (dump_file && (dump_flags & TDF_DETAILS))
692 fprintf (dump_file, "\n[CHECK] Found def edge %d in ", (int)i);
693 print_gimple_stmt (dump_file, phi, 0, 0);
695 edges->safe_push (opnd_edge);
701 /* For each use edge of PHI, computes all control dependence chains.
702 The control dependence chains are then converted to an array of
703 composite predicates pointed to by PREDS. */
705 static bool
706 find_def_preds (pred_chain_union *preds, gimple phi)
708 size_t num_chains = 0, i, n;
709 vec<edge> dep_chains[MAX_NUM_CHAINS];
710 auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
711 vec<edge> def_edges = vNULL;
712 bool has_valid_pred = false;
713 basic_block phi_bb, cd_root = 0;
715 phi_bb = gimple_bb (phi);
716 /* First find the closest dominating bb to be
717 the control dependence root */
718 cd_root = find_dom (phi_bb);
719 if (!cd_root)
720 return false;
722 hash_set<gimple> visited_phis;
723 collect_phi_def_edges (phi, cd_root, &def_edges, &visited_phis);
725 n = def_edges.length ();
726 if (n == 0)
727 return false;
729 for (i = 0; i < n; i++)
731 size_t prev_nc, j;
732 int num_calls = 0;
733 edge opnd_edge;
735 opnd_edge = def_edges[i];
736 prev_nc = num_chains;
737 compute_control_dep_chain (cd_root, opnd_edge->src, dep_chains,
738 &num_chains, &cur_chain, &num_calls);
740 /* Now update the newly added chains with
741 the phi operand edge: */
742 if (EDGE_COUNT (opnd_edge->src->succs) > 1)
744 if (prev_nc == num_chains && num_chains < MAX_NUM_CHAINS)
745 dep_chains[num_chains++] = vNULL;
746 for (j = prev_nc; j < num_chains; j++)
747 dep_chains[j].safe_push (opnd_edge);
751 has_valid_pred
752 = convert_control_dep_chain_into_preds (dep_chains, num_chains, preds);
753 for (i = 0; i < num_chains; i++)
754 dep_chains[i].release ();
755 return has_valid_pred;
758 /* Dumps the predicates (PREDS) for USESTMT. */
760 static void
761 dump_predicates (gimple usestmt, pred_chain_union preds,
762 const char* msg)
764 size_t i, j;
765 pred_chain one_pred_chain = vNULL;
766 fprintf (dump_file, msg);
767 print_gimple_stmt (dump_file, usestmt, 0, 0);
768 fprintf (dump_file, "is guarded by :\n\n");
769 size_t num_preds = preds.length ();
770 /* Do some dumping here: */
771 for (i = 0; i < num_preds; i++)
773 size_t np;
775 one_pred_chain = preds[i];
776 np = one_pred_chain.length ();
778 for (j = 0; j < np; j++)
780 pred_info one_pred = one_pred_chain[j];
781 if (one_pred.invert)
782 fprintf (dump_file, " (.NOT.) ");
783 print_generic_expr (dump_file, one_pred.pred_lhs, 0);
784 fprintf (dump_file, " %s ", op_symbol_code (one_pred.cond_code));
785 print_generic_expr (dump_file, one_pred.pred_rhs, 0);
786 if (j < np - 1)
787 fprintf (dump_file, " (.AND.) ");
788 else
789 fprintf (dump_file, "\n");
791 if (i < num_preds - 1)
792 fprintf (dump_file, "(.OR.)\n");
793 else
794 fprintf (dump_file, "\n\n");
798 /* Destroys the predicate set *PREDS. */
800 static void
801 destroy_predicate_vecs (pred_chain_union preds)
803 size_t i;
805 size_t n = preds.length ();
806 for (i = 0; i < n; i++)
807 preds[i].release ();
808 preds.release ();
812 /* Computes the 'normalized' conditional code with operand
813 swapping and condition inversion. */
815 static enum tree_code
816 get_cmp_code (enum tree_code orig_cmp_code,
817 bool swap_cond, bool invert)
819 enum tree_code tc = orig_cmp_code;
821 if (swap_cond)
822 tc = swap_tree_comparison (orig_cmp_code);
823 if (invert)
824 tc = invert_tree_comparison (tc, false);
826 switch (tc)
828 case LT_EXPR:
829 case LE_EXPR:
830 case GT_EXPR:
831 case GE_EXPR:
832 case EQ_EXPR:
833 case NE_EXPR:
834 break;
835 default:
836 return ERROR_MARK;
838 return tc;
841 /* Returns true if VAL falls in the range defined by BOUNDARY and CMPC, i.e.
842 all values in the range satisfies (x CMPC BOUNDARY) == true. */
844 static bool
845 is_value_included_in (tree val, tree boundary, enum tree_code cmpc)
847 bool inverted = false;
848 bool is_unsigned;
849 bool result;
851 /* Only handle integer constant here. */
852 if (TREE_CODE (val) != INTEGER_CST
853 || TREE_CODE (boundary) != INTEGER_CST)
854 return true;
856 is_unsigned = TYPE_UNSIGNED (TREE_TYPE (val));
858 if (cmpc == GE_EXPR || cmpc == GT_EXPR
859 || cmpc == NE_EXPR)
861 cmpc = invert_tree_comparison (cmpc, false);
862 inverted = true;
865 if (is_unsigned)
867 if (cmpc == EQ_EXPR)
868 result = tree_int_cst_equal (val, boundary);
869 else if (cmpc == LT_EXPR)
870 result = tree_int_cst_lt (val, boundary);
871 else
873 gcc_assert (cmpc == LE_EXPR);
874 result = tree_int_cst_le (val, boundary);
877 else
879 if (cmpc == EQ_EXPR)
880 result = tree_int_cst_equal (val, boundary);
881 else if (cmpc == LT_EXPR)
882 result = tree_int_cst_lt (val, boundary);
883 else
885 gcc_assert (cmpc == LE_EXPR);
886 result = (tree_int_cst_equal (val, boundary)
887 || tree_int_cst_lt (val, boundary));
891 if (inverted)
892 result ^= 1;
894 return result;
897 /* Returns true if PRED is common among all the predicate
898 chains (PREDS) (and therefore can be factored out).
899 NUM_PRED_CHAIN is the size of array PREDS. */
901 static bool
902 find_matching_predicate_in_rest_chains (pred_info pred,
903 pred_chain_union preds,
904 size_t num_pred_chains)
906 size_t i, j, n;
908 /* Trival case. */
909 if (num_pred_chains == 1)
910 return true;
912 for (i = 1; i < num_pred_chains; i++)
914 bool found = false;
915 pred_chain one_chain = preds[i];
916 n = one_chain.length ();
917 for (j = 0; j < n; j++)
919 pred_info pred2 = one_chain[j];
920 /* Can relax the condition comparison to not
921 use address comparison. However, the most common
922 case is that multiple control dependent paths share
923 a common path prefix, so address comparison should
924 be ok. */
926 if (operand_equal_p (pred2.pred_lhs, pred.pred_lhs, 0)
927 && operand_equal_p (pred2.pred_rhs, pred.pred_rhs, 0)
928 && pred2.invert == pred.invert)
930 found = true;
931 break;
934 if (!found)
935 return false;
937 return true;
940 /* Forward declaration. */
941 static bool
942 is_use_properly_guarded (gimple use_stmt,
943 basic_block use_bb,
944 gimple phi,
945 unsigned uninit_opnds,
946 hash_set<gimple> *visited_phis);
948 /* Returns true if all uninitialized opnds are pruned. Returns false
949 otherwise. PHI is the phi node with uninitialized operands,
950 UNINIT_OPNDS is the bitmap of the uninitialize operand positions,
951 FLAG_DEF is the statement defining the flag guarding the use of the
952 PHI output, BOUNDARY_CST is the const value used in the predicate
953 associated with the flag, CMP_CODE is the comparison code used in
954 the predicate, VISITED_PHIS is the pointer set of phis visited, and
955 VISITED_FLAG_PHIS is the pointer to the pointer set of flag definitions
956 that are also phis.
958 Example scenario:
960 BB1:
961 flag_1 = phi <0, 1> // (1)
962 var_1 = phi <undef, some_val>
965 BB2:
966 flag_2 = phi <0, flag_1, flag_1> // (2)
967 var_2 = phi <undef, var_1, var_1>
968 if (flag_2 == 1)
969 goto BB3;
971 BB3:
972 use of var_2 // (3)
974 Because some flag arg in (1) is not constant, if we do not look into the
975 flag phis recursively, it is conservatively treated as unknown and var_1
976 is thought to be flowed into use at (3). Since var_1 is potentially uninitialized
977 a false warning will be emitted. Checking recursively into (1), the compiler can
978 find out that only some_val (which is defined) can flow into (3) which is OK.
982 static bool
983 prune_uninit_phi_opnds_in_unrealizable_paths (gimple phi,
984 unsigned uninit_opnds,
985 gimple flag_def,
986 tree boundary_cst,
987 enum tree_code cmp_code,
988 hash_set<gimple> *visited_phis,
989 bitmap *visited_flag_phis)
991 unsigned i;
993 for (i = 0; i < MIN (32, gimple_phi_num_args (flag_def)); i++)
995 tree flag_arg;
997 if (!MASK_TEST_BIT (uninit_opnds, i))
998 continue;
1000 flag_arg = gimple_phi_arg_def (flag_def, i);
1001 if (!is_gimple_constant (flag_arg))
1003 gimple flag_arg_def, phi_arg_def;
1004 tree phi_arg;
1005 unsigned uninit_opnds_arg_phi;
1007 if (TREE_CODE (flag_arg) != SSA_NAME)
1008 return false;
1009 flag_arg_def = SSA_NAME_DEF_STMT (flag_arg);
1010 if (gimple_code (flag_arg_def) != GIMPLE_PHI)
1011 return false;
1013 phi_arg = gimple_phi_arg_def (phi, i);
1014 if (TREE_CODE (phi_arg) != SSA_NAME)
1015 return false;
1017 phi_arg_def = SSA_NAME_DEF_STMT (phi_arg);
1018 if (gimple_code (phi_arg_def) != GIMPLE_PHI)
1019 return false;
1021 if (gimple_bb (phi_arg_def) != gimple_bb (flag_arg_def))
1022 return false;
1024 if (!*visited_flag_phis)
1025 *visited_flag_phis = BITMAP_ALLOC (NULL);
1027 if (bitmap_bit_p (*visited_flag_phis,
1028 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def))))
1029 return false;
1031 bitmap_set_bit (*visited_flag_phis,
1032 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def)));
1034 /* Now recursively prune the uninitialized phi args. */
1035 uninit_opnds_arg_phi = compute_uninit_opnds_pos (phi_arg_def);
1036 if (!prune_uninit_phi_opnds_in_unrealizable_paths
1037 (phi_arg_def, uninit_opnds_arg_phi, flag_arg_def,
1038 boundary_cst, cmp_code, visited_phis, visited_flag_phis))
1039 return false;
1041 bitmap_clear_bit (*visited_flag_phis,
1042 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def)));
1043 continue;
1046 /* Now check if the constant is in the guarded range. */
1047 if (is_value_included_in (flag_arg, boundary_cst, cmp_code))
1049 tree opnd;
1050 gimple opnd_def;
1052 /* Now that we know that this undefined edge is not
1053 pruned. If the operand is defined by another phi,
1054 we can further prune the incoming edges of that
1055 phi by checking the predicates of this operands. */
1057 opnd = gimple_phi_arg_def (phi, i);
1058 opnd_def = SSA_NAME_DEF_STMT (opnd);
1059 if (gimple_code (opnd_def) == GIMPLE_PHI)
1061 edge opnd_edge;
1062 unsigned uninit_opnds2
1063 = compute_uninit_opnds_pos (opnd_def);
1064 gcc_assert (!MASK_EMPTY (uninit_opnds2));
1065 opnd_edge = gimple_phi_arg_edge (phi, i);
1066 if (!is_use_properly_guarded (phi,
1067 opnd_edge->src,
1068 opnd_def,
1069 uninit_opnds2,
1070 visited_phis))
1071 return false;
1073 else
1074 return false;
1078 return true;
1081 /* A helper function that determines if the predicate set
1082 of the use is not overlapping with that of the uninit paths.
1083 The most common senario of guarded use is in Example 1:
1084 Example 1:
1085 if (some_cond)
1087 x = ...;
1088 flag = true;
1091 ... some code ...
1093 if (flag)
1094 use (x);
1096 The real world examples are usually more complicated, but similar
1097 and usually result from inlining:
1099 bool init_func (int * x)
1101 if (some_cond)
1102 return false;
1103 *x = ..
1104 return true;
1107 void foo(..)
1109 int x;
1111 if (!init_func(&x))
1112 return;
1114 .. some_code ...
1115 use (x);
1118 Another possible use scenario is in the following trivial example:
1120 Example 2:
1121 if (n > 0)
1122 x = 1;
1124 if (n > 0)
1126 if (m < 2)
1127 .. = x;
1130 Predicate analysis needs to compute the composite predicate:
1132 1) 'x' use predicate: (n > 0) .AND. (m < 2)
1133 2) 'x' default value (non-def) predicate: .NOT. (n > 0)
1134 (the predicate chain for phi operand defs can be computed
1135 starting from a bb that is control equivalent to the phi's
1136 bb and is dominating the operand def.)
1138 and check overlapping:
1139 (n > 0) .AND. (m < 2) .AND. (.NOT. (n > 0))
1140 <==> false
1142 This implementation provides framework that can handle
1143 scenarios. (Note that many simple cases are handled properly
1144 without the predicate analysis -- this is due to jump threading
1145 transformation which eliminates the merge point thus makes
1146 path sensitive analysis unnecessary.)
1148 NUM_PREDS is the number is the number predicate chains, PREDS is
1149 the array of chains, PHI is the phi node whose incoming (undefined)
1150 paths need to be pruned, and UNINIT_OPNDS is the bitmap holding
1151 uninit operand positions. VISITED_PHIS is the pointer set of phi
1152 stmts being checked. */
1155 static bool
1156 use_pred_not_overlap_with_undef_path_pred (pred_chain_union preds,
1157 gimple phi, unsigned uninit_opnds,
1158 hash_set<gimple> *visited_phis)
1160 unsigned int i, n;
1161 gimple flag_def = 0;
1162 tree boundary_cst = 0;
1163 enum tree_code cmp_code;
1164 bool swap_cond = false;
1165 bool invert = false;
1166 pred_chain the_pred_chain = vNULL;
1167 bitmap visited_flag_phis = NULL;
1168 bool all_pruned = false;
1169 size_t num_preds = preds.length ();
1171 gcc_assert (num_preds > 0);
1172 /* Find within the common prefix of multiple predicate chains
1173 a predicate that is a comparison of a flag variable against
1174 a constant. */
1175 the_pred_chain = preds[0];
1176 n = the_pred_chain.length ();
1177 for (i = 0; i < n; i++)
1179 tree cond_lhs, cond_rhs, flag = 0;
1181 pred_info the_pred = the_pred_chain[i];
1183 invert = the_pred.invert;
1184 cond_lhs = the_pred.pred_lhs;
1185 cond_rhs = the_pred.pred_rhs;
1186 cmp_code = the_pred.cond_code;
1188 if (cond_lhs != NULL_TREE && TREE_CODE (cond_lhs) == SSA_NAME
1189 && cond_rhs != NULL_TREE && is_gimple_constant (cond_rhs))
1191 boundary_cst = cond_rhs;
1192 flag = cond_lhs;
1194 else if (cond_rhs != NULL_TREE && TREE_CODE (cond_rhs) == SSA_NAME
1195 && cond_lhs != NULL_TREE && is_gimple_constant (cond_lhs))
1197 boundary_cst = cond_lhs;
1198 flag = cond_rhs;
1199 swap_cond = true;
1202 if (!flag)
1203 continue;
1205 flag_def = SSA_NAME_DEF_STMT (flag);
1207 if (!flag_def)
1208 continue;
1210 if ((gimple_code (flag_def) == GIMPLE_PHI)
1211 && (gimple_bb (flag_def) == gimple_bb (phi))
1212 && find_matching_predicate_in_rest_chains (the_pred, preds,
1213 num_preds))
1214 break;
1216 flag_def = 0;
1219 if (!flag_def)
1220 return false;
1222 /* Now check all the uninit incoming edge has a constant flag value
1223 that is in conflict with the use guard/predicate. */
1224 cmp_code = get_cmp_code (cmp_code, swap_cond, invert);
1226 if (cmp_code == ERROR_MARK)
1227 return false;
1229 all_pruned = prune_uninit_phi_opnds_in_unrealizable_paths (phi,
1230 uninit_opnds,
1231 flag_def,
1232 boundary_cst,
1233 cmp_code,
1234 visited_phis,
1235 &visited_flag_phis);
1237 if (visited_flag_phis)
1238 BITMAP_FREE (visited_flag_phis);
1240 return all_pruned;
1243 /* The helper function returns true if two predicates X1 and X2
1244 are equivalent. It assumes the expressions have already
1245 properly re-associated. */
1247 static inline bool
1248 pred_equal_p (pred_info x1, pred_info x2)
1250 enum tree_code c1, c2;
1251 if (!operand_equal_p (x1.pred_lhs, x2.pred_lhs, 0)
1252 || !operand_equal_p (x1.pred_rhs, x2.pred_rhs, 0))
1253 return false;
1255 c1 = x1.cond_code;
1256 if (x1.invert != x2.invert)
1257 c2 = invert_tree_comparison (x2.cond_code, false);
1258 else
1259 c2 = x2.cond_code;
1261 return c1 == c2;
1264 /* Returns true if the predication is testing !=. */
1266 static inline bool
1267 is_neq_relop_p (pred_info pred)
1270 return (pred.cond_code == NE_EXPR && !pred.invert)
1271 || (pred.cond_code == EQ_EXPR && pred.invert);
1274 /* Returns true if pred is of the form X != 0. */
1276 static inline bool
1277 is_neq_zero_form_p (pred_info pred)
1279 if (!is_neq_relop_p (pred) || !integer_zerop (pred.pred_rhs)
1280 || TREE_CODE (pred.pred_lhs) != SSA_NAME)
1281 return false;
1282 return true;
1285 /* The helper function returns true if two predicates X1
1286 is equivalent to X2 != 0. */
1288 static inline bool
1289 pred_expr_equal_p (pred_info x1, tree x2)
1291 if (!is_neq_zero_form_p (x1))
1292 return false;
1294 return operand_equal_p (x1.pred_lhs, x2, 0);
1297 /* Returns true of the domain of single predicate expression
1298 EXPR1 is a subset of that of EXPR2. Returns false if it
1299 can not be proved. */
1301 static bool
1302 is_pred_expr_subset_of (pred_info expr1, pred_info expr2)
1304 enum tree_code code1, code2;
1306 if (pred_equal_p (expr1, expr2))
1307 return true;
1309 if ((TREE_CODE (expr1.pred_rhs) != INTEGER_CST)
1310 || (TREE_CODE (expr2.pred_rhs) != INTEGER_CST))
1311 return false;
1313 if (!operand_equal_p (expr1.pred_lhs, expr2.pred_lhs, 0))
1314 return false;
1316 code1 = expr1.cond_code;
1317 if (expr1.invert)
1318 code1 = invert_tree_comparison (code1, false);
1319 code2 = expr2.cond_code;
1320 if (expr2.invert)
1321 code2 = invert_tree_comparison (code2, false);
1323 if (code1 != code2 && code2 != NE_EXPR)
1324 return false;
1326 if (is_value_included_in (expr1.pred_rhs, expr2.pred_rhs, code2))
1327 return true;
1329 return false;
1332 /* Returns true if the domain of PRED1 is a subset
1333 of that of PRED2. Returns false if it can not be proved so. */
1335 static bool
1336 is_pred_chain_subset_of (pred_chain pred1,
1337 pred_chain pred2)
1339 size_t np1, np2, i1, i2;
1341 np1 = pred1.length ();
1342 np2 = pred2.length ();
1344 for (i2 = 0; i2 < np2; i2++)
1346 bool found = false;
1347 pred_info info2 = pred2[i2];
1348 for (i1 = 0; i1 < np1; i1++)
1350 pred_info info1 = pred1[i1];
1351 if (is_pred_expr_subset_of (info1, info2))
1353 found = true;
1354 break;
1357 if (!found)
1358 return false;
1360 return true;
1363 /* Returns true if the domain defined by
1364 one pred chain ONE_PRED is a subset of the domain
1365 of *PREDS. It returns false if ONE_PRED's domain is
1366 not a subset of any of the sub-domains of PREDS
1367 (corresponding to each individual chains in it), even
1368 though it may be still be a subset of whole domain
1369 of PREDS which is the union (ORed) of all its subdomains.
1370 In other words, the result is conservative. */
1372 static bool
1373 is_included_in (pred_chain one_pred, pred_chain_union preds)
1375 size_t i;
1376 size_t n = preds.length ();
1378 for (i = 0; i < n; i++)
1380 if (is_pred_chain_subset_of (one_pred, preds[i]))
1381 return true;
1384 return false;
1387 /* Compares two predicate sets PREDS1 and PREDS2 and returns
1388 true if the domain defined by PREDS1 is a superset
1389 of PREDS2's domain. N1 and N2 are array sizes of PREDS1 and
1390 PREDS2 respectively. The implementation chooses not to build
1391 generic trees (and relying on the folding capability of the
1392 compiler), but instead performs brute force comparison of
1393 individual predicate chains (won't be a compile time problem
1394 as the chains are pretty short). When the function returns
1395 false, it does not necessarily mean *PREDS1 is not a superset
1396 of *PREDS2, but mean it may not be so since the analysis can
1397 not prove it. In such cases, false warnings may still be
1398 emitted. */
1400 static bool
1401 is_superset_of (pred_chain_union preds1, pred_chain_union preds2)
1403 size_t i, n2;
1404 pred_chain one_pred_chain = vNULL;
1406 n2 = preds2.length ();
1408 for (i = 0; i < n2; i++)
1410 one_pred_chain = preds2[i];
1411 if (!is_included_in (one_pred_chain, preds1))
1412 return false;
1415 return true;
1418 /* Returns true if TC is AND or OR. */
1420 static inline bool
1421 is_and_or_or_p (enum tree_code tc, tree type)
1423 return (tc == BIT_IOR_EXPR
1424 || (tc == BIT_AND_EXPR
1425 && (type == 0 || TREE_CODE (type) == BOOLEAN_TYPE)));
1428 /* Returns true if X1 is the negate of X2. */
1430 static inline bool
1431 pred_neg_p (pred_info x1, pred_info x2)
1433 enum tree_code c1, c2;
1434 if (!operand_equal_p (x1.pred_lhs, x2.pred_lhs, 0)
1435 || !operand_equal_p (x1.pred_rhs, x2.pred_rhs, 0))
1436 return false;
1438 c1 = x1.cond_code;
1439 if (x1.invert == x2.invert)
1440 c2 = invert_tree_comparison (x2.cond_code, false);
1441 else
1442 c2 = x2.cond_code;
1444 return c1 == c2;
1447 /* 1) ((x IOR y) != 0) AND (x != 0) is equivalent to (x != 0);
1448 2) (X AND Y) OR (!X AND Y) is equivalent to Y;
1449 3) X OR (!X AND Y) is equivalent to (X OR Y);
1450 4) ((x IAND y) != 0) || (x != 0 AND y != 0)) is equivalent to
1451 (x != 0 AND y != 0)
1452 5) (X AND Y) OR (!X AND Z) OR (!Y AND Z) is equivalent to
1453 (X AND Y) OR Z
1455 PREDS is the predicate chains, and N is the number of chains. */
1457 /* Helper function to implement rule 1 above. ONE_CHAIN is
1458 the AND predication to be simplified. */
1460 static void
1461 simplify_pred (pred_chain *one_chain)
1463 size_t i, j, n;
1464 bool simplified = false;
1465 pred_chain s_chain = vNULL;
1467 n = one_chain->length ();
1469 for (i = 0; i < n; i++)
1471 pred_info *a_pred = &(*one_chain)[i];
1473 if (!a_pred->pred_lhs)
1474 continue;
1475 if (!is_neq_zero_form_p (*a_pred))
1476 continue;
1478 gimple def_stmt = SSA_NAME_DEF_STMT (a_pred->pred_lhs);
1479 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1480 continue;
1481 if (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR)
1483 for (j = 0; j < n; j++)
1485 pred_info *b_pred = &(*one_chain)[j];
1487 if (!b_pred->pred_lhs)
1488 continue;
1489 if (!is_neq_zero_form_p (*b_pred))
1490 continue;
1492 if (pred_expr_equal_p (*b_pred, gimple_assign_rhs1 (def_stmt))
1493 || pred_expr_equal_p (*b_pred, gimple_assign_rhs2 (def_stmt)))
1495 /* Mark a_pred for removal. */
1496 a_pred->pred_lhs = NULL;
1497 a_pred->pred_rhs = NULL;
1498 simplified = true;
1499 break;
1505 if (!simplified)
1506 return;
1508 for (i = 0; i < n; i++)
1510 pred_info *a_pred = &(*one_chain)[i];
1511 if (!a_pred->pred_lhs)
1512 continue;
1513 s_chain.safe_push (*a_pred);
1516 one_chain->release ();
1517 *one_chain = s_chain;
1520 /* The helper function implements the rule 2 for the
1521 OR predicate PREDS.
1523 2) (X AND Y) OR (!X AND Y) is equivalent to Y. */
1525 static bool
1526 simplify_preds_2 (pred_chain_union *preds)
1528 size_t i, j, n;
1529 bool simplified = false;
1530 pred_chain_union s_preds = vNULL;
1532 /* (X AND Y) OR (!X AND Y) is equivalent to Y.
1533 (X AND Y) OR (X AND !Y) is equivalent to X. */
1535 n = preds->length ();
1536 for (i = 0; i < n; i++)
1538 pred_info x, y;
1539 pred_chain *a_chain = &(*preds)[i];
1541 if (a_chain->length () != 2)
1542 continue;
1544 x = (*a_chain)[0];
1545 y = (*a_chain)[1];
1547 for (j = 0; j < n; j++)
1549 pred_chain *b_chain;
1550 pred_info x2, y2;
1552 if (j == i)
1553 continue;
1555 b_chain = &(*preds)[j];
1556 if (b_chain->length () != 2)
1557 continue;
1559 x2 = (*b_chain)[0];
1560 y2 = (*b_chain)[1];
1562 if (pred_equal_p (x, x2) && pred_neg_p (y, y2))
1564 /* Kill a_chain. */
1565 a_chain->release ();
1566 b_chain->release ();
1567 b_chain->safe_push (x);
1568 simplified = true;
1569 break;
1571 if (pred_neg_p (x, x2) && pred_equal_p (y, y2))
1573 /* Kill a_chain. */
1574 a_chain->release ();
1575 b_chain->release ();
1576 b_chain->safe_push (y);
1577 simplified = true;
1578 break;
1582 /* Now clean up the chain. */
1583 if (simplified)
1585 for (i = 0; i < n; i++)
1587 if ((*preds)[i].is_empty ())
1588 continue;
1589 s_preds.safe_push ((*preds)[i]);
1591 preds->release ();
1592 (*preds) = s_preds;
1593 s_preds = vNULL;
1596 return simplified;
1599 /* The helper function implements the rule 2 for the
1600 OR predicate PREDS.
1602 3) x OR (!x AND y) is equivalent to x OR y. */
1604 static bool
1605 simplify_preds_3 (pred_chain_union *preds)
1607 size_t i, j, n;
1608 bool simplified = false;
1610 /* Now iteratively simplify X OR (!X AND Z ..)
1611 into X OR (Z ...). */
1613 n = preds->length ();
1614 if (n < 2)
1615 return false;
1617 for (i = 0; i < n; i++)
1619 pred_info x;
1620 pred_chain *a_chain = &(*preds)[i];
1622 if (a_chain->length () != 1)
1623 continue;
1625 x = (*a_chain)[0];
1627 for (j = 0; j < n; j++)
1629 pred_chain *b_chain;
1630 pred_info x2;
1631 size_t k;
1633 if (j == i)
1634 continue;
1636 b_chain = &(*preds)[j];
1637 if (b_chain->length () < 2)
1638 continue;
1640 for (k = 0; k < b_chain->length (); k++)
1642 x2 = (*b_chain)[k];
1643 if (pred_neg_p (x, x2))
1645 b_chain->unordered_remove (k);
1646 simplified = true;
1647 break;
1652 return simplified;
1655 /* The helper function implements the rule 4 for the
1656 OR predicate PREDS.
1658 2) ((x AND y) != 0) OR (x != 0 AND y != 0) is equivalent to
1659 (x != 0 ANd y != 0). */
1661 static bool
1662 simplify_preds_4 (pred_chain_union *preds)
1664 size_t i, j, n;
1665 bool simplified = false;
1666 pred_chain_union s_preds = vNULL;
1667 gimple def_stmt;
1669 n = preds->length ();
1670 for (i = 0; i < n; i++)
1672 pred_info z;
1673 pred_chain *a_chain = &(*preds)[i];
1675 if (a_chain->length () != 1)
1676 continue;
1678 z = (*a_chain)[0];
1680 if (!is_neq_zero_form_p (z))
1681 continue;
1683 def_stmt = SSA_NAME_DEF_STMT (z.pred_lhs);
1684 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1685 continue;
1687 if (gimple_assign_rhs_code (def_stmt) != BIT_AND_EXPR)
1688 continue;
1690 for (j = 0; j < n; j++)
1692 pred_chain *b_chain;
1693 pred_info x2, y2;
1695 if (j == i)
1696 continue;
1698 b_chain = &(*preds)[j];
1699 if (b_chain->length () != 2)
1700 continue;
1702 x2 = (*b_chain)[0];
1703 y2 = (*b_chain)[1];
1704 if (!is_neq_zero_form_p (x2)
1705 || !is_neq_zero_form_p (y2))
1706 continue;
1708 if ((pred_expr_equal_p (x2, gimple_assign_rhs1 (def_stmt))
1709 && pred_expr_equal_p (y2, gimple_assign_rhs2 (def_stmt)))
1710 || (pred_expr_equal_p (x2, gimple_assign_rhs2 (def_stmt))
1711 && pred_expr_equal_p (y2, gimple_assign_rhs1 (def_stmt))))
1713 /* Kill a_chain. */
1714 a_chain->release ();
1715 simplified = true;
1716 break;
1720 /* Now clean up the chain. */
1721 if (simplified)
1723 for (i = 0; i < n; i++)
1725 if ((*preds)[i].is_empty ())
1726 continue;
1727 s_preds.safe_push ((*preds)[i]);
1729 preds->release ();
1730 (*preds) = s_preds;
1731 s_preds = vNULL;
1734 return simplified;
1738 /* This function simplifies predicates in PREDS. */
1740 static void
1741 simplify_preds (pred_chain_union *preds, gimple use_or_def, bool is_use)
1743 size_t i, n;
1744 bool changed = false;
1746 if (dump_file && dump_flags & TDF_DETAILS)
1748 fprintf (dump_file, "[BEFORE SIMPLICATION -- ");
1749 dump_predicates (use_or_def, *preds, is_use ? "[USE]:\n" : "[DEF]:\n");
1752 for (i = 0; i < preds->length (); i++)
1753 simplify_pred (&(*preds)[i]);
1755 n = preds->length ();
1756 if (n < 2)
1757 return;
1761 changed = false;
1762 if (simplify_preds_2 (preds))
1763 changed = true;
1765 /* Now iteratively simplify X OR (!X AND Z ..)
1766 into X OR (Z ...). */
1767 if (simplify_preds_3 (preds))
1768 changed = true;
1770 if (simplify_preds_4 (preds))
1771 changed = true;
1773 } while (changed);
1775 return;
1778 /* This is a helper function which attempts to normalize predicate chains
1779 by following UD chains. It basically builds up a big tree of either IOR
1780 operations or AND operations, and convert the IOR tree into a
1781 pred_chain_union or BIT_AND tree into a pred_chain.
1782 Example:
1784 _3 = _2 RELOP1 _1;
1785 _6 = _5 RELOP2 _4;
1786 _9 = _8 RELOP3 _7;
1787 _10 = _3 | _6;
1788 _12 = _9 | _0;
1789 _t = _10 | _12;
1791 then _t != 0 will be normalized into a pred_chain_union
1793 (_2 RELOP1 _1) OR (_5 RELOP2 _4) OR (_8 RELOP3 _7) OR (_0 != 0)
1795 Similarly given,
1797 _3 = _2 RELOP1 _1;
1798 _6 = _5 RELOP2 _4;
1799 _9 = _8 RELOP3 _7;
1800 _10 = _3 & _6;
1801 _12 = _9 & _0;
1803 then _t != 0 will be normalized into a pred_chain:
1804 (_2 RELOP1 _1) AND (_5 RELOP2 _4) AND (_8 RELOP3 _7) AND (_0 != 0)
1808 /* This is a helper function that stores a PRED into NORM_PREDS. */
1810 inline static void
1811 push_pred (pred_chain_union *norm_preds, pred_info pred)
1813 pred_chain pred_chain = vNULL;
1814 pred_chain.safe_push (pred);
1815 norm_preds->safe_push (pred_chain);
1818 /* A helper function that creates a predicate of the form
1819 OP != 0 and push it WORK_LIST. */
1821 inline static void
1822 push_to_worklist (tree op, vec<pred_info, va_heap, vl_ptr> *work_list,
1823 hash_set<tree> *mark_set)
1825 if (mark_set->contains (op))
1826 return;
1827 mark_set->add (op);
1829 pred_info arg_pred;
1830 arg_pred.pred_lhs = op;
1831 arg_pred.pred_rhs = integer_zero_node;
1832 arg_pred.cond_code = NE_EXPR;
1833 arg_pred.invert = false;
1834 work_list->safe_push (arg_pred);
1837 /* A helper that generates a pred_info from a gimple assignment
1838 CMP_ASSIGN with comparison rhs. */
1840 static pred_info
1841 get_pred_info_from_cmp (gimple cmp_assign)
1843 pred_info n_pred;
1844 n_pred.pred_lhs = gimple_assign_rhs1 (cmp_assign);
1845 n_pred.pred_rhs = gimple_assign_rhs2 (cmp_assign);
1846 n_pred.cond_code = gimple_assign_rhs_code (cmp_assign);
1847 n_pred.invert = false;
1848 return n_pred;
1851 /* Returns true if the PHI is a degenerated phi with
1852 all args with the same value (relop). In that case, *PRED
1853 will be updated to that value. */
1855 static bool
1856 is_degenerated_phi (gimple phi, pred_info *pred_p)
1858 int i, n;
1859 tree op0;
1860 gimple def0;
1861 pred_info pred0;
1863 n = gimple_phi_num_args (phi);
1864 op0 = gimple_phi_arg_def (phi, 0);
1866 if (TREE_CODE (op0) != SSA_NAME)
1867 return false;
1869 def0 = SSA_NAME_DEF_STMT (op0);
1870 if (gimple_code (def0) != GIMPLE_ASSIGN)
1871 return false;
1872 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def0))
1873 != tcc_comparison)
1874 return false;
1875 pred0 = get_pred_info_from_cmp (def0);
1877 for (i = 1; i < n; ++i)
1879 gimple def;
1880 pred_info pred;
1881 tree op = gimple_phi_arg_def (phi, i);
1883 if (TREE_CODE (op) != SSA_NAME)
1884 return false;
1886 def = SSA_NAME_DEF_STMT (op);
1887 if (gimple_code (def) != GIMPLE_ASSIGN)
1888 return false;
1889 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def))
1890 != tcc_comparison)
1891 return false;
1892 pred = get_pred_info_from_cmp (def);
1893 if (!pred_equal_p (pred, pred0))
1894 return false;
1897 *pred_p = pred0;
1898 return true;
1901 /* Normalize one predicate PRED
1902 1) if PRED can no longer be normlized, put it into NORM_PREDS.
1903 2) otherwise if PRED is of the form x != 0, follow x's definition
1904 and put normalized predicates into WORK_LIST. */
1906 static void
1907 normalize_one_pred_1 (pred_chain_union *norm_preds,
1908 pred_chain *norm_chain,
1909 pred_info pred,
1910 enum tree_code and_or_code,
1911 vec<pred_info, va_heap, vl_ptr> *work_list,
1912 hash_set<tree> *mark_set)
1914 if (!is_neq_zero_form_p (pred))
1916 if (and_or_code == BIT_IOR_EXPR)
1917 push_pred (norm_preds, pred);
1918 else
1919 norm_chain->safe_push (pred);
1920 return;
1923 gimple def_stmt = SSA_NAME_DEF_STMT (pred.pred_lhs);
1925 if (gimple_code (def_stmt) == GIMPLE_PHI
1926 && is_degenerated_phi (def_stmt, &pred))
1927 work_list->safe_push (pred);
1928 else if (gimple_code (def_stmt) == GIMPLE_PHI
1929 && and_or_code == BIT_IOR_EXPR)
1931 int i, n;
1932 n = gimple_phi_num_args (def_stmt);
1934 /* If we see non zero constant, we should punt. The predicate
1935 * should be one guarding the phi edge. */
1936 for (i = 0; i < n; ++i)
1938 tree op = gimple_phi_arg_def (def_stmt, i);
1939 if (TREE_CODE (op) == INTEGER_CST && !integer_zerop (op))
1941 push_pred (norm_preds, pred);
1942 return;
1946 for (i = 0; i < n; ++i)
1948 tree op = gimple_phi_arg_def (def_stmt, i);
1949 if (integer_zerop (op))
1950 continue;
1952 push_to_worklist (op, work_list, mark_set);
1955 else if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1957 if (and_or_code == BIT_IOR_EXPR)
1958 push_pred (norm_preds, pred);
1959 else
1960 norm_chain->safe_push (pred);
1962 else if (gimple_assign_rhs_code (def_stmt) == and_or_code)
1964 push_to_worklist (gimple_assign_rhs1 (def_stmt), work_list, mark_set);
1965 push_to_worklist (gimple_assign_rhs2 (def_stmt), work_list, mark_set);
1967 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt))
1968 == tcc_comparison)
1970 pred_info n_pred = get_pred_info_from_cmp (def_stmt);
1971 if (and_or_code == BIT_IOR_EXPR)
1972 push_pred (norm_preds, n_pred);
1973 else
1974 norm_chain->safe_push (n_pred);
1976 else
1978 if (and_or_code == BIT_IOR_EXPR)
1979 push_pred (norm_preds, pred);
1980 else
1981 norm_chain->safe_push (pred);
1985 /* Normalize PRED and store the normalized predicates into NORM_PREDS. */
1987 static void
1988 normalize_one_pred (pred_chain_union *norm_preds,
1989 pred_info pred)
1991 vec<pred_info, va_heap, vl_ptr> work_list = vNULL;
1992 enum tree_code and_or_code = ERROR_MARK;
1993 pred_chain norm_chain = vNULL;
1995 if (!is_neq_zero_form_p (pred))
1997 push_pred (norm_preds, pred);
1998 return;
2001 gimple def_stmt = SSA_NAME_DEF_STMT (pred.pred_lhs);
2002 if (gimple_code (def_stmt) == GIMPLE_ASSIGN)
2003 and_or_code = gimple_assign_rhs_code (def_stmt);
2004 if (and_or_code != BIT_IOR_EXPR
2005 && and_or_code != BIT_AND_EXPR)
2007 if (TREE_CODE_CLASS (and_or_code)
2008 == tcc_comparison)
2010 pred_info n_pred = get_pred_info_from_cmp (def_stmt);
2011 push_pred (norm_preds, n_pred);
2013 else
2014 push_pred (norm_preds, pred);
2015 return;
2018 work_list.safe_push (pred);
2019 hash_set<tree> mark_set;
2021 while (!work_list.is_empty ())
2023 pred_info a_pred = work_list.pop ();
2024 normalize_one_pred_1 (norm_preds, &norm_chain, a_pred,
2025 and_or_code, &work_list, &mark_set);
2027 if (and_or_code == BIT_AND_EXPR)
2028 norm_preds->safe_push (norm_chain);
2030 work_list.release ();
2033 static void
2034 normalize_one_pred_chain (pred_chain_union *norm_preds,
2035 pred_chain one_chain)
2037 vec<pred_info, va_heap, vl_ptr> work_list = vNULL;
2038 hash_set<tree> mark_set;
2039 pred_chain norm_chain = vNULL;
2040 size_t i;
2042 for (i = 0; i < one_chain.length (); i++)
2044 work_list.safe_push (one_chain[i]);
2045 mark_set.add (one_chain[i].pred_lhs);
2048 while (!work_list.is_empty ())
2050 pred_info a_pred = work_list.pop ();
2051 normalize_one_pred_1 (0, &norm_chain, a_pred,
2052 BIT_AND_EXPR, &work_list, &mark_set);
2055 norm_preds->safe_push (norm_chain);
2056 work_list.release ();
2059 /* Normalize predicate chains PREDS and returns the normalized one. */
2061 static pred_chain_union
2062 normalize_preds (pred_chain_union preds, gimple use_or_def, bool is_use)
2064 pred_chain_union norm_preds = vNULL;
2065 size_t n = preds.length ();
2066 size_t i;
2068 if (dump_file && dump_flags & TDF_DETAILS)
2070 fprintf (dump_file, "[BEFORE NORMALIZATION --");
2071 dump_predicates (use_or_def, preds, is_use ? "[USE]:\n" : "[DEF]:\n");
2074 for (i = 0; i < n; i++)
2076 if (preds[i].length () != 1)
2077 normalize_one_pred_chain (&norm_preds, preds[i]);
2078 else
2080 normalize_one_pred (&norm_preds, preds[i][0]);
2081 preds[i].release ();
2085 if (dump_file)
2087 fprintf (dump_file, "[AFTER NORMALIZATION -- ");
2088 dump_predicates (use_or_def, norm_preds, is_use ? "[USE]:\n" : "[DEF]:\n");
2091 preds.release ();
2092 return norm_preds;
2096 /* Computes the predicates that guard the use and checks
2097 if the incoming paths that have empty (or possibly
2098 empty) definition can be pruned/filtered. The function returns
2099 true if it can be determined that the use of PHI's def in
2100 USE_STMT is guarded with a predicate set not overlapping with
2101 predicate sets of all runtime paths that do not have a definition.
2102 Returns false if it is not or it can not be determined. USE_BB is
2103 the bb of the use (for phi operand use, the bb is not the bb of
2104 the phi stmt, but the src bb of the operand edge). UNINIT_OPNDS
2105 is a bit vector. If an operand of PHI is uninitialized, the
2106 corresponding bit in the vector is 1. VISIED_PHIS is a pointer
2107 set of phis being visted. */
2109 static bool
2110 is_use_properly_guarded (gimple use_stmt,
2111 basic_block use_bb,
2112 gimple phi,
2113 unsigned uninit_opnds,
2114 hash_set<gimple> *visited_phis)
2116 basic_block phi_bb;
2117 pred_chain_union preds = vNULL;
2118 pred_chain_union def_preds = vNULL;
2119 bool has_valid_preds = false;
2120 bool is_properly_guarded = false;
2122 if (visited_phis->add (phi))
2123 return false;
2125 phi_bb = gimple_bb (phi);
2127 if (is_non_loop_exit_postdominating (use_bb, phi_bb))
2128 return false;
2130 has_valid_preds = find_predicates (&preds, phi_bb, use_bb);
2132 if (!has_valid_preds)
2134 destroy_predicate_vecs (preds);
2135 return false;
2138 /* Try to prune the dead incoming phi edges. */
2139 is_properly_guarded
2140 = use_pred_not_overlap_with_undef_path_pred (preds, phi, uninit_opnds,
2141 visited_phis);
2143 if (is_properly_guarded)
2145 destroy_predicate_vecs (preds);
2146 return true;
2149 has_valid_preds = find_def_preds (&def_preds, phi);
2151 if (!has_valid_preds)
2153 destroy_predicate_vecs (preds);
2154 destroy_predicate_vecs (def_preds);
2155 return false;
2158 simplify_preds (&preds, use_stmt, true);
2159 preds = normalize_preds (preds, use_stmt, true);
2161 simplify_preds (&def_preds, phi, false);
2162 def_preds = normalize_preds (def_preds, phi, false);
2164 is_properly_guarded = is_superset_of (def_preds, preds);
2166 destroy_predicate_vecs (preds);
2167 destroy_predicate_vecs (def_preds);
2168 return is_properly_guarded;
2171 /* Searches through all uses of a potentially
2172 uninitialized variable defined by PHI and returns a use
2173 statement if the use is not properly guarded. It returns
2174 NULL if all uses are guarded. UNINIT_OPNDS is a bitvector
2175 holding the position(s) of uninit PHI operands. WORKLIST
2176 is the vector of candidate phis that may be updated by this
2177 function. ADDED_TO_WORKLIST is the pointer set tracking
2178 if the new phi is already in the worklist. */
2180 static gimple
2181 find_uninit_use (gimple phi, unsigned uninit_opnds,
2182 vec<gimple> *worklist,
2183 hash_set<gimple> *added_to_worklist)
2185 tree phi_result;
2186 use_operand_p use_p;
2187 gimple use_stmt;
2188 imm_use_iterator iter;
2190 phi_result = gimple_phi_result (phi);
2192 FOR_EACH_IMM_USE_FAST (use_p, iter, phi_result)
2194 basic_block use_bb;
2196 use_stmt = USE_STMT (use_p);
2197 if (is_gimple_debug (use_stmt))
2198 continue;
2200 if (gimple_code (use_stmt) == GIMPLE_PHI)
2201 use_bb = gimple_phi_arg_edge (use_stmt,
2202 PHI_ARG_INDEX_FROM_USE (use_p))->src;
2203 else
2204 use_bb = gimple_bb (use_stmt);
2206 hash_set<gimple> visited_phis;
2207 if (is_use_properly_guarded (use_stmt, use_bb, phi, uninit_opnds,
2208 &visited_phis))
2209 continue;
2211 if (dump_file && (dump_flags & TDF_DETAILS))
2213 fprintf (dump_file, "[CHECK]: Found unguarded use: ");
2214 print_gimple_stmt (dump_file, use_stmt, 0, 0);
2216 /* Found one real use, return. */
2217 if (gimple_code (use_stmt) != GIMPLE_PHI)
2218 return use_stmt;
2220 /* Found a phi use that is not guarded,
2221 add the phi to the worklist. */
2222 if (!added_to_worklist->add (use_stmt))
2224 if (dump_file && (dump_flags & TDF_DETAILS))
2226 fprintf (dump_file, "[WORKLIST]: Update worklist with phi: ");
2227 print_gimple_stmt (dump_file, use_stmt, 0, 0);
2230 worklist->safe_push (use_stmt);
2231 possibly_undefined_names->add (phi_result);
2235 return NULL;
2238 /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions
2239 and gives warning if there exists a runtime path from the entry to a
2240 use of the PHI def that does not contain a definition. In other words,
2241 the warning is on the real use. The more dead paths that can be pruned
2242 by the compiler, the fewer false positives the warning is. WORKLIST
2243 is a vector of candidate phis to be examined. ADDED_TO_WORKLIST is
2244 a pointer set tracking if the new phi is added to the worklist or not. */
2246 static void
2247 warn_uninitialized_phi (gimple phi, vec<gimple> *worklist,
2248 hash_set<gimple> *added_to_worklist)
2250 unsigned uninit_opnds;
2251 gimple uninit_use_stmt = 0;
2252 tree uninit_op;
2253 int phiarg_index;
2254 location_t loc;
2256 /* Don't look at virtual operands. */
2257 if (virtual_operand_p (gimple_phi_result (phi)))
2258 return;
2260 uninit_opnds = compute_uninit_opnds_pos (phi);
2262 if (MASK_EMPTY (uninit_opnds))
2263 return;
2265 if (dump_file && (dump_flags & TDF_DETAILS))
2267 fprintf (dump_file, "[CHECK]: examining phi: ");
2268 print_gimple_stmt (dump_file, phi, 0, 0);
2271 /* Now check if we have any use of the value without proper guard. */
2272 uninit_use_stmt = find_uninit_use (phi, uninit_opnds,
2273 worklist, added_to_worklist);
2275 /* All uses are properly guarded. */
2276 if (!uninit_use_stmt)
2277 return;
2279 phiarg_index = MASK_FIRST_SET_BIT (uninit_opnds);
2280 uninit_op = gimple_phi_arg_def (phi, phiarg_index);
2281 if (SSA_NAME_VAR (uninit_op) == NULL_TREE)
2282 return;
2283 if (gimple_phi_arg_has_location (phi, phiarg_index))
2284 loc = gimple_phi_arg_location (phi, phiarg_index);
2285 else
2286 loc = UNKNOWN_LOCATION;
2287 warn_uninit (OPT_Wmaybe_uninitialized, uninit_op, SSA_NAME_VAR (uninit_op),
2288 SSA_NAME_VAR (uninit_op),
2289 "%qD may be used uninitialized in this function",
2290 uninit_use_stmt, loc);
2294 static bool
2295 gate_warn_uninitialized (void)
2297 return warn_uninitialized || warn_maybe_uninitialized;
2300 namespace {
2302 const pass_data pass_data_late_warn_uninitialized =
2304 GIMPLE_PASS, /* type */
2305 "uninit", /* name */
2306 OPTGROUP_NONE, /* optinfo_flags */
2307 TV_NONE, /* tv_id */
2308 PROP_ssa, /* properties_required */
2309 0, /* properties_provided */
2310 0, /* properties_destroyed */
2311 0, /* todo_flags_start */
2312 0, /* todo_flags_finish */
2315 class pass_late_warn_uninitialized : public gimple_opt_pass
2317 public:
2318 pass_late_warn_uninitialized (gcc::context *ctxt)
2319 : gimple_opt_pass (pass_data_late_warn_uninitialized, ctxt)
2322 /* opt_pass methods: */
2323 opt_pass * clone () { return new pass_late_warn_uninitialized (m_ctxt); }
2324 virtual bool gate (function *) { return gate_warn_uninitialized (); }
2325 virtual unsigned int execute (function *);
2327 }; // class pass_late_warn_uninitialized
2329 unsigned int
2330 pass_late_warn_uninitialized::execute (function *fun)
2332 basic_block bb;
2333 gimple_stmt_iterator gsi;
2334 vec<gimple> worklist = vNULL;
2336 calculate_dominance_info (CDI_DOMINATORS);
2337 calculate_dominance_info (CDI_POST_DOMINATORS);
2338 /* Re-do the plain uninitialized variable check, as optimization may have
2339 straightened control flow. Do this first so that we don't accidentally
2340 get a "may be" warning when we'd have seen an "is" warning later. */
2341 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/1);
2343 timevar_push (TV_TREE_UNINIT);
2345 possibly_undefined_names = new hash_set<tree>;
2346 hash_set<gimple> added_to_worklist;
2348 /* Initialize worklist */
2349 FOR_EACH_BB_FN (bb, fun)
2350 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2352 gimple phi = gsi_stmt (gsi);
2353 size_t n, i;
2355 n = gimple_phi_num_args (phi);
2357 /* Don't look at virtual operands. */
2358 if (virtual_operand_p (gimple_phi_result (phi)))
2359 continue;
2361 for (i = 0; i < n; ++i)
2363 tree op = gimple_phi_arg_def (phi, i);
2364 if (TREE_CODE (op) == SSA_NAME
2365 && uninit_undefined_value_p (op))
2367 worklist.safe_push (phi);
2368 added_to_worklist.add (phi);
2369 if (dump_file && (dump_flags & TDF_DETAILS))
2371 fprintf (dump_file, "[WORKLIST]: add to initial list: ");
2372 print_gimple_stmt (dump_file, phi, 0, 0);
2374 break;
2379 while (worklist.length () != 0)
2381 gimple cur_phi = 0;
2382 cur_phi = worklist.pop ();
2383 warn_uninitialized_phi (cur_phi, &worklist, &added_to_worklist);
2386 worklist.release ();
2387 delete possibly_undefined_names;
2388 possibly_undefined_names = NULL;
2389 free_dominance_info (CDI_POST_DOMINATORS);
2390 timevar_pop (TV_TREE_UNINIT);
2391 return 0;
2394 } // anon namespace
2396 gimple_opt_pass *
2397 make_pass_late_warn_uninitialized (gcc::context *ctxt)
2399 return new pass_late_warn_uninitialized (ctxt);
2403 static unsigned int
2404 execute_early_warn_uninitialized (void)
2406 /* Currently, this pass runs always but
2407 execute_late_warn_uninitialized only runs with optimization. With
2408 optimization we want to warn about possible uninitialized as late
2409 as possible, thus don't do it here. However, without
2410 optimization we need to warn here about "may be uninitialized". */
2411 calculate_dominance_info (CDI_POST_DOMINATORS);
2413 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/!optimize);
2415 /* Post-dominator information can not be reliably updated. Free it
2416 after the use. */
2418 free_dominance_info (CDI_POST_DOMINATORS);
2419 return 0;
2423 namespace {
2425 const pass_data pass_data_early_warn_uninitialized =
2427 GIMPLE_PASS, /* type */
2428 "*early_warn_uninitialized", /* name */
2429 OPTGROUP_NONE, /* optinfo_flags */
2430 TV_TREE_UNINIT, /* tv_id */
2431 PROP_ssa, /* properties_required */
2432 0, /* properties_provided */
2433 0, /* properties_destroyed */
2434 0, /* todo_flags_start */
2435 0, /* todo_flags_finish */
2438 class pass_early_warn_uninitialized : public gimple_opt_pass
2440 public:
2441 pass_early_warn_uninitialized (gcc::context *ctxt)
2442 : gimple_opt_pass (pass_data_early_warn_uninitialized, ctxt)
2445 /* opt_pass methods: */
2446 virtual bool gate (function *) { return gate_warn_uninitialized (); }
2447 virtual unsigned int execute (function *)
2449 return execute_early_warn_uninitialized ();
2452 }; // class pass_early_warn_uninitialized
2454 } // anon namespace
2456 gimple_opt_pass *
2457 make_pass_early_warn_uninitialized (gcc::context *ctxt)
2459 return new pass_early_warn_uninitialized (ctxt);