PR c/79855: add full stop to store merging param descriptions
[official-gcc.git] / gcc / tree-ssa-uninit.c
blob1805c674ea497b31123b4e27aa2708369ad0c7cb
1 /* Predicate aware uninitialized variable warning.
2 Copyright (C) 2001-2017 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 /* Max PHI args we can handle in pass. */
48 const unsigned max_phi_args = 32;
50 /* Pointer set of potentially undefined ssa names, i.e.,
51 ssa names that are defined by phi with operands that
52 are not defined or potentially undefined. */
53 static hash_set<tree> *possibly_undefined_names = 0;
55 /* Bit mask handling macros. */
56 #define MASK_SET_BIT(mask, pos) mask |= (1 << pos)
57 #define MASK_TEST_BIT(mask, pos) (mask & (1 << pos))
58 #define MASK_EMPTY(mask) (mask == 0)
60 /* Returns the first bit position (starting from LSB)
61 in mask that is non zero. Returns -1 if the mask is empty. */
62 static int
63 get_mask_first_set_bit (unsigned mask)
65 int pos = 0;
66 if (mask == 0)
67 return -1;
69 while ((mask & (1 << pos)) == 0)
70 pos++;
72 return pos;
74 #define MASK_FIRST_SET_BIT(mask) get_mask_first_set_bit (mask)
76 /* Return true if T, an SSA_NAME, has an undefined value. */
77 static bool
78 has_undefined_value_p (tree t)
80 return (ssa_undefined_value_p (t)
81 || (possibly_undefined_names
82 && possibly_undefined_names->contains (t)));
85 /* Like has_undefined_value_p, but don't return true if TREE_NO_WARNING
86 is set on SSA_NAME_VAR. */
88 static inline bool
89 uninit_undefined_value_p (tree t)
91 if (!has_undefined_value_p (t))
92 return false;
93 if (SSA_NAME_VAR (t) && TREE_NO_WARNING (SSA_NAME_VAR (t)))
94 return false;
95 return true;
98 /* Emit warnings for uninitialized variables. This is done in two passes.
100 The first pass notices real uses of SSA names with undefined values.
101 Such uses are unconditionally uninitialized, and we can be certain that
102 such a use is a mistake. This pass is run before most optimizations,
103 so that we catch as many as we can.
105 The second pass follows PHI nodes to find uses that are potentially
106 uninitialized. In this case we can't necessarily prove that the use
107 is really uninitialized. This pass is run after most optimizations,
108 so that we thread as many jumps and possible, and delete as much dead
109 code as possible, in order to reduce false positives. We also look
110 again for plain uninitialized variables, since optimization may have
111 changed conditionally uninitialized to unconditionally uninitialized. */
113 /* Emit a warning for EXPR based on variable VAR at the point in the
114 program T, an SSA_NAME, is used being uninitialized. The exact
115 warning text is in MSGID and DATA is the gimple stmt with info about
116 the location in source code. When DATA is a GIMPLE_PHI, PHIARG_IDX
117 gives which argument of the phi node to take the location from. WC
118 is the warning code. */
120 static void
121 warn_uninit (enum opt_code wc, tree t, tree expr, tree var,
122 const char *gmsgid, void *data, location_t phiarg_loc)
124 gimple *context = (gimple *) data;
125 location_t location, cfun_loc;
126 expanded_location xloc, floc;
128 /* Ignore COMPLEX_EXPR as initializing only a part of a complex
129 turns in a COMPLEX_EXPR with the not initialized part being
130 set to its previous (undefined) value. */
131 if (is_gimple_assign (context)
132 && gimple_assign_rhs_code (context) == COMPLEX_EXPR)
133 return;
134 if (!has_undefined_value_p (t))
135 return;
137 /* Anonymous SSA_NAMEs shouldn't be uninitialized, but ssa_undefined_value_p
138 can return true if the def stmt of anonymous SSA_NAME is COMPLEX_EXPR
139 created for conversion from scalar to complex. Use the underlying var of
140 the COMPLEX_EXPRs real part in that case. See PR71581. */
141 if (expr == NULL_TREE
142 && var == NULL_TREE
143 && SSA_NAME_VAR (t) == NULL_TREE
144 && is_gimple_assign (SSA_NAME_DEF_STMT (t))
145 && gimple_assign_rhs_code (SSA_NAME_DEF_STMT (t)) == COMPLEX_EXPR)
147 tree v = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (t));
148 if (TREE_CODE (v) == SSA_NAME
149 && has_undefined_value_p (v)
150 && zerop (gimple_assign_rhs2 (SSA_NAME_DEF_STMT (t))))
152 expr = SSA_NAME_VAR (v);
153 var = expr;
157 if (expr == NULL_TREE)
158 return;
160 /* TREE_NO_WARNING either means we already warned, or the front end
161 wishes to suppress the warning. */
162 if ((context
163 && (gimple_no_warning_p (context)
164 || (gimple_assign_single_p (context)
165 && TREE_NO_WARNING (gimple_assign_rhs1 (context)))))
166 || TREE_NO_WARNING (expr))
167 return;
169 if (context != NULL && gimple_has_location (context))
170 location = gimple_location (context);
171 else if (phiarg_loc != UNKNOWN_LOCATION)
172 location = phiarg_loc;
173 else
174 location = DECL_SOURCE_LOCATION (var);
175 location = linemap_resolve_location (line_table, location,
176 LRK_SPELLING_LOCATION, NULL);
177 cfun_loc = DECL_SOURCE_LOCATION (cfun->decl);
178 xloc = expand_location (location);
179 floc = expand_location (cfun_loc);
180 if (warning_at (location, wc, gmsgid, expr))
182 TREE_NO_WARNING (expr) = 1;
184 if (location == DECL_SOURCE_LOCATION (var))
185 return;
186 if (xloc.file != floc.file
187 || linemap_location_before_p (line_table, location, cfun_loc)
188 || linemap_location_before_p (line_table, cfun->function_end_locus,
189 location))
190 inform (DECL_SOURCE_LOCATION (var), "%qD was declared here", var);
194 struct check_defs_data
196 /* If we found any may-defs besides must-def clobbers. */
197 bool found_may_defs;
200 /* Callback for walk_aliased_vdefs. */
202 static bool
203 check_defs (ao_ref *ref, tree vdef, void *data_)
205 check_defs_data *data = (check_defs_data *)data_;
206 gimple *def_stmt = SSA_NAME_DEF_STMT (vdef);
207 /* If this is a clobber then if it is not a kill walk past it. */
208 if (gimple_clobber_p (def_stmt))
210 if (stmt_kills_ref_p (def_stmt, ref))
211 return true;
212 return false;
214 /* Found a may-def on this path. */
215 data->found_may_defs = true;
216 return true;
219 static unsigned int
220 warn_uninitialized_vars (bool warn_possibly_uninitialized)
222 gimple_stmt_iterator gsi;
223 basic_block bb;
224 unsigned int vdef_cnt = 0;
225 unsigned int oracle_cnt = 0;
226 unsigned limit = 0;
228 FOR_EACH_BB_FN (bb, cfun)
230 basic_block succ = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
231 bool always_executed = dominated_by_p (CDI_POST_DOMINATORS, succ, bb);
232 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
234 gimple *stmt = gsi_stmt (gsi);
235 use_operand_p use_p;
236 ssa_op_iter op_iter;
237 tree use;
239 if (is_gimple_debug (stmt))
240 continue;
242 /* We only do data flow with SSA_NAMEs, so that's all we
243 can warn about. */
244 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, op_iter, SSA_OP_USE)
246 /* BIT_INSERT_EXPR first operand should not be considered
247 a use for the purpose of uninit warnings. */
248 if (gassign *ass = dyn_cast <gassign *> (stmt))
250 if (gimple_assign_rhs_code (ass) == BIT_INSERT_EXPR
251 && use_p->use == gimple_assign_rhs1_ptr (ass))
252 continue;
254 use = USE_FROM_PTR (use_p);
255 if (always_executed)
256 warn_uninit (OPT_Wuninitialized, use, SSA_NAME_VAR (use),
257 SSA_NAME_VAR (use),
258 "%qD is used uninitialized in this function", stmt,
259 UNKNOWN_LOCATION);
260 else if (warn_possibly_uninitialized)
261 warn_uninit (OPT_Wmaybe_uninitialized, use, SSA_NAME_VAR (use),
262 SSA_NAME_VAR (use),
263 "%qD may be used uninitialized in this function",
264 stmt, UNKNOWN_LOCATION);
267 /* For limiting the alias walk below we count all
268 vdefs in the function. */
269 if (gimple_vdef (stmt))
270 vdef_cnt++;
272 if (gimple_assign_load_p (stmt)
273 && gimple_has_location (stmt))
275 tree rhs = gimple_assign_rhs1 (stmt);
276 if (TREE_NO_WARNING (rhs))
277 continue;
279 ao_ref ref;
280 ao_ref_init (&ref, rhs);
282 /* Do not warn if it can be initialized outside this function. */
283 tree base = ao_ref_base (&ref);
284 if (!VAR_P (base)
285 || DECL_HARD_REGISTER (base)
286 || is_global_var (base)
287 || TREE_NO_WARNING (base))
288 continue;
290 /* Limit the walking to a constant number of stmts after
291 we overcommit quadratic behavior for small functions
292 and O(n) behavior. */
293 if (oracle_cnt > 128 * 128
294 && oracle_cnt > vdef_cnt * 2)
295 limit = 32;
296 check_defs_data data;
297 data.found_may_defs = false;
298 use = gimple_vuse (stmt);
299 int res = walk_aliased_vdefs (&ref, use,
300 check_defs, &data, NULL,
301 NULL, limit);
302 if (res == -1)
304 oracle_cnt += limit;
305 continue;
307 oracle_cnt += res;
308 if (data.found_may_defs)
309 continue;
311 /* We didn't find any may-defs so on all paths either
312 reached function entry or a killing clobber. */
313 location_t location
314 = linemap_resolve_location (line_table, gimple_location (stmt),
315 LRK_SPELLING_LOCATION, NULL);
316 if (always_executed)
318 if (warning_at (location, OPT_Wuninitialized,
319 "%qE is used uninitialized in this function",
320 rhs))
321 /* ??? This is only effective for decls as in
322 gcc.dg/uninit-B-O0.c. Avoid doing this for
323 maybe-uninit uses as it may hide important
324 locations. */
325 TREE_NO_WARNING (rhs) = 1;
327 else if (warn_possibly_uninitialized)
328 warning_at (location, OPT_Wmaybe_uninitialized,
329 "%qE may be used uninitialized in this function",
330 rhs);
335 return 0;
338 /* Checks if the operand OPND of PHI is defined by
339 another phi with one operand defined by this PHI,
340 but the rest operands are all defined. If yes,
341 returns true to skip this operand as being
342 redundant. Can be enhanced to be more general. */
344 static bool
345 can_skip_redundant_opnd (tree opnd, gimple *phi)
347 gimple *op_def;
348 tree phi_def;
349 int i, n;
351 phi_def = gimple_phi_result (phi);
352 op_def = SSA_NAME_DEF_STMT (opnd);
353 if (gimple_code (op_def) != GIMPLE_PHI)
354 return false;
355 n = gimple_phi_num_args (op_def);
356 for (i = 0; i < n; ++i)
358 tree op = gimple_phi_arg_def (op_def, i);
359 if (TREE_CODE (op) != SSA_NAME)
360 continue;
361 if (op != phi_def && uninit_undefined_value_p (op))
362 return false;
365 return true;
368 /* Returns a bit mask holding the positions of arguments in PHI
369 that have empty (or possibly empty) definitions. */
371 static unsigned
372 compute_uninit_opnds_pos (gphi *phi)
374 size_t i, n;
375 unsigned uninit_opnds = 0;
377 n = gimple_phi_num_args (phi);
378 /* Bail out for phi with too many args. */
379 if (n > max_phi_args)
380 return 0;
382 for (i = 0; i < n; ++i)
384 tree op = gimple_phi_arg_def (phi, i);
385 if (TREE_CODE (op) == SSA_NAME
386 && uninit_undefined_value_p (op)
387 && !can_skip_redundant_opnd (op, phi))
389 if (cfun->has_nonlocal_label || cfun->calls_setjmp)
391 /* Ignore SSA_NAMEs that appear on abnormal edges
392 somewhere. */
393 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
394 continue;
396 MASK_SET_BIT (uninit_opnds, i);
399 return uninit_opnds;
402 /* Find the immediate postdominator PDOM of the specified
403 basic block BLOCK. */
405 static inline basic_block
406 find_pdom (basic_block block)
408 if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
409 return EXIT_BLOCK_PTR_FOR_FN (cfun);
410 else
412 basic_block bb = get_immediate_dominator (CDI_POST_DOMINATORS, block);
413 if (!bb)
414 return EXIT_BLOCK_PTR_FOR_FN (cfun);
415 return bb;
419 /* Find the immediate DOM of the specified basic block BLOCK. */
421 static inline basic_block
422 find_dom (basic_block block)
424 if (block == ENTRY_BLOCK_PTR_FOR_FN (cfun))
425 return ENTRY_BLOCK_PTR_FOR_FN (cfun);
426 else
428 basic_block bb = get_immediate_dominator (CDI_DOMINATORS, block);
429 if (!bb)
430 return ENTRY_BLOCK_PTR_FOR_FN (cfun);
431 return bb;
435 /* Returns true if BB1 is postdominating BB2 and BB1 is
436 not a loop exit bb. The loop exit bb check is simple and does
437 not cover all cases. */
439 static bool
440 is_non_loop_exit_postdominating (basic_block bb1, basic_block bb2)
442 if (!dominated_by_p (CDI_POST_DOMINATORS, bb2, bb1))
443 return false;
445 if (single_pred_p (bb1) && !single_succ_p (bb2))
446 return false;
448 return true;
451 /* Find the closest postdominator of a specified BB, which is control
452 equivalent to BB. */
454 static inline basic_block
455 find_control_equiv_block (basic_block bb)
457 basic_block pdom;
459 pdom = find_pdom (bb);
461 /* Skip the postdominating bb that is also loop exit. */
462 if (!is_non_loop_exit_postdominating (pdom, bb))
463 return NULL;
465 if (dominated_by_p (CDI_DOMINATORS, pdom, bb))
466 return pdom;
468 return NULL;
471 #define MAX_NUM_CHAINS 8
472 #define MAX_CHAIN_LEN 5
473 #define MAX_POSTDOM_CHECK 8
474 #define MAX_SWITCH_CASES 40
476 /* Computes the control dependence chains (paths of edges)
477 for DEP_BB up to the dominating basic block BB (the head node of a
478 chain should be dominated by it). CD_CHAINS is pointer to an
479 array holding the result chains. CUR_CD_CHAIN is the current
480 chain being computed. *NUM_CHAINS is total number of chains. The
481 function returns true if the information is successfully computed,
482 return false if there is no control dependence or not computed. */
484 static bool
485 compute_control_dep_chain (basic_block bb, basic_block dep_bb,
486 vec<edge> *cd_chains,
487 size_t *num_chains,
488 vec<edge> *cur_cd_chain,
489 int *num_calls)
491 edge_iterator ei;
492 edge e;
493 size_t i;
494 bool found_cd_chain = false;
495 size_t cur_chain_len = 0;
497 if (EDGE_COUNT (bb->succs) < 2)
498 return false;
500 if (*num_calls > PARAM_VALUE (PARAM_UNINIT_CONTROL_DEP_ATTEMPTS))
501 return false;
502 ++*num_calls;
504 /* Could use a set instead. */
505 cur_chain_len = cur_cd_chain->length ();
506 if (cur_chain_len > MAX_CHAIN_LEN)
507 return false;
509 for (i = 0; i < cur_chain_len; i++)
511 edge e = (*cur_cd_chain)[i];
512 /* Cycle detected. */
513 if (e->src == bb)
514 return false;
517 FOR_EACH_EDGE (e, ei, bb->succs)
519 basic_block cd_bb;
520 int post_dom_check = 0;
521 if (e->flags & (EDGE_FAKE | EDGE_ABNORMAL))
522 continue;
524 cd_bb = e->dest;
525 cur_cd_chain->safe_push (e);
526 while (!is_non_loop_exit_postdominating (cd_bb, bb))
528 if (cd_bb == dep_bb)
530 /* Found a direct control dependence. */
531 if (*num_chains < MAX_NUM_CHAINS)
533 cd_chains[*num_chains] = cur_cd_chain->copy ();
534 (*num_chains)++;
536 found_cd_chain = true;
537 /* Check path from next edge. */
538 break;
541 /* Now check if DEP_BB is indirectly control dependent on BB. */
542 if (compute_control_dep_chain (cd_bb, dep_bb, cd_chains, num_chains,
543 cur_cd_chain, num_calls))
545 found_cd_chain = true;
546 break;
549 cd_bb = find_pdom (cd_bb);
550 post_dom_check++;
551 if (cd_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
552 || post_dom_check > MAX_POSTDOM_CHECK)
553 break;
555 cur_cd_chain->pop ();
556 gcc_assert (cur_cd_chain->length () == cur_chain_len);
558 gcc_assert (cur_cd_chain->length () == cur_chain_len);
560 return found_cd_chain;
563 /* The type to represent a simple predicate. */
565 struct pred_info
567 tree pred_lhs;
568 tree pred_rhs;
569 enum tree_code cond_code;
570 bool invert;
573 /* The type to represent a sequence of predicates grouped
574 with .AND. operation. */
576 typedef vec<pred_info, va_heap, vl_ptr> pred_chain;
578 /* The type to represent a sequence of pred_chains grouped
579 with .OR. operation. */
581 typedef vec<pred_chain, va_heap, vl_ptr> pred_chain_union;
583 /* Converts the chains of control dependence edges into a set of
584 predicates. A control dependence chain is represented by a vector
585 edges. DEP_CHAINS points to an array of dependence chains.
586 NUM_CHAINS is the size of the chain array. One edge in a dependence
587 chain is mapped to predicate expression represented by pred_info
588 type. One dependence chain is converted to a composite predicate that
589 is the result of AND operation of pred_info mapped to each edge.
590 A composite predicate is presented by a vector of pred_info. On
591 return, *PREDS points to the resulting array of composite predicates.
592 *NUM_PREDS is the number of composite predictes. */
594 static bool
595 convert_control_dep_chain_into_preds (vec<edge> *dep_chains,
596 size_t num_chains,
597 pred_chain_union *preds)
599 bool has_valid_pred = false;
600 size_t i, j;
601 if (num_chains == 0 || num_chains >= MAX_NUM_CHAINS)
602 return false;
604 /* Now convert the control dep chain into a set
605 of predicates. */
606 preds->reserve (num_chains);
608 for (i = 0; i < num_chains; i++)
610 vec<edge> one_cd_chain = dep_chains[i];
612 has_valid_pred = false;
613 pred_chain t_chain = vNULL;
614 for (j = 0; j < one_cd_chain.length (); j++)
616 gimple *cond_stmt;
617 gimple_stmt_iterator gsi;
618 basic_block guard_bb;
619 pred_info one_pred;
620 edge e;
622 e = one_cd_chain[j];
623 guard_bb = e->src;
624 gsi = gsi_last_bb (guard_bb);
625 if (gsi_end_p (gsi))
627 has_valid_pred = false;
628 break;
630 cond_stmt = gsi_stmt (gsi);
631 if (is_gimple_call (cond_stmt) && EDGE_COUNT (e->src->succs) >= 2)
632 /* Ignore EH edge. Can add assertion on the other edge's flag. */
633 continue;
634 /* Skip if there is essentially one succesor. */
635 if (EDGE_COUNT (e->src->succs) == 2)
637 edge e1;
638 edge_iterator ei1;
639 bool skip = false;
641 FOR_EACH_EDGE (e1, ei1, e->src->succs)
643 if (EDGE_COUNT (e1->dest->succs) == 0)
645 skip = true;
646 break;
649 if (skip)
650 continue;
652 if (gimple_code (cond_stmt) == GIMPLE_COND)
654 one_pred.pred_lhs = gimple_cond_lhs (cond_stmt);
655 one_pred.pred_rhs = gimple_cond_rhs (cond_stmt);
656 one_pred.cond_code = gimple_cond_code (cond_stmt);
657 one_pred.invert = !!(e->flags & EDGE_FALSE_VALUE);
658 t_chain.safe_push (one_pred);
659 has_valid_pred = true;
661 else if (gswitch *gs = dyn_cast<gswitch *> (cond_stmt))
663 /* Avoid quadratic behavior. */
664 if (gimple_switch_num_labels (gs) > MAX_SWITCH_CASES)
666 has_valid_pred = false;
667 break;
669 /* Find the case label. */
670 tree l = NULL_TREE;
671 unsigned idx;
672 for (idx = 0; idx < gimple_switch_num_labels (gs); ++idx)
674 tree tl = gimple_switch_label (gs, idx);
675 if (e->dest == label_to_block (CASE_LABEL (tl)))
677 if (!l)
678 l = tl;
679 else
681 l = NULL_TREE;
682 break;
686 /* If more than one label reaches this block or the case
687 label doesn't have a single value (like the default one)
688 fail. */
689 if (!l
690 || !CASE_LOW (l)
691 || (CASE_HIGH (l)
692 && !operand_equal_p (CASE_LOW (l), CASE_HIGH (l), 0)))
694 has_valid_pred = false;
695 break;
697 one_pred.pred_lhs = gimple_switch_index (gs);
698 one_pred.pred_rhs = CASE_LOW (l);
699 one_pred.cond_code = EQ_EXPR;
700 one_pred.invert = false;
701 t_chain.safe_push (one_pred);
702 has_valid_pred = true;
704 else
706 has_valid_pred = false;
707 break;
711 if (!has_valid_pred)
712 break;
713 else
714 preds->safe_push (t_chain);
716 return has_valid_pred;
719 /* Computes all control dependence chains for USE_BB. The control
720 dependence chains are then converted to an array of composite
721 predicates pointed to by PREDS. PHI_BB is the basic block of
722 the phi whose result is used in USE_BB. */
724 static bool
725 find_predicates (pred_chain_union *preds,
726 basic_block phi_bb,
727 basic_block use_bb)
729 size_t num_chains = 0, i;
730 int num_calls = 0;
731 vec<edge> dep_chains[MAX_NUM_CHAINS];
732 auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
733 bool has_valid_pred = false;
734 basic_block cd_root = 0;
736 /* First find the closest bb that is control equivalent to PHI_BB
737 that also dominates USE_BB. */
738 cd_root = phi_bb;
739 while (dominated_by_p (CDI_DOMINATORS, use_bb, cd_root))
741 basic_block ctrl_eq_bb = find_control_equiv_block (cd_root);
742 if (ctrl_eq_bb && dominated_by_p (CDI_DOMINATORS, use_bb, ctrl_eq_bb))
743 cd_root = ctrl_eq_bb;
744 else
745 break;
748 compute_control_dep_chain (cd_root, use_bb, dep_chains, &num_chains,
749 &cur_chain, &num_calls);
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 /* Computes the set of incoming edges of PHI that have non empty
759 definitions of a phi chain. The collection will be done
760 recursively on operands that are defined by phis. CD_ROOT
761 is the control dependence root. *EDGES holds the result, and
762 VISITED_PHIS is a pointer set for detecting cycles. */
764 static void
765 collect_phi_def_edges (gphi *phi, basic_block cd_root,
766 auto_vec<edge> *edges,
767 hash_set<gimple *> *visited_phis)
769 size_t i, n;
770 edge opnd_edge;
771 tree opnd;
773 if (visited_phis->add (phi))
774 return;
776 n = gimple_phi_num_args (phi);
777 for (i = 0; i < n; i++)
779 opnd_edge = gimple_phi_arg_edge (phi, i);
780 opnd = gimple_phi_arg_def (phi, i);
782 if (TREE_CODE (opnd) != SSA_NAME)
784 if (dump_file && (dump_flags & TDF_DETAILS))
786 fprintf (dump_file, "\n[CHECK] Found def edge %d in ", (int) i);
787 print_gimple_stmt (dump_file, phi, 0, 0);
789 edges->safe_push (opnd_edge);
791 else
793 gimple *def = SSA_NAME_DEF_STMT (opnd);
795 if (gimple_code (def) == GIMPLE_PHI
796 && dominated_by_p (CDI_DOMINATORS, gimple_bb (def), cd_root))
797 collect_phi_def_edges (as_a<gphi *> (def), cd_root, edges,
798 visited_phis);
799 else if (!uninit_undefined_value_p (opnd))
801 if (dump_file && (dump_flags & TDF_DETAILS))
803 fprintf (dump_file, "\n[CHECK] Found def edge %d in ",
804 (int) i);
805 print_gimple_stmt (dump_file, phi, 0, 0);
807 edges->safe_push (opnd_edge);
813 /* For each use edge of PHI, computes all control dependence chains.
814 The control dependence chains are then converted to an array of
815 composite predicates pointed to by PREDS. */
817 static bool
818 find_def_preds (pred_chain_union *preds, gphi *phi)
820 size_t num_chains = 0, i, n;
821 vec<edge> dep_chains[MAX_NUM_CHAINS];
822 auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
823 auto_vec<edge> def_edges;
824 bool has_valid_pred = false;
825 basic_block phi_bb, cd_root = 0;
827 phi_bb = gimple_bb (phi);
828 /* First find the closest dominating bb to be
829 the control dependence root. */
830 cd_root = find_dom (phi_bb);
831 if (!cd_root)
832 return false;
834 hash_set<gimple *> visited_phis;
835 collect_phi_def_edges (phi, cd_root, &def_edges, &visited_phis);
837 n = def_edges.length ();
838 if (n == 0)
839 return false;
841 for (i = 0; i < n; i++)
843 size_t prev_nc, j;
844 int num_calls = 0;
845 edge opnd_edge;
847 opnd_edge = def_edges[i];
848 prev_nc = num_chains;
849 compute_control_dep_chain (cd_root, opnd_edge->src, dep_chains,
850 &num_chains, &cur_chain, &num_calls);
852 /* Now update the newly added chains with
853 the phi operand edge: */
854 if (EDGE_COUNT (opnd_edge->src->succs) > 1)
856 if (prev_nc == num_chains && num_chains < MAX_NUM_CHAINS)
857 dep_chains[num_chains++] = vNULL;
858 for (j = prev_nc; j < num_chains; j++)
859 dep_chains[j].safe_push (opnd_edge);
863 has_valid_pred
864 = convert_control_dep_chain_into_preds (dep_chains, num_chains, preds);
865 for (i = 0; i < num_chains; i++)
866 dep_chains[i].release ();
867 return has_valid_pred;
870 /* Dumps the predicates (PREDS) for USESTMT. */
872 static void
873 dump_predicates (gimple *usestmt, pred_chain_union preds, const char *msg)
875 size_t i, j;
876 pred_chain one_pred_chain = vNULL;
877 fprintf (dump_file, "%s", msg);
878 print_gimple_stmt (dump_file, usestmt, 0, 0);
879 fprintf (dump_file, "is guarded by :\n\n");
880 size_t num_preds = preds.length ();
881 /* Do some dumping here: */
882 for (i = 0; i < num_preds; i++)
884 size_t np;
886 one_pred_chain = preds[i];
887 np = one_pred_chain.length ();
889 for (j = 0; j < np; j++)
891 pred_info one_pred = one_pred_chain[j];
892 if (one_pred.invert)
893 fprintf (dump_file, " (.NOT.) ");
894 print_generic_expr (dump_file, one_pred.pred_lhs, 0);
895 fprintf (dump_file, " %s ", op_symbol_code (one_pred.cond_code));
896 print_generic_expr (dump_file, one_pred.pred_rhs, 0);
897 if (j < np - 1)
898 fprintf (dump_file, " (.AND.) ");
899 else
900 fprintf (dump_file, "\n");
902 if (i < num_preds - 1)
903 fprintf (dump_file, "(.OR.)\n");
904 else
905 fprintf (dump_file, "\n\n");
909 /* Destroys the predicate set *PREDS. */
911 static void
912 destroy_predicate_vecs (pred_chain_union *preds)
914 size_t i;
916 size_t n = preds->length ();
917 for (i = 0; i < n; i++)
918 (*preds)[i].release ();
919 preds->release ();
922 /* Computes the 'normalized' conditional code with operand
923 swapping and condition inversion. */
925 static enum tree_code
926 get_cmp_code (enum tree_code orig_cmp_code, bool swap_cond, bool invert)
928 enum tree_code tc = orig_cmp_code;
930 if (swap_cond)
931 tc = swap_tree_comparison (orig_cmp_code);
932 if (invert)
933 tc = invert_tree_comparison (tc, false);
935 switch (tc)
937 case LT_EXPR:
938 case LE_EXPR:
939 case GT_EXPR:
940 case GE_EXPR:
941 case EQ_EXPR:
942 case NE_EXPR:
943 break;
944 default:
945 return ERROR_MARK;
947 return tc;
950 /* Returns true if VAL falls in the range defined by BOUNDARY and CMPC, i.e.
951 all values in the range satisfies (x CMPC BOUNDARY) == true. */
953 static bool
954 is_value_included_in (tree val, tree boundary, enum tree_code cmpc)
956 bool inverted = false;
957 bool is_unsigned;
958 bool result;
960 /* Only handle integer constant here. */
961 if (TREE_CODE (val) != INTEGER_CST || TREE_CODE (boundary) != INTEGER_CST)
962 return true;
964 is_unsigned = TYPE_UNSIGNED (TREE_TYPE (val));
966 if (cmpc == GE_EXPR || cmpc == GT_EXPR || cmpc == NE_EXPR)
968 cmpc = invert_tree_comparison (cmpc, false);
969 inverted = true;
972 if (is_unsigned)
974 if (cmpc == EQ_EXPR)
975 result = tree_int_cst_equal (val, boundary);
976 else if (cmpc == LT_EXPR)
977 result = tree_int_cst_lt (val, boundary);
978 else
980 gcc_assert (cmpc == LE_EXPR);
981 result = tree_int_cst_le (val, boundary);
984 else
986 if (cmpc == EQ_EXPR)
987 result = tree_int_cst_equal (val, boundary);
988 else if (cmpc == LT_EXPR)
989 result = tree_int_cst_lt (val, boundary);
990 else
992 gcc_assert (cmpc == LE_EXPR);
993 result = (tree_int_cst_equal (val, boundary)
994 || tree_int_cst_lt (val, boundary));
998 if (inverted)
999 result ^= 1;
1001 return result;
1004 /* Returns true if PRED is common among all the predicate
1005 chains (PREDS) (and therefore can be factored out).
1006 NUM_PRED_CHAIN is the size of array PREDS. */
1008 static bool
1009 find_matching_predicate_in_rest_chains (pred_info pred,
1010 pred_chain_union preds,
1011 size_t num_pred_chains)
1013 size_t i, j, n;
1015 /* Trival case. */
1016 if (num_pred_chains == 1)
1017 return true;
1019 for (i = 1; i < num_pred_chains; i++)
1021 bool found = false;
1022 pred_chain one_chain = preds[i];
1023 n = one_chain.length ();
1024 for (j = 0; j < n; j++)
1026 pred_info pred2 = one_chain[j];
1027 /* Can relax the condition comparison to not
1028 use address comparison. However, the most common
1029 case is that multiple control dependent paths share
1030 a common path prefix, so address comparison should
1031 be ok. */
1033 if (operand_equal_p (pred2.pred_lhs, pred.pred_lhs, 0)
1034 && operand_equal_p (pred2.pred_rhs, pred.pred_rhs, 0)
1035 && pred2.invert == pred.invert)
1037 found = true;
1038 break;
1041 if (!found)
1042 return false;
1044 return true;
1047 /* Forward declaration. */
1048 static bool is_use_properly_guarded (gimple *use_stmt,
1049 basic_block use_bb,
1050 gphi *phi,
1051 unsigned uninit_opnds,
1052 pred_chain_union *def_preds,
1053 hash_set<gphi *> *visited_phis);
1055 /* Returns true if all uninitialized opnds are pruned. Returns false
1056 otherwise. PHI is the phi node with uninitialized operands,
1057 UNINIT_OPNDS is the bitmap of the uninitialize operand positions,
1058 FLAG_DEF is the statement defining the flag guarding the use of the
1059 PHI output, BOUNDARY_CST is the const value used in the predicate
1060 associated with the flag, CMP_CODE is the comparison code used in
1061 the predicate, VISITED_PHIS is the pointer set of phis visited, and
1062 VISITED_FLAG_PHIS is the pointer to the pointer set of flag definitions
1063 that are also phis.
1065 Example scenario:
1067 BB1:
1068 flag_1 = phi <0, 1> // (1)
1069 var_1 = phi <undef, some_val>
1072 BB2:
1073 flag_2 = phi <0, flag_1, flag_1> // (2)
1074 var_2 = phi <undef, var_1, var_1>
1075 if (flag_2 == 1)
1076 goto BB3;
1078 BB3:
1079 use of var_2 // (3)
1081 Because some flag arg in (1) is not constant, if we do not look into the
1082 flag phis recursively, it is conservatively treated as unknown and var_1
1083 is thought to be flowed into use at (3). Since var_1 is potentially
1084 uninitialized a false warning will be emitted.
1085 Checking recursively into (1), the compiler can find out that only some_val
1086 (which is defined) can flow into (3) which is OK. */
1088 static bool
1089 prune_uninit_phi_opnds (gphi *phi, unsigned uninit_opnds, gphi *flag_def,
1090 tree boundary_cst, enum tree_code cmp_code,
1091 hash_set<gphi *> *visited_phis,
1092 bitmap *visited_flag_phis)
1094 unsigned i;
1096 for (i = 0; i < MIN (max_phi_args, gimple_phi_num_args (flag_def)); i++)
1098 tree flag_arg;
1100 if (!MASK_TEST_BIT (uninit_opnds, i))
1101 continue;
1103 flag_arg = gimple_phi_arg_def (flag_def, i);
1104 if (!is_gimple_constant (flag_arg))
1106 gphi *flag_arg_def, *phi_arg_def;
1107 tree phi_arg;
1108 unsigned uninit_opnds_arg_phi;
1110 if (TREE_CODE (flag_arg) != SSA_NAME)
1111 return false;
1112 flag_arg_def = dyn_cast<gphi *> (SSA_NAME_DEF_STMT (flag_arg));
1113 if (!flag_arg_def)
1114 return false;
1116 phi_arg = gimple_phi_arg_def (phi, i);
1117 if (TREE_CODE (phi_arg) != SSA_NAME)
1118 return false;
1120 phi_arg_def = dyn_cast<gphi *> (SSA_NAME_DEF_STMT (phi_arg));
1121 if (!phi_arg_def)
1122 return false;
1124 if (gimple_bb (phi_arg_def) != gimple_bb (flag_arg_def))
1125 return false;
1127 if (!*visited_flag_phis)
1128 *visited_flag_phis = BITMAP_ALLOC (NULL);
1130 tree phi_result = gimple_phi_result (flag_arg_def);
1131 if (bitmap_bit_p (*visited_flag_phis, SSA_NAME_VERSION (phi_result)))
1132 return false;
1134 bitmap_set_bit (*visited_flag_phis,
1135 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def)));
1137 /* Now recursively prune the uninitialized phi args. */
1138 uninit_opnds_arg_phi = compute_uninit_opnds_pos (phi_arg_def);
1139 if (!prune_uninit_phi_opnds
1140 (phi_arg_def, uninit_opnds_arg_phi, flag_arg_def, boundary_cst,
1141 cmp_code, visited_phis, visited_flag_phis))
1142 return false;
1144 phi_result = gimple_phi_result (flag_arg_def);
1145 bitmap_clear_bit (*visited_flag_phis, SSA_NAME_VERSION (phi_result));
1146 continue;
1149 /* Now check if the constant is in the guarded range. */
1150 if (is_value_included_in (flag_arg, boundary_cst, cmp_code))
1152 tree opnd;
1153 gimple *opnd_def;
1155 /* Now that we know that this undefined edge is not
1156 pruned. If the operand is defined by another phi,
1157 we can further prune the incoming edges of that
1158 phi by checking the predicates of this operands. */
1160 opnd = gimple_phi_arg_def (phi, i);
1161 opnd_def = SSA_NAME_DEF_STMT (opnd);
1162 if (gphi *opnd_def_phi = dyn_cast <gphi *> (opnd_def))
1164 edge opnd_edge;
1165 unsigned uninit_opnds2 = compute_uninit_opnds_pos (opnd_def_phi);
1166 if (!MASK_EMPTY (uninit_opnds2))
1168 pred_chain_union def_preds = vNULL;
1169 bool ok;
1170 opnd_edge = gimple_phi_arg_edge (phi, i);
1171 ok = is_use_properly_guarded (phi,
1172 opnd_edge->src,
1173 opnd_def_phi,
1174 uninit_opnds2,
1175 &def_preds,
1176 visited_phis);
1177 destroy_predicate_vecs (&def_preds);
1178 if (!ok)
1179 return false;
1182 else
1183 return false;
1187 return true;
1190 /* A helper function that determines if the predicate set
1191 of the use is not overlapping with that of the uninit paths.
1192 The most common senario of guarded use is in Example 1:
1193 Example 1:
1194 if (some_cond)
1196 x = ...;
1197 flag = true;
1200 ... some code ...
1202 if (flag)
1203 use (x);
1205 The real world examples are usually more complicated, but similar
1206 and usually result from inlining:
1208 bool init_func (int * x)
1210 if (some_cond)
1211 return false;
1212 *x = ..
1213 return true;
1216 void foo (..)
1218 int x;
1220 if (!init_func (&x))
1221 return;
1223 .. some_code ...
1224 use (x);
1227 Another possible use scenario is in the following trivial example:
1229 Example 2:
1230 if (n > 0)
1231 x = 1;
1233 if (n > 0)
1235 if (m < 2)
1236 .. = x;
1239 Predicate analysis needs to compute the composite predicate:
1241 1) 'x' use predicate: (n > 0) .AND. (m < 2)
1242 2) 'x' default value (non-def) predicate: .NOT. (n > 0)
1243 (the predicate chain for phi operand defs can be computed
1244 starting from a bb that is control equivalent to the phi's
1245 bb and is dominating the operand def.)
1247 and check overlapping:
1248 (n > 0) .AND. (m < 2) .AND. (.NOT. (n > 0))
1249 <==> false
1251 This implementation provides framework that can handle
1252 scenarios. (Note that many simple cases are handled properly
1253 without the predicate analysis -- this is due to jump threading
1254 transformation which eliminates the merge point thus makes
1255 path sensitive analysis unnecessary.)
1257 PHI is the phi node whose incoming (undefined) paths need to be
1258 pruned, and UNINIT_OPNDS is the bitmap holding uninit operand
1259 positions. VISITED_PHIS is the pointer set of phi stmts being
1260 checked. */
1262 static bool
1263 use_pred_not_overlap_with_undef_path_pred (pred_chain_union preds,
1264 gphi *phi, unsigned uninit_opnds,
1265 hash_set<gphi *> *visited_phis)
1267 unsigned int i, n;
1268 gimple *flag_def = 0;
1269 tree boundary_cst = 0;
1270 enum tree_code cmp_code;
1271 bool swap_cond = false;
1272 bool invert = false;
1273 pred_chain the_pred_chain = vNULL;
1274 bitmap visited_flag_phis = NULL;
1275 bool all_pruned = false;
1276 size_t num_preds = preds.length ();
1278 gcc_assert (num_preds > 0);
1279 /* Find within the common prefix of multiple predicate chains
1280 a predicate that is a comparison of a flag variable against
1281 a constant. */
1282 the_pred_chain = preds[0];
1283 n = the_pred_chain.length ();
1284 for (i = 0; i < n; i++)
1286 tree cond_lhs, cond_rhs, flag = 0;
1288 pred_info the_pred = the_pred_chain[i];
1290 invert = the_pred.invert;
1291 cond_lhs = the_pred.pred_lhs;
1292 cond_rhs = the_pred.pred_rhs;
1293 cmp_code = the_pred.cond_code;
1295 if (cond_lhs != NULL_TREE && TREE_CODE (cond_lhs) == SSA_NAME
1296 && cond_rhs != NULL_TREE && is_gimple_constant (cond_rhs))
1298 boundary_cst = cond_rhs;
1299 flag = cond_lhs;
1301 else if (cond_rhs != NULL_TREE && TREE_CODE (cond_rhs) == SSA_NAME
1302 && cond_lhs != NULL_TREE && is_gimple_constant (cond_lhs))
1304 boundary_cst = cond_lhs;
1305 flag = cond_rhs;
1306 swap_cond = true;
1309 if (!flag)
1310 continue;
1312 flag_def = SSA_NAME_DEF_STMT (flag);
1314 if (!flag_def)
1315 continue;
1317 if ((gimple_code (flag_def) == GIMPLE_PHI)
1318 && (gimple_bb (flag_def) == gimple_bb (phi))
1319 && find_matching_predicate_in_rest_chains (the_pred, preds,
1320 num_preds))
1321 break;
1323 flag_def = 0;
1326 if (!flag_def)
1327 return false;
1329 /* Now check all the uninit incoming edge has a constant flag value
1330 that is in conflict with the use guard/predicate. */
1331 cmp_code = get_cmp_code (cmp_code, swap_cond, invert);
1333 if (cmp_code == ERROR_MARK)
1334 return false;
1336 all_pruned = prune_uninit_phi_opnds
1337 (phi, uninit_opnds, as_a<gphi *> (flag_def), boundary_cst, cmp_code,
1338 visited_phis, &visited_flag_phis);
1340 if (visited_flag_phis)
1341 BITMAP_FREE (visited_flag_phis);
1343 return all_pruned;
1346 /* The helper function returns true if two predicates X1 and X2
1347 are equivalent. It assumes the expressions have already
1348 properly re-associated. */
1350 static inline bool
1351 pred_equal_p (pred_info x1, pred_info x2)
1353 enum tree_code c1, c2;
1354 if (!operand_equal_p (x1.pred_lhs, x2.pred_lhs, 0)
1355 || !operand_equal_p (x1.pred_rhs, x2.pred_rhs, 0))
1356 return false;
1358 c1 = x1.cond_code;
1359 if (x1.invert != x2.invert
1360 && TREE_CODE_CLASS (x2.cond_code) == tcc_comparison)
1361 c2 = invert_tree_comparison (x2.cond_code, false);
1362 else
1363 c2 = x2.cond_code;
1365 return c1 == c2;
1368 /* Returns true if the predication is testing !=. */
1370 static inline bool
1371 is_neq_relop_p (pred_info pred)
1374 return ((pred.cond_code == NE_EXPR && !pred.invert)
1375 || (pred.cond_code == EQ_EXPR && pred.invert));
1378 /* Returns true if pred is of the form X != 0. */
1380 static inline bool
1381 is_neq_zero_form_p (pred_info pred)
1383 if (!is_neq_relop_p (pred) || !integer_zerop (pred.pred_rhs)
1384 || TREE_CODE (pred.pred_lhs) != SSA_NAME)
1385 return false;
1386 return true;
1389 /* The helper function returns true if two predicates X1
1390 is equivalent to X2 != 0. */
1392 static inline bool
1393 pred_expr_equal_p (pred_info x1, tree x2)
1395 if (!is_neq_zero_form_p (x1))
1396 return false;
1398 return operand_equal_p (x1.pred_lhs, x2, 0);
1401 /* Returns true of the domain of single predicate expression
1402 EXPR1 is a subset of that of EXPR2. Returns false if it
1403 can not be proved. */
1405 static bool
1406 is_pred_expr_subset_of (pred_info expr1, pred_info expr2)
1408 enum tree_code code1, code2;
1410 if (pred_equal_p (expr1, expr2))
1411 return true;
1413 if ((TREE_CODE (expr1.pred_rhs) != INTEGER_CST)
1414 || (TREE_CODE (expr2.pred_rhs) != INTEGER_CST))
1415 return false;
1417 if (!operand_equal_p (expr1.pred_lhs, expr2.pred_lhs, 0))
1418 return false;
1420 code1 = expr1.cond_code;
1421 if (expr1.invert)
1422 code1 = invert_tree_comparison (code1, false);
1423 code2 = expr2.cond_code;
1424 if (expr2.invert)
1425 code2 = invert_tree_comparison (code2, false);
1427 if ((code1 == EQ_EXPR || code1 == BIT_AND_EXPR) && code2 == BIT_AND_EXPR)
1428 return wi::eq_p (expr1.pred_rhs,
1429 wi::bit_and (expr1.pred_rhs, expr2.pred_rhs));
1431 if (code1 != code2 && code2 != NE_EXPR)
1432 return false;
1434 if (is_value_included_in (expr1.pred_rhs, expr2.pred_rhs, code2))
1435 return true;
1437 return false;
1440 /* Returns true if the domain of PRED1 is a subset
1441 of that of PRED2. Returns false if it can not be proved so. */
1443 static bool
1444 is_pred_chain_subset_of (pred_chain pred1, pred_chain pred2)
1446 size_t np1, np2, i1, i2;
1448 np1 = pred1.length ();
1449 np2 = pred2.length ();
1451 for (i2 = 0; i2 < np2; i2++)
1453 bool found = false;
1454 pred_info info2 = pred2[i2];
1455 for (i1 = 0; i1 < np1; i1++)
1457 pred_info info1 = pred1[i1];
1458 if (is_pred_expr_subset_of (info1, info2))
1460 found = true;
1461 break;
1464 if (!found)
1465 return false;
1467 return true;
1470 /* Returns true if the domain defined by
1471 one pred chain ONE_PRED is a subset of the domain
1472 of *PREDS. It returns false if ONE_PRED's domain is
1473 not a subset of any of the sub-domains of PREDS
1474 (corresponding to each individual chains in it), even
1475 though it may be still be a subset of whole domain
1476 of PREDS which is the union (ORed) of all its subdomains.
1477 In other words, the result is conservative. */
1479 static bool
1480 is_included_in (pred_chain one_pred, pred_chain_union preds)
1482 size_t i;
1483 size_t n = preds.length ();
1485 for (i = 0; i < n; i++)
1487 if (is_pred_chain_subset_of (one_pred, preds[i]))
1488 return true;
1491 return false;
1494 /* Compares two predicate sets PREDS1 and PREDS2 and returns
1495 true if the domain defined by PREDS1 is a superset
1496 of PREDS2's domain. N1 and N2 are array sizes of PREDS1 and
1497 PREDS2 respectively. The implementation chooses not to build
1498 generic trees (and relying on the folding capability of the
1499 compiler), but instead performs brute force comparison of
1500 individual predicate chains (won't be a compile time problem
1501 as the chains are pretty short). When the function returns
1502 false, it does not necessarily mean *PREDS1 is not a superset
1503 of *PREDS2, but mean it may not be so since the analysis can
1504 not prove it. In such cases, false warnings may still be
1505 emitted. */
1507 static bool
1508 is_superset_of (pred_chain_union preds1, pred_chain_union preds2)
1510 size_t i, n2;
1511 pred_chain one_pred_chain = vNULL;
1513 n2 = preds2.length ();
1515 for (i = 0; i < n2; i++)
1517 one_pred_chain = preds2[i];
1518 if (!is_included_in (one_pred_chain, preds1))
1519 return false;
1522 return true;
1525 /* Returns true if TC is AND or OR. */
1527 static inline bool
1528 is_and_or_or_p (enum tree_code tc, tree type)
1530 return (tc == BIT_IOR_EXPR
1531 || (tc == BIT_AND_EXPR
1532 && (type == 0 || TREE_CODE (type) == BOOLEAN_TYPE)));
1535 /* Returns true if X1 is the negate of X2. */
1537 static inline bool
1538 pred_neg_p (pred_info x1, pred_info x2)
1540 enum tree_code c1, c2;
1541 if (!operand_equal_p (x1.pred_lhs, x2.pred_lhs, 0)
1542 || !operand_equal_p (x1.pred_rhs, x2.pred_rhs, 0))
1543 return false;
1545 c1 = x1.cond_code;
1546 if (x1.invert == x2.invert)
1547 c2 = invert_tree_comparison (x2.cond_code, false);
1548 else
1549 c2 = x2.cond_code;
1551 return c1 == c2;
1554 /* 1) ((x IOR y) != 0) AND (x != 0) is equivalent to (x != 0);
1555 2) (X AND Y) OR (!X AND Y) is equivalent to Y;
1556 3) X OR (!X AND Y) is equivalent to (X OR Y);
1557 4) ((x IAND y) != 0) || (x != 0 AND y != 0)) is equivalent to
1558 (x != 0 AND y != 0)
1559 5) (X AND Y) OR (!X AND Z) OR (!Y AND Z) is equivalent to
1560 (X AND Y) OR Z
1562 PREDS is the predicate chains, and N is the number of chains. */
1564 /* Helper function to implement rule 1 above. ONE_CHAIN is
1565 the AND predication to be simplified. */
1567 static void
1568 simplify_pred (pred_chain *one_chain)
1570 size_t i, j, n;
1571 bool simplified = false;
1572 pred_chain s_chain = vNULL;
1574 n = one_chain->length ();
1576 for (i = 0; i < n; i++)
1578 pred_info *a_pred = &(*one_chain)[i];
1580 if (!a_pred->pred_lhs)
1581 continue;
1582 if (!is_neq_zero_form_p (*a_pred))
1583 continue;
1585 gimple *def_stmt = SSA_NAME_DEF_STMT (a_pred->pred_lhs);
1586 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1587 continue;
1588 if (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR)
1590 for (j = 0; j < n; j++)
1592 pred_info *b_pred = &(*one_chain)[j];
1594 if (!b_pred->pred_lhs)
1595 continue;
1596 if (!is_neq_zero_form_p (*b_pred))
1597 continue;
1599 if (pred_expr_equal_p (*b_pred, gimple_assign_rhs1 (def_stmt))
1600 || pred_expr_equal_p (*b_pred, gimple_assign_rhs2 (def_stmt)))
1602 /* Mark a_pred for removal. */
1603 a_pred->pred_lhs = NULL;
1604 a_pred->pred_rhs = NULL;
1605 simplified = true;
1606 break;
1612 if (!simplified)
1613 return;
1615 for (i = 0; i < n; i++)
1617 pred_info *a_pred = &(*one_chain)[i];
1618 if (!a_pred->pred_lhs)
1619 continue;
1620 s_chain.safe_push (*a_pred);
1623 one_chain->release ();
1624 *one_chain = s_chain;
1627 /* The helper function implements the rule 2 for the
1628 OR predicate PREDS.
1630 2) (X AND Y) OR (!X AND Y) is equivalent to Y. */
1632 static bool
1633 simplify_preds_2 (pred_chain_union *preds)
1635 size_t i, j, n;
1636 bool simplified = false;
1637 pred_chain_union s_preds = vNULL;
1639 /* (X AND Y) OR (!X AND Y) is equivalent to Y.
1640 (X AND Y) OR (X AND !Y) is equivalent to X. */
1642 n = preds->length ();
1643 for (i = 0; i < n; i++)
1645 pred_info x, y;
1646 pred_chain *a_chain = &(*preds)[i];
1648 if (a_chain->length () != 2)
1649 continue;
1651 x = (*a_chain)[0];
1652 y = (*a_chain)[1];
1654 for (j = 0; j < n; j++)
1656 pred_chain *b_chain;
1657 pred_info x2, y2;
1659 if (j == i)
1660 continue;
1662 b_chain = &(*preds)[j];
1663 if (b_chain->length () != 2)
1664 continue;
1666 x2 = (*b_chain)[0];
1667 y2 = (*b_chain)[1];
1669 if (pred_equal_p (x, x2) && pred_neg_p (y, y2))
1671 /* Kill a_chain. */
1672 a_chain->release ();
1673 b_chain->release ();
1674 b_chain->safe_push (x);
1675 simplified = true;
1676 break;
1678 if (pred_neg_p (x, x2) && pred_equal_p (y, y2))
1680 /* Kill a_chain. */
1681 a_chain->release ();
1682 b_chain->release ();
1683 b_chain->safe_push (y);
1684 simplified = true;
1685 break;
1689 /* Now clean up the chain. */
1690 if (simplified)
1692 for (i = 0; i < n; i++)
1694 if ((*preds)[i].is_empty ())
1695 continue;
1696 s_preds.safe_push ((*preds)[i]);
1698 preds->release ();
1699 (*preds) = s_preds;
1700 s_preds = vNULL;
1703 return simplified;
1706 /* The helper function implements the rule 2 for the
1707 OR predicate PREDS.
1709 3) x OR (!x AND y) is equivalent to x OR y. */
1711 static bool
1712 simplify_preds_3 (pred_chain_union *preds)
1714 size_t i, j, n;
1715 bool simplified = false;
1717 /* Now iteratively simplify X OR (!X AND Z ..)
1718 into X OR (Z ...). */
1720 n = preds->length ();
1721 if (n < 2)
1722 return false;
1724 for (i = 0; i < n; i++)
1726 pred_info x;
1727 pred_chain *a_chain = &(*preds)[i];
1729 if (a_chain->length () != 1)
1730 continue;
1732 x = (*a_chain)[0];
1734 for (j = 0; j < n; j++)
1736 pred_chain *b_chain;
1737 pred_info x2;
1738 size_t k;
1740 if (j == i)
1741 continue;
1743 b_chain = &(*preds)[j];
1744 if (b_chain->length () < 2)
1745 continue;
1747 for (k = 0; k < b_chain->length (); k++)
1749 x2 = (*b_chain)[k];
1750 if (pred_neg_p (x, x2))
1752 b_chain->unordered_remove (k);
1753 simplified = true;
1754 break;
1759 return simplified;
1762 /* The helper function implements the rule 4 for the
1763 OR predicate PREDS.
1765 2) ((x AND y) != 0) OR (x != 0 AND y != 0) is equivalent to
1766 (x != 0 ANd y != 0). */
1768 static bool
1769 simplify_preds_4 (pred_chain_union *preds)
1771 size_t i, j, n;
1772 bool simplified = false;
1773 pred_chain_union s_preds = vNULL;
1774 gimple *def_stmt;
1776 n = preds->length ();
1777 for (i = 0; i < n; i++)
1779 pred_info z;
1780 pred_chain *a_chain = &(*preds)[i];
1782 if (a_chain->length () != 1)
1783 continue;
1785 z = (*a_chain)[0];
1787 if (!is_neq_zero_form_p (z))
1788 continue;
1790 def_stmt = SSA_NAME_DEF_STMT (z.pred_lhs);
1791 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1792 continue;
1794 if (gimple_assign_rhs_code (def_stmt) != BIT_AND_EXPR)
1795 continue;
1797 for (j = 0; j < n; j++)
1799 pred_chain *b_chain;
1800 pred_info x2, y2;
1802 if (j == i)
1803 continue;
1805 b_chain = &(*preds)[j];
1806 if (b_chain->length () != 2)
1807 continue;
1809 x2 = (*b_chain)[0];
1810 y2 = (*b_chain)[1];
1811 if (!is_neq_zero_form_p (x2) || !is_neq_zero_form_p (y2))
1812 continue;
1814 if ((pred_expr_equal_p (x2, gimple_assign_rhs1 (def_stmt))
1815 && pred_expr_equal_p (y2, gimple_assign_rhs2 (def_stmt)))
1816 || (pred_expr_equal_p (x2, gimple_assign_rhs2 (def_stmt))
1817 && pred_expr_equal_p (y2, gimple_assign_rhs1 (def_stmt))))
1819 /* Kill a_chain. */
1820 a_chain->release ();
1821 simplified = true;
1822 break;
1826 /* Now clean up the chain. */
1827 if (simplified)
1829 for (i = 0; i < n; i++)
1831 if ((*preds)[i].is_empty ())
1832 continue;
1833 s_preds.safe_push ((*preds)[i]);
1836 preds->release ();
1837 (*preds) = s_preds;
1838 s_preds = vNULL;
1841 return simplified;
1844 /* This function simplifies predicates in PREDS. */
1846 static void
1847 simplify_preds (pred_chain_union *preds, gimple *use_or_def, bool is_use)
1849 size_t i, n;
1850 bool changed = false;
1852 if (dump_file && dump_flags & TDF_DETAILS)
1854 fprintf (dump_file, "[BEFORE SIMPLICATION -- ");
1855 dump_predicates (use_or_def, *preds, is_use ? "[USE]:\n" : "[DEF]:\n");
1858 for (i = 0; i < preds->length (); i++)
1859 simplify_pred (&(*preds)[i]);
1861 n = preds->length ();
1862 if (n < 2)
1863 return;
1867 changed = false;
1868 if (simplify_preds_2 (preds))
1869 changed = true;
1871 /* Now iteratively simplify X OR (!X AND Z ..)
1872 into X OR (Z ...). */
1873 if (simplify_preds_3 (preds))
1874 changed = true;
1876 if (simplify_preds_4 (preds))
1877 changed = true;
1879 while (changed);
1881 return;
1884 /* This is a helper function which attempts to normalize predicate chains
1885 by following UD chains. It basically builds up a big tree of either IOR
1886 operations or AND operations, and convert the IOR tree into a
1887 pred_chain_union or BIT_AND tree into a pred_chain.
1888 Example:
1890 _3 = _2 RELOP1 _1;
1891 _6 = _5 RELOP2 _4;
1892 _9 = _8 RELOP3 _7;
1893 _10 = _3 | _6;
1894 _12 = _9 | _0;
1895 _t = _10 | _12;
1897 then _t != 0 will be normalized into a pred_chain_union
1899 (_2 RELOP1 _1) OR (_5 RELOP2 _4) OR (_8 RELOP3 _7) OR (_0 != 0)
1901 Similarly given,
1903 _3 = _2 RELOP1 _1;
1904 _6 = _5 RELOP2 _4;
1905 _9 = _8 RELOP3 _7;
1906 _10 = _3 & _6;
1907 _12 = _9 & _0;
1909 then _t != 0 will be normalized into a pred_chain:
1910 (_2 RELOP1 _1) AND (_5 RELOP2 _4) AND (_8 RELOP3 _7) AND (_0 != 0)
1914 /* This is a helper function that stores a PRED into NORM_PREDS. */
1916 inline static void
1917 push_pred (pred_chain_union *norm_preds, pred_info pred)
1919 pred_chain pred_chain = vNULL;
1920 pred_chain.safe_push (pred);
1921 norm_preds->safe_push (pred_chain);
1924 /* A helper function that creates a predicate of the form
1925 OP != 0 and push it WORK_LIST. */
1927 inline static void
1928 push_to_worklist (tree op, vec<pred_info, va_heap, vl_ptr> *work_list,
1929 hash_set<tree> *mark_set)
1931 if (mark_set->contains (op))
1932 return;
1933 mark_set->add (op);
1935 pred_info arg_pred;
1936 arg_pred.pred_lhs = op;
1937 arg_pred.pred_rhs = integer_zero_node;
1938 arg_pred.cond_code = NE_EXPR;
1939 arg_pred.invert = false;
1940 work_list->safe_push (arg_pred);
1943 /* A helper that generates a pred_info from a gimple assignment
1944 CMP_ASSIGN with comparison rhs. */
1946 static pred_info
1947 get_pred_info_from_cmp (gimple *cmp_assign)
1949 pred_info n_pred;
1950 n_pred.pred_lhs = gimple_assign_rhs1 (cmp_assign);
1951 n_pred.pred_rhs = gimple_assign_rhs2 (cmp_assign);
1952 n_pred.cond_code = gimple_assign_rhs_code (cmp_assign);
1953 n_pred.invert = false;
1954 return n_pred;
1957 /* Returns true if the PHI is a degenerated phi with
1958 all args with the same value (relop). In that case, *PRED
1959 will be updated to that value. */
1961 static bool
1962 is_degenerated_phi (gimple *phi, pred_info *pred_p)
1964 int i, n;
1965 tree op0;
1966 gimple *def0;
1967 pred_info pred0;
1969 n = gimple_phi_num_args (phi);
1970 op0 = gimple_phi_arg_def (phi, 0);
1972 if (TREE_CODE (op0) != SSA_NAME)
1973 return false;
1975 def0 = SSA_NAME_DEF_STMT (op0);
1976 if (gimple_code (def0) != GIMPLE_ASSIGN)
1977 return false;
1978 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def0)) != tcc_comparison)
1979 return false;
1980 pred0 = get_pred_info_from_cmp (def0);
1982 for (i = 1; i < n; ++i)
1984 gimple *def;
1985 pred_info pred;
1986 tree op = gimple_phi_arg_def (phi, i);
1988 if (TREE_CODE (op) != SSA_NAME)
1989 return false;
1991 def = SSA_NAME_DEF_STMT (op);
1992 if (gimple_code (def) != GIMPLE_ASSIGN)
1993 return false;
1994 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def)) != tcc_comparison)
1995 return false;
1996 pred = get_pred_info_from_cmp (def);
1997 if (!pred_equal_p (pred, pred0))
1998 return false;
2001 *pred_p = pred0;
2002 return true;
2005 /* Normalize one predicate PRED
2006 1) if PRED can no longer be normlized, put it into NORM_PREDS.
2007 2) otherwise if PRED is of the form x != 0, follow x's definition
2008 and put normalized predicates into WORK_LIST. */
2010 static void
2011 normalize_one_pred_1 (pred_chain_union *norm_preds,
2012 pred_chain *norm_chain,
2013 pred_info pred,
2014 enum tree_code and_or_code,
2015 vec<pred_info, va_heap, vl_ptr> *work_list,
2016 hash_set<tree> *mark_set)
2018 if (!is_neq_zero_form_p (pred))
2020 if (and_or_code == BIT_IOR_EXPR)
2021 push_pred (norm_preds, pred);
2022 else
2023 norm_chain->safe_push (pred);
2024 return;
2027 gimple *def_stmt = SSA_NAME_DEF_STMT (pred.pred_lhs);
2029 if (gimple_code (def_stmt) == GIMPLE_PHI
2030 && is_degenerated_phi (def_stmt, &pred))
2031 work_list->safe_push (pred);
2032 else if (gimple_code (def_stmt) == GIMPLE_PHI && and_or_code == BIT_IOR_EXPR)
2034 int i, n;
2035 n = gimple_phi_num_args (def_stmt);
2037 /* If we see non zero constant, we should punt. The predicate
2038 * should be one guarding the phi edge. */
2039 for (i = 0; i < n; ++i)
2041 tree op = gimple_phi_arg_def (def_stmt, i);
2042 if (TREE_CODE (op) == INTEGER_CST && !integer_zerop (op))
2044 push_pred (norm_preds, pred);
2045 return;
2049 for (i = 0; i < n; ++i)
2051 tree op = gimple_phi_arg_def (def_stmt, i);
2052 if (integer_zerop (op))
2053 continue;
2055 push_to_worklist (op, work_list, mark_set);
2058 else if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
2060 if (and_or_code == BIT_IOR_EXPR)
2061 push_pred (norm_preds, pred);
2062 else
2063 norm_chain->safe_push (pred);
2065 else if (gimple_assign_rhs_code (def_stmt) == and_or_code)
2067 /* Avoid splitting up bit manipulations like x & 3 or y | 1. */
2068 if (is_gimple_min_invariant (gimple_assign_rhs2 (def_stmt)))
2070 /* But treat x & 3 as condition. */
2071 if (and_or_code == BIT_AND_EXPR)
2073 pred_info n_pred;
2074 n_pred.pred_lhs = gimple_assign_rhs1 (def_stmt);
2075 n_pred.pred_rhs = gimple_assign_rhs2 (def_stmt);
2076 n_pred.cond_code = and_or_code;
2077 n_pred.invert = false;
2078 norm_chain->safe_push (n_pred);
2081 else
2083 push_to_worklist (gimple_assign_rhs1 (def_stmt), work_list, mark_set);
2084 push_to_worklist (gimple_assign_rhs2 (def_stmt), work_list, mark_set);
2087 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt))
2088 == tcc_comparison)
2090 pred_info n_pred = get_pred_info_from_cmp (def_stmt);
2091 if (and_or_code == BIT_IOR_EXPR)
2092 push_pred (norm_preds, n_pred);
2093 else
2094 norm_chain->safe_push (n_pred);
2096 else
2098 if (and_or_code == BIT_IOR_EXPR)
2099 push_pred (norm_preds, pred);
2100 else
2101 norm_chain->safe_push (pred);
2105 /* Normalize PRED and store the normalized predicates into NORM_PREDS. */
2107 static void
2108 normalize_one_pred (pred_chain_union *norm_preds, pred_info pred)
2110 vec<pred_info, va_heap, vl_ptr> work_list = vNULL;
2111 enum tree_code and_or_code = ERROR_MARK;
2112 pred_chain norm_chain = vNULL;
2114 if (!is_neq_zero_form_p (pred))
2116 push_pred (norm_preds, pred);
2117 return;
2120 gimple *def_stmt = SSA_NAME_DEF_STMT (pred.pred_lhs);
2121 if (gimple_code (def_stmt) == GIMPLE_ASSIGN)
2122 and_or_code = gimple_assign_rhs_code (def_stmt);
2123 if (and_or_code != BIT_IOR_EXPR && and_or_code != BIT_AND_EXPR)
2125 if (TREE_CODE_CLASS (and_or_code) == tcc_comparison)
2127 pred_info n_pred = get_pred_info_from_cmp (def_stmt);
2128 push_pred (norm_preds, n_pred);
2130 else
2131 push_pred (norm_preds, pred);
2132 return;
2135 work_list.safe_push (pred);
2136 hash_set<tree> mark_set;
2138 while (!work_list.is_empty ())
2140 pred_info a_pred = work_list.pop ();
2141 normalize_one_pred_1 (norm_preds, &norm_chain, a_pred, and_or_code,
2142 &work_list, &mark_set);
2144 if (and_or_code == BIT_AND_EXPR)
2145 norm_preds->safe_push (norm_chain);
2147 work_list.release ();
2150 static void
2151 normalize_one_pred_chain (pred_chain_union *norm_preds, pred_chain one_chain)
2153 vec<pred_info, va_heap, vl_ptr> work_list = vNULL;
2154 hash_set<tree> mark_set;
2155 pred_chain norm_chain = vNULL;
2156 size_t i;
2158 for (i = 0; i < one_chain.length (); i++)
2160 work_list.safe_push (one_chain[i]);
2161 mark_set.add (one_chain[i].pred_lhs);
2164 while (!work_list.is_empty ())
2166 pred_info a_pred = work_list.pop ();
2167 normalize_one_pred_1 (0, &norm_chain, a_pred, BIT_AND_EXPR, &work_list,
2168 &mark_set);
2171 norm_preds->safe_push (norm_chain);
2172 work_list.release ();
2175 /* Normalize predicate chains PREDS and returns the normalized one. */
2177 static pred_chain_union
2178 normalize_preds (pred_chain_union preds, gimple *use_or_def, bool is_use)
2180 pred_chain_union norm_preds = vNULL;
2181 size_t n = preds.length ();
2182 size_t i;
2184 if (dump_file && dump_flags & TDF_DETAILS)
2186 fprintf (dump_file, "[BEFORE NORMALIZATION --");
2187 dump_predicates (use_or_def, preds, is_use ? "[USE]:\n" : "[DEF]:\n");
2190 for (i = 0; i < n; i++)
2192 if (preds[i].length () != 1)
2193 normalize_one_pred_chain (&norm_preds, preds[i]);
2194 else
2196 normalize_one_pred (&norm_preds, preds[i][0]);
2197 preds[i].release ();
2201 if (dump_file)
2203 fprintf (dump_file, "[AFTER NORMALIZATION -- ");
2204 dump_predicates (use_or_def, norm_preds,
2205 is_use ? "[USE]:\n" : "[DEF]:\n");
2208 destroy_predicate_vecs (&preds);
2209 return norm_preds;
2212 /* Return TRUE if PREDICATE can be invalidated by any individual
2213 predicate in WORKLIST. */
2215 static bool
2216 can_one_predicate_be_invalidated_p (pred_info predicate,
2217 pred_chain use_guard)
2219 for (size_t i = 0; i < use_guard.length (); ++i)
2221 /* NOTE: This is a very simple check, and only understands an
2222 exact opposite. So, [i == 0] is currently only invalidated
2223 by [.NOT. i == 0] or [i != 0]. Ideally we should also
2224 invalidate with say [i > 5] or [i == 8]. There is certainly
2225 room for improvement here. */
2226 if (pred_neg_p (predicate, use_guard[i]))
2227 return true;
2229 return false;
2232 /* Return TRUE if all predicates in UNINIT_PRED are invalidated by
2233 USE_GUARD being true. */
2235 static bool
2236 can_chain_union_be_invalidated_p (pred_chain_union uninit_pred,
2237 pred_chain use_guard)
2239 if (uninit_pred.is_empty ())
2240 return false;
2241 for (size_t i = 0; i < uninit_pred.length (); ++i)
2243 pred_chain c = uninit_pred[i];
2244 for (size_t j = 0; j < c.length (); ++j)
2245 if (!can_one_predicate_be_invalidated_p (c[j], use_guard))
2246 return false;
2248 return true;
2251 /* Return TRUE if none of the uninitialized operands in UNINT_OPNDS
2252 can actually happen if we arrived at a use for PHI.
2254 PHI_USE_GUARDS are the guard conditions for the use of the PHI. */
2256 static bool
2257 uninit_uses_cannot_happen (gphi *phi, unsigned uninit_opnds,
2258 pred_chain_union phi_use_guards)
2260 unsigned phi_args = gimple_phi_num_args (phi);
2261 if (phi_args > max_phi_args)
2262 return false;
2264 /* PHI_USE_GUARDS are OR'ed together. If we have more than one
2265 possible guard, there's no way of knowing which guard was true.
2266 Since we need to be absolutely sure that the uninitialized
2267 operands will be invalidated, bail. */
2268 if (phi_use_guards.length () != 1)
2269 return false;
2271 /* Look for the control dependencies of all the uninitialized
2272 operands and build guard predicates describing them. */
2273 pred_chain_union uninit_preds;
2274 bool ret = true;
2275 for (unsigned i = 0; i < phi_args; ++i)
2277 if (!MASK_TEST_BIT (uninit_opnds, i))
2278 continue;
2280 edge e = gimple_phi_arg_edge (phi, i);
2281 vec<edge> dep_chains[MAX_NUM_CHAINS];
2282 auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
2283 size_t num_chains = 0;
2284 int num_calls = 0;
2286 /* Build the control dependency chain for uninit operand `i'... */
2287 uninit_preds = vNULL;
2288 if (!compute_control_dep_chain (find_dom (e->src),
2289 e->src, dep_chains, &num_chains,
2290 &cur_chain, &num_calls))
2292 ret = false;
2293 break;
2295 /* ...and convert it into a set of predicates. */
2296 convert_control_dep_chain_into_preds (dep_chains, num_chains,
2297 &uninit_preds);
2298 for (size_t j = 0; j < num_chains; ++j)
2299 dep_chains[j].release ();
2300 simplify_preds (&uninit_preds, NULL, false);
2301 uninit_preds = normalize_preds (uninit_preds, NULL, false);
2303 /* Can the guard for this uninitialized operand be invalidated
2304 by the PHI use? */
2305 if (!can_chain_union_be_invalidated_p (uninit_preds, phi_use_guards[0]))
2307 ret = false;
2308 break;
2311 destroy_predicate_vecs (&uninit_preds);
2312 return ret;
2315 /* Computes the predicates that guard the use and checks
2316 if the incoming paths that have empty (or possibly
2317 empty) definition can be pruned/filtered. The function returns
2318 true if it can be determined that the use of PHI's def in
2319 USE_STMT is guarded with a predicate set not overlapping with
2320 predicate sets of all runtime paths that do not have a definition.
2322 Returns false if it is not or it can not be determined. USE_BB is
2323 the bb of the use (for phi operand use, the bb is not the bb of
2324 the phi stmt, but the src bb of the operand edge).
2326 UNINIT_OPNDS is a bit vector. If an operand of PHI is uninitialized, the
2327 corresponding bit in the vector is 1. VISITED_PHIS is a pointer
2328 set of phis being visited.
2330 *DEF_PREDS contains the (memoized) defining predicate chains of PHI.
2331 If *DEF_PREDS is the empty vector, the defining predicate chains of
2332 PHI will be computed and stored into *DEF_PREDS as needed.
2334 VISITED_PHIS is a pointer set of phis being visited. */
2336 static bool
2337 is_use_properly_guarded (gimple *use_stmt,
2338 basic_block use_bb,
2339 gphi *phi,
2340 unsigned uninit_opnds,
2341 pred_chain_union *def_preds,
2342 hash_set<gphi *> *visited_phis)
2344 basic_block phi_bb;
2345 pred_chain_union preds = vNULL;
2346 bool has_valid_preds = false;
2347 bool is_properly_guarded = false;
2349 if (visited_phis->add (phi))
2350 return false;
2352 phi_bb = gimple_bb (phi);
2354 if (is_non_loop_exit_postdominating (use_bb, phi_bb))
2355 return false;
2357 has_valid_preds = find_predicates (&preds, phi_bb, use_bb);
2359 if (!has_valid_preds)
2361 destroy_predicate_vecs (&preds);
2362 return false;
2365 /* Try to prune the dead incoming phi edges. */
2366 is_properly_guarded
2367 = use_pred_not_overlap_with_undef_path_pred (preds, phi, uninit_opnds,
2368 visited_phis);
2370 /* We might be able to prove that if the control dependencies
2371 for UNINIT_OPNDS are true, that the control dependencies for
2372 USE_STMT can never be true. */
2373 if (!is_properly_guarded)
2374 is_properly_guarded |= uninit_uses_cannot_happen (phi, uninit_opnds,
2375 preds);
2377 if (is_properly_guarded)
2379 destroy_predicate_vecs (&preds);
2380 return true;
2383 if (def_preds->is_empty ())
2385 has_valid_preds = find_def_preds (def_preds, phi);
2387 if (!has_valid_preds)
2389 destroy_predicate_vecs (&preds);
2390 return false;
2393 simplify_preds (def_preds, phi, false);
2394 *def_preds = normalize_preds (*def_preds, phi, false);
2397 simplify_preds (&preds, use_stmt, true);
2398 preds = normalize_preds (preds, use_stmt, true);
2400 is_properly_guarded = is_superset_of (*def_preds, preds);
2402 destroy_predicate_vecs (&preds);
2403 return is_properly_guarded;
2406 /* Searches through all uses of a potentially
2407 uninitialized variable defined by PHI and returns a use
2408 statement if the use is not properly guarded. It returns
2409 NULL if all uses are guarded. UNINIT_OPNDS is a bitvector
2410 holding the position(s) of uninit PHI operands. WORKLIST
2411 is the vector of candidate phis that may be updated by this
2412 function. ADDED_TO_WORKLIST is the pointer set tracking
2413 if the new phi is already in the worklist. */
2415 static gimple *
2416 find_uninit_use (gphi *phi, unsigned uninit_opnds,
2417 vec<gphi *> *worklist,
2418 hash_set<gphi *> *added_to_worklist)
2420 tree phi_result;
2421 use_operand_p use_p;
2422 gimple *use_stmt;
2423 imm_use_iterator iter;
2424 pred_chain_union def_preds = vNULL;
2425 gimple *ret = NULL;
2427 phi_result = gimple_phi_result (phi);
2429 FOR_EACH_IMM_USE_FAST (use_p, iter, phi_result)
2431 basic_block use_bb;
2433 use_stmt = USE_STMT (use_p);
2434 if (is_gimple_debug (use_stmt))
2435 continue;
2437 if (gphi *use_phi = dyn_cast<gphi *> (use_stmt))
2438 use_bb = gimple_phi_arg_edge (use_phi,
2439 PHI_ARG_INDEX_FROM_USE (use_p))->src;
2440 else
2441 use_bb = gimple_bb (use_stmt);
2443 hash_set<gphi *> visited_phis;
2444 if (is_use_properly_guarded (use_stmt, use_bb, phi, uninit_opnds,
2445 &def_preds, &visited_phis))
2446 continue;
2448 if (dump_file && (dump_flags & TDF_DETAILS))
2450 fprintf (dump_file, "[CHECK]: Found unguarded use: ");
2451 print_gimple_stmt (dump_file, use_stmt, 0, 0);
2453 /* Found one real use, return. */
2454 if (gimple_code (use_stmt) != GIMPLE_PHI)
2456 ret = use_stmt;
2457 break;
2460 /* Found a phi use that is not guarded,
2461 add the phi to the worklist. */
2462 if (!added_to_worklist->add (as_a<gphi *> (use_stmt)))
2464 if (dump_file && (dump_flags & TDF_DETAILS))
2466 fprintf (dump_file, "[WORKLIST]: Update worklist with phi: ");
2467 print_gimple_stmt (dump_file, use_stmt, 0, 0);
2470 worklist->safe_push (as_a<gphi *> (use_stmt));
2471 possibly_undefined_names->add (phi_result);
2475 destroy_predicate_vecs (&def_preds);
2476 return ret;
2479 /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions
2480 and gives warning if there exists a runtime path from the entry to a
2481 use of the PHI def that does not contain a definition. In other words,
2482 the warning is on the real use. The more dead paths that can be pruned
2483 by the compiler, the fewer false positives the warning is. WORKLIST
2484 is a vector of candidate phis to be examined. ADDED_TO_WORKLIST is
2485 a pointer set tracking if the new phi is added to the worklist or not. */
2487 static void
2488 warn_uninitialized_phi (gphi *phi, vec<gphi *> *worklist,
2489 hash_set<gphi *> *added_to_worklist)
2491 unsigned uninit_opnds;
2492 gimple *uninit_use_stmt = 0;
2493 tree uninit_op;
2494 int phiarg_index;
2495 location_t loc;
2497 /* Don't look at virtual operands. */
2498 if (virtual_operand_p (gimple_phi_result (phi)))
2499 return;
2501 uninit_opnds = compute_uninit_opnds_pos (phi);
2503 if (MASK_EMPTY (uninit_opnds))
2504 return;
2506 if (dump_file && (dump_flags & TDF_DETAILS))
2508 fprintf (dump_file, "[CHECK]: examining phi: ");
2509 print_gimple_stmt (dump_file, phi, 0, 0);
2512 /* Now check if we have any use of the value without proper guard. */
2513 uninit_use_stmt = find_uninit_use (phi, uninit_opnds,
2514 worklist, added_to_worklist);
2516 /* All uses are properly guarded. */
2517 if (!uninit_use_stmt)
2518 return;
2520 phiarg_index = MASK_FIRST_SET_BIT (uninit_opnds);
2521 uninit_op = gimple_phi_arg_def (phi, phiarg_index);
2522 if (SSA_NAME_VAR (uninit_op) == NULL_TREE)
2523 return;
2524 if (gimple_phi_arg_has_location (phi, phiarg_index))
2525 loc = gimple_phi_arg_location (phi, phiarg_index);
2526 else
2527 loc = UNKNOWN_LOCATION;
2528 warn_uninit (OPT_Wmaybe_uninitialized, uninit_op, SSA_NAME_VAR (uninit_op),
2529 SSA_NAME_VAR (uninit_op),
2530 "%qD may be used uninitialized in this function",
2531 uninit_use_stmt, loc);
2534 static bool
2535 gate_warn_uninitialized (void)
2537 return warn_uninitialized || warn_maybe_uninitialized;
2540 namespace {
2542 const pass_data pass_data_late_warn_uninitialized =
2544 GIMPLE_PASS, /* type */
2545 "uninit", /* name */
2546 OPTGROUP_NONE, /* optinfo_flags */
2547 TV_NONE, /* tv_id */
2548 PROP_ssa, /* properties_required */
2549 0, /* properties_provided */
2550 0, /* properties_destroyed */
2551 0, /* todo_flags_start */
2552 0, /* todo_flags_finish */
2555 class pass_late_warn_uninitialized : public gimple_opt_pass
2557 public:
2558 pass_late_warn_uninitialized (gcc::context *ctxt)
2559 : gimple_opt_pass (pass_data_late_warn_uninitialized, ctxt)
2562 /* opt_pass methods: */
2563 opt_pass *clone () { return new pass_late_warn_uninitialized (m_ctxt); }
2564 virtual bool gate (function *) { return gate_warn_uninitialized (); }
2565 virtual unsigned int execute (function *);
2567 }; // class pass_late_warn_uninitialized
2569 unsigned int
2570 pass_late_warn_uninitialized::execute (function *fun)
2572 basic_block bb;
2573 gphi_iterator gsi;
2574 vec<gphi *> worklist = vNULL;
2576 calculate_dominance_info (CDI_DOMINATORS);
2577 calculate_dominance_info (CDI_POST_DOMINATORS);
2578 /* Re-do the plain uninitialized variable check, as optimization may have
2579 straightened control flow. Do this first so that we don't accidentally
2580 get a "may be" warning when we'd have seen an "is" warning later. */
2581 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/1);
2583 timevar_push (TV_TREE_UNINIT);
2585 possibly_undefined_names = new hash_set<tree>;
2586 hash_set<gphi *> added_to_worklist;
2588 /* Initialize worklist */
2589 FOR_EACH_BB_FN (bb, fun)
2590 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2592 gphi *phi = gsi.phi ();
2593 size_t n, i;
2595 n = gimple_phi_num_args (phi);
2597 /* Don't look at virtual operands. */
2598 if (virtual_operand_p (gimple_phi_result (phi)))
2599 continue;
2601 for (i = 0; i < n; ++i)
2603 tree op = gimple_phi_arg_def (phi, i);
2604 if (TREE_CODE (op) == SSA_NAME && uninit_undefined_value_p (op))
2606 worklist.safe_push (phi);
2607 added_to_worklist.add (phi);
2608 if (dump_file && (dump_flags & TDF_DETAILS))
2610 fprintf (dump_file, "[WORKLIST]: add to initial list: ");
2611 print_gimple_stmt (dump_file, phi, 0, 0);
2613 break;
2618 while (worklist.length () != 0)
2620 gphi *cur_phi = 0;
2621 cur_phi = worklist.pop ();
2622 warn_uninitialized_phi (cur_phi, &worklist, &added_to_worklist);
2625 worklist.release ();
2626 delete possibly_undefined_names;
2627 possibly_undefined_names = NULL;
2628 free_dominance_info (CDI_POST_DOMINATORS);
2629 timevar_pop (TV_TREE_UNINIT);
2630 return 0;
2633 } // anon namespace
2635 gimple_opt_pass *
2636 make_pass_late_warn_uninitialized (gcc::context *ctxt)
2638 return new pass_late_warn_uninitialized (ctxt);
2641 static unsigned int
2642 execute_early_warn_uninitialized (void)
2644 /* Currently, this pass runs always but
2645 execute_late_warn_uninitialized only runs with optimization. With
2646 optimization we want to warn about possible uninitialized as late
2647 as possible, thus don't do it here. However, without
2648 optimization we need to warn here about "may be uninitialized". */
2649 calculate_dominance_info (CDI_POST_DOMINATORS);
2651 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/!optimize);
2653 /* Post-dominator information can not be reliably updated. Free it
2654 after the use. */
2656 free_dominance_info (CDI_POST_DOMINATORS);
2657 return 0;
2660 namespace {
2662 const pass_data pass_data_early_warn_uninitialized =
2664 GIMPLE_PASS, /* type */
2665 "*early_warn_uninitialized", /* name */
2666 OPTGROUP_NONE, /* optinfo_flags */
2667 TV_TREE_UNINIT, /* tv_id */
2668 PROP_ssa, /* properties_required */
2669 0, /* properties_provided */
2670 0, /* properties_destroyed */
2671 0, /* todo_flags_start */
2672 0, /* todo_flags_finish */
2675 class pass_early_warn_uninitialized : public gimple_opt_pass
2677 public:
2678 pass_early_warn_uninitialized (gcc::context *ctxt)
2679 : gimple_opt_pass (pass_data_early_warn_uninitialized, ctxt)
2682 /* opt_pass methods: */
2683 virtual bool gate (function *) { return gate_warn_uninitialized (); }
2684 virtual unsigned int execute (function *)
2686 return execute_early_warn_uninitialized ();
2689 }; // class pass_early_warn_uninitialized
2691 } // anon namespace
2693 gimple_opt_pass *
2694 make_pass_early_warn_uninitialized (gcc::context *ctxt)
2696 return new pass_early_warn_uninitialized (ctxt);