2013-01-12 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / gcc / tree-ssa-uninit.c
bloba91ee379d696bf07553180e52faadb37ce6248af
1 /* Predicate aware uninitialized variable warning.
2 Copyright (C) 2001-2013 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 "function.h"
30 #include "gimple-pretty-print.h"
31 #include "bitmap.h"
32 #include "pointer-set.h"
33 #include "tree-flow.h"
34 #include "gimple.h"
35 #include "tree-inline.h"
36 #include "hashtab.h"
37 #include "tree-pass.h"
38 #include "diagnostic-core.h"
40 /* This implements the pass that does predicate aware warning on uses of
41 possibly uninitialized variables. The pass first collects the set of
42 possibly uninitialized SSA names. For each such name, it walks through
43 all its immediate uses. For each immediate use, it rebuilds the condition
44 expression (the predicate) that guards the use. The predicate is then
45 examined to see if the variable is always defined under that same condition.
46 This is done either by pruning the unrealizable paths that lead to the
47 default definitions or by checking if the predicate set that guards the
48 defining paths is a superset of the use predicate. */
51 /* Pointer set of potentially undefined ssa names, i.e.,
52 ssa names that are defined by phi with operands that
53 are not defined or potentially undefined. */
54 static struct pointer_set_t *possibly_undefined_names = 0;
56 /* Bit mask handling macros. */
57 #define MASK_SET_BIT(mask, pos) mask |= (1 << pos)
58 #define MASK_TEST_BIT(mask, pos) (mask & (1 << pos))
59 #define MASK_EMPTY(mask) (mask == 0)
61 /* Returns the first bit position (starting from LSB)
62 in mask that is non zero. Returns -1 if the mask is empty. */
63 static int
64 get_mask_first_set_bit (unsigned mask)
66 int pos = 0;
67 if (mask == 0)
68 return -1;
70 while ((mask & (1 << pos)) == 0)
71 pos++;
73 return pos;
75 #define MASK_FIRST_SET_BIT(mask) get_mask_first_set_bit (mask)
78 /* Return true if T, an SSA_NAME, has an undefined value. */
80 bool
81 ssa_undefined_value_p (tree t)
83 tree var = SSA_NAME_VAR (t);
85 if (!var)
87 /* Parameters get their initial value from the function entry. */
88 else if (TREE_CODE (var) == PARM_DECL)
89 return false;
90 /* When returning by reference the return address is actually a hidden
91 parameter. */
92 else if (TREE_CODE (var) == RESULT_DECL && DECL_BY_REFERENCE (var))
93 return false;
94 /* Hard register variables get their initial value from the ether. */
95 else if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
96 return false;
98 /* The value is undefined iff its definition statement is empty. */
99 return (gimple_nop_p (SSA_NAME_DEF_STMT (t))
100 || (possibly_undefined_names
101 && pointer_set_contains (possibly_undefined_names, t)));
104 /* Checks if the operand OPND of PHI is defined by
105 another phi with one operand defined by this PHI,
106 but the rest operands are all defined. If yes,
107 returns true to skip this this operand as being
108 redundant. Can be enhanced to be more general. */
110 static bool
111 can_skip_redundant_opnd (tree opnd, gimple phi)
113 gimple op_def;
114 tree phi_def;
115 int i, n;
117 phi_def = gimple_phi_result (phi);
118 op_def = SSA_NAME_DEF_STMT (opnd);
119 if (gimple_code (op_def) != GIMPLE_PHI)
120 return false;
121 n = gimple_phi_num_args (op_def);
122 for (i = 0; i < n; ++i)
124 tree op = gimple_phi_arg_def (op_def, i);
125 if (TREE_CODE (op) != SSA_NAME)
126 continue;
127 if (op != phi_def && ssa_undefined_value_p (op))
128 return false;
131 return true;
134 /* Returns a bit mask holding the positions of arguments in PHI
135 that have empty (or possibly empty) definitions. */
137 static unsigned
138 compute_uninit_opnds_pos (gimple phi)
140 size_t i, n;
141 unsigned uninit_opnds = 0;
143 n = gimple_phi_num_args (phi);
144 /* Bail out for phi with too many args. */
145 if (n > 32)
146 return 0;
148 for (i = 0; i < n; ++i)
150 tree op = gimple_phi_arg_def (phi, i);
151 if (TREE_CODE (op) == SSA_NAME
152 && ssa_undefined_value_p (op)
153 && !can_skip_redundant_opnd (op, phi))
154 MASK_SET_BIT (uninit_opnds, i);
156 return uninit_opnds;
159 /* Find the immediate postdominator PDOM of the specified
160 basic block BLOCK. */
162 static inline basic_block
163 find_pdom (basic_block block)
165 if (block == EXIT_BLOCK_PTR)
166 return EXIT_BLOCK_PTR;
167 else
169 basic_block bb
170 = get_immediate_dominator (CDI_POST_DOMINATORS, block);
171 if (! bb)
172 return EXIT_BLOCK_PTR;
173 return bb;
177 /* Find the immediate DOM of the specified
178 basic block BLOCK. */
180 static inline basic_block
181 find_dom (basic_block block)
183 if (block == ENTRY_BLOCK_PTR)
184 return ENTRY_BLOCK_PTR;
185 else
187 basic_block bb = get_immediate_dominator (CDI_DOMINATORS, block);
188 if (! bb)
189 return ENTRY_BLOCK_PTR;
190 return bb;
194 /* Returns true if BB1 is postdominating BB2 and BB1 is
195 not a loop exit bb. The loop exit bb check is simple and does
196 not cover all cases. */
198 static bool
199 is_non_loop_exit_postdominating (basic_block bb1, basic_block bb2)
201 if (!dominated_by_p (CDI_POST_DOMINATORS, bb2, bb1))
202 return false;
204 if (single_pred_p (bb1) && !single_succ_p (bb2))
205 return false;
207 return true;
210 /* Find the closest postdominator of a specified BB, which is control
211 equivalent to BB. */
213 static inline basic_block
214 find_control_equiv_block (basic_block bb)
216 basic_block pdom;
218 pdom = find_pdom (bb);
220 /* Skip the postdominating bb that is also loop exit. */
221 if (!is_non_loop_exit_postdominating (pdom, bb))
222 return NULL;
224 if (dominated_by_p (CDI_DOMINATORS, pdom, bb))
225 return pdom;
227 return NULL;
230 #define MAX_NUM_CHAINS 8
231 #define MAX_CHAIN_LEN 5
233 /* Computes the control dependence chains (paths of edges)
234 for DEP_BB up to the dominating basic block BB (the head node of a
235 chain should be dominated by it). CD_CHAINS is pointer to a
236 dynamic array holding the result chains. CUR_CD_CHAIN is the current
237 chain being computed. *NUM_CHAINS is total number of chains. The
238 function returns true if the information is successfully computed,
239 return false if there is no control dependence or not computed. */
241 static bool
242 compute_control_dep_chain (basic_block bb, basic_block dep_bb,
243 vec<edge> *cd_chains,
244 size_t *num_chains,
245 vec<edge> *cur_cd_chain)
247 edge_iterator ei;
248 edge e;
249 size_t i;
250 bool found_cd_chain = false;
251 size_t cur_chain_len = 0;
253 if (EDGE_COUNT (bb->succs) < 2)
254 return false;
256 /* Could use a set instead. */
257 cur_chain_len = cur_cd_chain->length ();
258 if (cur_chain_len > MAX_CHAIN_LEN)
259 return false;
261 for (i = 0; i < cur_chain_len; i++)
263 edge e = (*cur_cd_chain)[i];
264 /* cycle detected. */
265 if (e->src == bb)
266 return false;
269 FOR_EACH_EDGE (e, ei, bb->succs)
271 basic_block cd_bb;
272 if (e->flags & (EDGE_FAKE | EDGE_ABNORMAL))
273 continue;
275 cd_bb = e->dest;
276 cur_cd_chain->safe_push (e);
277 while (!is_non_loop_exit_postdominating (cd_bb, bb))
279 if (cd_bb == dep_bb)
281 /* Found a direct control dependence. */
282 if (*num_chains < MAX_NUM_CHAINS)
284 cd_chains[*num_chains] = cur_cd_chain->copy ();
285 (*num_chains)++;
287 found_cd_chain = true;
288 /* check path from next edge. */
289 break;
292 /* Now check if DEP_BB is indirectly control dependent on BB. */
293 if (compute_control_dep_chain (cd_bb, dep_bb, cd_chains,
294 num_chains, cur_cd_chain))
296 found_cd_chain = true;
297 break;
300 cd_bb = find_pdom (cd_bb);
301 if (cd_bb == EXIT_BLOCK_PTR)
302 break;
304 cur_cd_chain->pop ();
305 gcc_assert (cur_cd_chain->length () == cur_chain_len);
307 gcc_assert (cur_cd_chain->length () == cur_chain_len);
309 return found_cd_chain;
312 typedef struct use_pred_info
314 gimple cond;
315 bool invert;
316 } *use_pred_info_t;
320 /* Converts the chains of control dependence edges into a set of
321 predicates. A control dependence chain is represented by a vector
322 edges. DEP_CHAINS points to an array of dependence chains.
323 NUM_CHAINS is the size of the chain array. One edge in a dependence
324 chain is mapped to predicate expression represented by use_pred_info_t
325 type. One dependence chain is converted to a composite predicate that
326 is the result of AND operation of use_pred_info_t mapped to each edge.
327 A composite predicate is presented by a vector of use_pred_info_t. On
328 return, *PREDS points to the resulting array of composite predicates.
329 *NUM_PREDS is the number of composite predictes. */
331 static bool
332 convert_control_dep_chain_into_preds (vec<edge> *dep_chains,
333 size_t num_chains,
334 vec<use_pred_info_t> **preds,
335 size_t *num_preds)
337 bool has_valid_pred = false;
338 size_t i, j;
339 if (num_chains == 0 || num_chains >= MAX_NUM_CHAINS)
340 return false;
342 /* Now convert the control dep chain into a set
343 of predicates. */
344 typedef vec<use_pred_info_t> vec_use_pred_info_t_heap;
345 *preds = XCNEWVEC (vec_use_pred_info_t_heap, num_chains);
346 *num_preds = num_chains;
348 for (i = 0; i < num_chains; i++)
350 vec<edge> one_cd_chain = dep_chains[i];
352 has_valid_pred = false;
353 for (j = 0; j < one_cd_chain.length (); j++)
355 gimple cond_stmt;
356 gimple_stmt_iterator gsi;
357 basic_block guard_bb;
358 use_pred_info_t one_pred;
359 edge e;
361 e = one_cd_chain[j];
362 guard_bb = e->src;
363 gsi = gsi_last_bb (guard_bb);
364 if (gsi_end_p (gsi))
366 has_valid_pred = false;
367 break;
369 cond_stmt = gsi_stmt (gsi);
370 if (gimple_code (cond_stmt) == GIMPLE_CALL
371 && EDGE_COUNT (e->src->succs) >= 2)
373 /* Ignore EH edge. Can add assertion
374 on the other edge's flag. */
375 continue;
377 /* Skip if there is essentially one succesor. */
378 if (EDGE_COUNT (e->src->succs) == 2)
380 edge e1;
381 edge_iterator ei1;
382 bool skip = false;
384 FOR_EACH_EDGE (e1, ei1, e->src->succs)
386 if (EDGE_COUNT (e1->dest->succs) == 0)
388 skip = true;
389 break;
392 if (skip)
393 continue;
395 if (gimple_code (cond_stmt) != GIMPLE_COND)
397 has_valid_pred = false;
398 break;
400 one_pred = XNEW (struct use_pred_info);
401 one_pred->cond = cond_stmt;
402 one_pred->invert = !!(e->flags & EDGE_FALSE_VALUE);
403 (*preds)[i].safe_push (one_pred);
404 has_valid_pred = true;
407 if (!has_valid_pred)
408 break;
410 return has_valid_pred;
413 /* Computes all control dependence chains for USE_BB. The control
414 dependence chains are then converted to an array of composite
415 predicates pointed to by PREDS. PHI_BB is the basic block of
416 the phi whose result is used in USE_BB. */
418 static bool
419 find_predicates (vec<use_pred_info_t> **preds,
420 size_t *num_preds,
421 basic_block phi_bb,
422 basic_block use_bb)
424 size_t num_chains = 0, i;
425 vec<edge> *dep_chains = 0;
426 vec<edge> cur_chain = vNULL;
427 bool has_valid_pred = false;
428 basic_block cd_root = 0;
430 typedef vec<edge> vec_edge_heap;
431 dep_chains = XCNEWVEC (vec_edge_heap, MAX_NUM_CHAINS);
433 /* First find the closest bb that is control equivalent to PHI_BB
434 that also dominates USE_BB. */
435 cd_root = phi_bb;
436 while (dominated_by_p (CDI_DOMINATORS, use_bb, cd_root))
438 basic_block ctrl_eq_bb = find_control_equiv_block (cd_root);
439 if (ctrl_eq_bb && dominated_by_p (CDI_DOMINATORS, use_bb, ctrl_eq_bb))
440 cd_root = ctrl_eq_bb;
441 else
442 break;
445 compute_control_dep_chain (cd_root, use_bb,
446 dep_chains, &num_chains,
447 &cur_chain);
449 has_valid_pred
450 = convert_control_dep_chain_into_preds (dep_chains,
451 num_chains,
452 preds,
453 num_preds);
454 /* Free individual chain */
455 cur_chain.release ();
456 for (i = 0; i < num_chains; i++)
457 dep_chains[i].release ();
458 free (dep_chains);
459 return has_valid_pred;
462 /* Computes the set of incoming edges of PHI that have non empty
463 definitions of a phi chain. The collection will be done
464 recursively on operands that are defined by phis. CD_ROOT
465 is the control dependence root. *EDGES holds the result, and
466 VISITED_PHIS is a pointer set for detecting cycles. */
468 static void
469 collect_phi_def_edges (gimple phi, basic_block cd_root,
470 vec<edge> *edges,
471 struct pointer_set_t *visited_phis)
473 size_t i, n;
474 edge opnd_edge;
475 tree opnd;
477 if (pointer_set_insert (visited_phis, phi))
478 return;
480 n = gimple_phi_num_args (phi);
481 for (i = 0; i < n; i++)
483 opnd_edge = gimple_phi_arg_edge (phi, i);
484 opnd = gimple_phi_arg_def (phi, i);
486 if (TREE_CODE (opnd) != SSA_NAME)
488 if (dump_file && (dump_flags & TDF_DETAILS))
490 fprintf (dump_file, "\n[CHECK] Found def edge %d in ", (int)i);
491 print_gimple_stmt (dump_file, phi, 0, 0);
493 edges->safe_push (opnd_edge);
495 else
497 gimple def = SSA_NAME_DEF_STMT (opnd);
499 if (gimple_code (def) == GIMPLE_PHI
500 && dominated_by_p (CDI_DOMINATORS,
501 gimple_bb (def), cd_root))
502 collect_phi_def_edges (def, cd_root, edges,
503 visited_phis);
504 else if (!ssa_undefined_value_p (opnd))
506 if (dump_file && (dump_flags & TDF_DETAILS))
508 fprintf (dump_file, "\n[CHECK] Found def edge %d in ", (int)i);
509 print_gimple_stmt (dump_file, phi, 0, 0);
511 edges->safe_push (opnd_edge);
517 /* For each use edge of PHI, computes all control dependence chains.
518 The control dependence chains are then converted to an array of
519 composite predicates pointed to by PREDS. */
521 static bool
522 find_def_preds (vec<use_pred_info_t> **preds,
523 size_t *num_preds, gimple phi)
525 size_t num_chains = 0, i, n;
526 vec<edge> *dep_chains = 0;
527 vec<edge> cur_chain = vNULL;
528 vec<edge> def_edges = vNULL;
529 bool has_valid_pred = false;
530 basic_block phi_bb, cd_root = 0;
531 struct pointer_set_t *visited_phis;
533 typedef vec<edge> vec_edge_heap;
534 dep_chains = XCNEWVEC (vec_edge_heap, MAX_NUM_CHAINS);
536 phi_bb = gimple_bb (phi);
537 /* First find the closest dominating bb to be
538 the control dependence root */
539 cd_root = find_dom (phi_bb);
540 if (!cd_root)
541 return false;
543 visited_phis = pointer_set_create ();
544 collect_phi_def_edges (phi, cd_root, &def_edges, visited_phis);
545 pointer_set_destroy (visited_phis);
547 n = def_edges.length ();
548 if (n == 0)
549 return false;
551 for (i = 0; i < n; i++)
553 size_t prev_nc, j;
554 edge opnd_edge;
556 opnd_edge = def_edges[i];
557 prev_nc = num_chains;
558 compute_control_dep_chain (cd_root, opnd_edge->src,
559 dep_chains, &num_chains,
560 &cur_chain);
561 /* Free individual chain */
562 cur_chain.release ();
564 /* Now update the newly added chains with
565 the phi operand edge: */
566 if (EDGE_COUNT (opnd_edge->src->succs) > 1)
568 if (prev_nc == num_chains
569 && num_chains < MAX_NUM_CHAINS)
570 num_chains++;
571 for (j = prev_nc; j < num_chains; j++)
573 dep_chains[j].safe_push (opnd_edge);
578 has_valid_pred
579 = convert_control_dep_chain_into_preds (dep_chains,
580 num_chains,
581 preds,
582 num_preds);
583 for (i = 0; i < num_chains; i++)
584 dep_chains[i].release ();
585 free (dep_chains);
586 return has_valid_pred;
589 /* Dumps the predicates (PREDS) for USESTMT. */
591 static void
592 dump_predicates (gimple usestmt, size_t num_preds,
593 vec<use_pred_info_t> *preds,
594 const char* msg)
596 size_t i, j;
597 vec<use_pred_info_t> one_pred_chain;
598 fprintf (dump_file, msg);
599 print_gimple_stmt (dump_file, usestmt, 0, 0);
600 fprintf (dump_file, "is guarded by :\n");
601 /* do some dumping here: */
602 for (i = 0; i < num_preds; i++)
604 size_t np;
606 one_pred_chain = preds[i];
607 np = one_pred_chain.length ();
609 for (j = 0; j < np; j++)
611 use_pred_info_t one_pred
612 = one_pred_chain[j];
613 if (one_pred->invert)
614 fprintf (dump_file, " (.NOT.) ");
615 print_gimple_stmt (dump_file, one_pred->cond, 0, 0);
616 if (j < np - 1)
617 fprintf (dump_file, "(.AND.)\n");
619 if (i < num_preds - 1)
620 fprintf (dump_file, "(.OR.)\n");
624 /* Destroys the predicate set *PREDS. */
626 static void
627 destroy_predicate_vecs (size_t n,
628 vec<use_pred_info_t> * preds)
630 size_t i, j;
631 for (i = 0; i < n; i++)
633 for (j = 0; j < preds[i].length (); j++)
634 free (preds[i][j]);
635 preds[i].release ();
637 free (preds);
641 /* Computes the 'normalized' conditional code with operand
642 swapping and condition inversion. */
644 static enum tree_code
645 get_cmp_code (enum tree_code orig_cmp_code,
646 bool swap_cond, bool invert)
648 enum tree_code tc = orig_cmp_code;
650 if (swap_cond)
651 tc = swap_tree_comparison (orig_cmp_code);
652 if (invert)
653 tc = invert_tree_comparison (tc, false);
655 switch (tc)
657 case LT_EXPR:
658 case LE_EXPR:
659 case GT_EXPR:
660 case GE_EXPR:
661 case EQ_EXPR:
662 case NE_EXPR:
663 break;
664 default:
665 return ERROR_MARK;
667 return tc;
670 /* Returns true if VAL falls in the range defined by BOUNDARY and CMPC, i.e.
671 all values in the range satisfies (x CMPC BOUNDARY) == true. */
673 static bool
674 is_value_included_in (tree val, tree boundary, enum tree_code cmpc)
676 bool inverted = false;
677 bool is_unsigned;
678 bool result;
680 /* Only handle integer constant here. */
681 if (TREE_CODE (val) != INTEGER_CST
682 || TREE_CODE (boundary) != INTEGER_CST)
683 return true;
685 is_unsigned = TYPE_UNSIGNED (TREE_TYPE (val));
687 if (cmpc == GE_EXPR || cmpc == GT_EXPR
688 || cmpc == NE_EXPR)
690 cmpc = invert_tree_comparison (cmpc, false);
691 inverted = true;
694 if (is_unsigned)
696 if (cmpc == EQ_EXPR)
697 result = tree_int_cst_equal (val, boundary);
698 else if (cmpc == LT_EXPR)
699 result = INT_CST_LT_UNSIGNED (val, boundary);
700 else
702 gcc_assert (cmpc == LE_EXPR);
703 result = (tree_int_cst_equal (val, boundary)
704 || INT_CST_LT_UNSIGNED (val, boundary));
707 else
709 if (cmpc == EQ_EXPR)
710 result = tree_int_cst_equal (val, boundary);
711 else if (cmpc == LT_EXPR)
712 result = INT_CST_LT (val, boundary);
713 else
715 gcc_assert (cmpc == LE_EXPR);
716 result = (tree_int_cst_equal (val, boundary)
717 || INT_CST_LT (val, boundary));
721 if (inverted)
722 result ^= 1;
724 return result;
727 /* Returns true if PRED is common among all the predicate
728 chains (PREDS) (and therefore can be factored out).
729 NUM_PRED_CHAIN is the size of array PREDS. */
731 static bool
732 find_matching_predicate_in_rest_chains (use_pred_info_t pred,
733 vec<use_pred_info_t> *preds,
734 size_t num_pred_chains)
736 size_t i, j, n;
738 /* trival case */
739 if (num_pred_chains == 1)
740 return true;
742 for (i = 1; i < num_pred_chains; i++)
744 bool found = false;
745 vec<use_pred_info_t> one_chain = preds[i];
746 n = one_chain.length ();
747 for (j = 0; j < n; j++)
749 use_pred_info_t pred2
750 = one_chain[j];
751 /* can relax the condition comparison to not
752 use address comparison. However, the most common
753 case is that multiple control dependent paths share
754 a common path prefix, so address comparison should
755 be ok. */
757 if (pred2->cond == pred->cond
758 && pred2->invert == pred->invert)
760 found = true;
761 break;
764 if (!found)
765 return false;
767 return true;
770 /* Forward declaration. */
771 static bool
772 is_use_properly_guarded (gimple use_stmt,
773 basic_block use_bb,
774 gimple phi,
775 unsigned uninit_opnds,
776 struct pointer_set_t *visited_phis);
778 /* Returns true if all uninitialized opnds are pruned. Returns false
779 otherwise. PHI is the phi node with uninitialized operands,
780 UNINIT_OPNDS is the bitmap of the uninitialize operand positions,
781 FLAG_DEF is the statement defining the flag guarding the use of the
782 PHI output, BOUNDARY_CST is the const value used in the predicate
783 associated with the flag, CMP_CODE is the comparison code used in
784 the predicate, VISITED_PHIS is the pointer set of phis visited, and
785 VISITED_FLAG_PHIS is the pointer to the pointer set of flag definitions
786 that are also phis.
788 Example scenario:
790 BB1:
791 flag_1 = phi <0, 1> // (1)
792 var_1 = phi <undef, some_val>
795 BB2:
796 flag_2 = phi <0, flag_1, flag_1> // (2)
797 var_2 = phi <undef, var_1, var_1>
798 if (flag_2 == 1)
799 goto BB3;
801 BB3:
802 use of var_2 // (3)
804 Because some flag arg in (1) is not constant, if we do not look into the
805 flag phis recursively, it is conservatively treated as unknown and var_1
806 is thought to be flowed into use at (3). Since var_1 is potentially uninitialized
807 a false warning will be emitted. Checking recursively into (1), the compiler can
808 find out that only some_val (which is defined) can flow into (3) which is OK.
812 static bool
813 prune_uninit_phi_opnds_in_unrealizable_paths (
814 gimple phi, unsigned uninit_opnds,
815 gimple flag_def, tree boundary_cst,
816 enum tree_code cmp_code,
817 struct pointer_set_t *visited_phis,
818 bitmap *visited_flag_phis)
820 unsigned i;
822 for (i = 0; i < MIN (32, gimple_phi_num_args (flag_def)); i++)
824 tree flag_arg;
826 if (!MASK_TEST_BIT (uninit_opnds, i))
827 continue;
829 flag_arg = gimple_phi_arg_def (flag_def, i);
830 if (!is_gimple_constant (flag_arg))
832 gimple flag_arg_def, phi_arg_def;
833 tree phi_arg;
834 unsigned uninit_opnds_arg_phi;
836 if (TREE_CODE (flag_arg) != SSA_NAME)
837 return false;
838 flag_arg_def = SSA_NAME_DEF_STMT (flag_arg);
839 if (gimple_code (flag_arg_def) != GIMPLE_PHI)
840 return false;
842 phi_arg = gimple_phi_arg_def (phi, i);
843 if (TREE_CODE (phi_arg) != SSA_NAME)
844 return false;
846 phi_arg_def = SSA_NAME_DEF_STMT (phi_arg);
847 if (gimple_code (phi_arg_def) != GIMPLE_PHI)
848 return false;
850 if (gimple_bb (phi_arg_def) != gimple_bb (flag_arg_def))
851 return false;
853 if (!*visited_flag_phis)
854 *visited_flag_phis = BITMAP_ALLOC (NULL);
856 if (bitmap_bit_p (*visited_flag_phis,
857 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def))))
858 return false;
860 bitmap_set_bit (*visited_flag_phis,
861 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def)));
863 /* Now recursively prune the uninitialized phi args. */
864 uninit_opnds_arg_phi = compute_uninit_opnds_pos (phi_arg_def);
865 if (!prune_uninit_phi_opnds_in_unrealizable_paths (
866 phi_arg_def, uninit_opnds_arg_phi,
867 flag_arg_def, boundary_cst, cmp_code,
868 visited_phis, visited_flag_phis))
869 return false;
871 bitmap_clear_bit (*visited_flag_phis,
872 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def)));
873 continue;
876 /* Now check if the constant is in the guarded range. */
877 if (is_value_included_in (flag_arg, boundary_cst, cmp_code))
879 tree opnd;
880 gimple opnd_def;
882 /* Now that we know that this undefined edge is not
883 pruned. If the operand is defined by another phi,
884 we can further prune the incoming edges of that
885 phi by checking the predicates of this operands. */
887 opnd = gimple_phi_arg_def (phi, i);
888 opnd_def = SSA_NAME_DEF_STMT (opnd);
889 if (gimple_code (opnd_def) == GIMPLE_PHI)
891 edge opnd_edge;
892 unsigned uninit_opnds2
893 = compute_uninit_opnds_pos (opnd_def);
894 gcc_assert (!MASK_EMPTY (uninit_opnds2));
895 opnd_edge = gimple_phi_arg_edge (phi, i);
896 if (!is_use_properly_guarded (phi,
897 opnd_edge->src,
898 opnd_def,
899 uninit_opnds2,
900 visited_phis))
901 return false;
903 else
904 return false;
908 return true;
911 /* A helper function that determines if the predicate set
912 of the use is not overlapping with that of the uninit paths.
913 The most common senario of guarded use is in Example 1:
914 Example 1:
915 if (some_cond)
917 x = ...;
918 flag = true;
921 ... some code ...
923 if (flag)
924 use (x);
926 The real world examples are usually more complicated, but similar
927 and usually result from inlining:
929 bool init_func (int * x)
931 if (some_cond)
932 return false;
933 *x = ..
934 return true;
937 void foo(..)
939 int x;
941 if (!init_func(&x))
942 return;
944 .. some_code ...
945 use (x);
948 Another possible use scenario is in the following trivial example:
950 Example 2:
951 if (n > 0)
952 x = 1;
954 if (n > 0)
956 if (m < 2)
957 .. = x;
960 Predicate analysis needs to compute the composite predicate:
962 1) 'x' use predicate: (n > 0) .AND. (m < 2)
963 2) 'x' default value (non-def) predicate: .NOT. (n > 0)
964 (the predicate chain for phi operand defs can be computed
965 starting from a bb that is control equivalent to the phi's
966 bb and is dominating the operand def.)
968 and check overlapping:
969 (n > 0) .AND. (m < 2) .AND. (.NOT. (n > 0))
970 <==> false
972 This implementation provides framework that can handle
973 scenarios. (Note that many simple cases are handled properly
974 without the predicate analysis -- this is due to jump threading
975 transformation which eliminates the merge point thus makes
976 path sensitive analysis unnecessary.)
978 NUM_PREDS is the number is the number predicate chains, PREDS is
979 the array of chains, PHI is the phi node whose incoming (undefined)
980 paths need to be pruned, and UNINIT_OPNDS is the bitmap holding
981 uninit operand positions. VISITED_PHIS is the pointer set of phi
982 stmts being checked. */
985 static bool
986 use_pred_not_overlap_with_undef_path_pred (
987 size_t num_preds,
988 vec<use_pred_info_t> *preds,
989 gimple phi, unsigned uninit_opnds,
990 struct pointer_set_t *visited_phis)
992 unsigned int i, n;
993 gimple flag_def = 0;
994 tree boundary_cst = 0;
995 enum tree_code cmp_code;
996 bool swap_cond = false;
997 bool invert = false;
998 vec<use_pred_info_t> the_pred_chain;
999 bitmap visited_flag_phis = NULL;
1000 bool all_pruned = false;
1002 gcc_assert (num_preds > 0);
1003 /* Find within the common prefix of multiple predicate chains
1004 a predicate that is a comparison of a flag variable against
1005 a constant. */
1006 the_pred_chain = preds[0];
1007 n = the_pred_chain.length ();
1008 for (i = 0; i < n; i++)
1010 gimple cond;
1011 tree cond_lhs, cond_rhs, flag = 0;
1013 use_pred_info_t the_pred
1014 = the_pred_chain[i];
1016 cond = the_pred->cond;
1017 invert = the_pred->invert;
1018 cond_lhs = gimple_cond_lhs (cond);
1019 cond_rhs = gimple_cond_rhs (cond);
1020 cmp_code = gimple_cond_code (cond);
1022 if (cond_lhs != NULL_TREE && TREE_CODE (cond_lhs) == SSA_NAME
1023 && cond_rhs != NULL_TREE && is_gimple_constant (cond_rhs))
1025 boundary_cst = cond_rhs;
1026 flag = cond_lhs;
1028 else if (cond_rhs != NULL_TREE && TREE_CODE (cond_rhs) == SSA_NAME
1029 && cond_lhs != NULL_TREE && is_gimple_constant (cond_lhs))
1031 boundary_cst = cond_lhs;
1032 flag = cond_rhs;
1033 swap_cond = true;
1036 if (!flag)
1037 continue;
1039 flag_def = SSA_NAME_DEF_STMT (flag);
1041 if (!flag_def)
1042 continue;
1044 if ((gimple_code (flag_def) == GIMPLE_PHI)
1045 && (gimple_bb (flag_def) == gimple_bb (phi))
1046 && find_matching_predicate_in_rest_chains (
1047 the_pred, preds, num_preds))
1048 break;
1050 flag_def = 0;
1053 if (!flag_def)
1054 return false;
1056 /* Now check all the uninit incoming edge has a constant flag value
1057 that is in conflict with the use guard/predicate. */
1058 cmp_code = get_cmp_code (cmp_code, swap_cond, invert);
1060 if (cmp_code == ERROR_MARK)
1061 return false;
1063 all_pruned = prune_uninit_phi_opnds_in_unrealizable_paths (phi,
1064 uninit_opnds,
1065 flag_def,
1066 boundary_cst,
1067 cmp_code,
1068 visited_phis,
1069 &visited_flag_phis);
1071 if (visited_flag_phis)
1072 BITMAP_FREE (visited_flag_phis);
1074 return all_pruned;
1077 /* Returns true if TC is AND or OR */
1079 static inline bool
1080 is_and_or_or (enum tree_code tc, tree typ)
1082 return (tc == BIT_IOR_EXPR
1083 || (tc == BIT_AND_EXPR
1084 && (typ == 0 || TREE_CODE (typ) == BOOLEAN_TYPE)));
1087 typedef struct norm_cond
1089 vec<gimple> conds;
1090 enum tree_code cond_code;
1091 bool invert;
1092 } *norm_cond_t;
1095 /* Normalizes gimple condition COND. The normalization follows
1096 UD chains to form larger condition expression trees. NORM_COND
1097 holds the normalized result. COND_CODE is the logical opcode
1098 (AND or OR) of the normalized tree. */
1100 static void
1101 normalize_cond_1 (gimple cond,
1102 norm_cond_t norm_cond,
1103 enum tree_code cond_code)
1105 enum gimple_code gc;
1106 enum tree_code cur_cond_code;
1107 tree rhs1, rhs2;
1109 gc = gimple_code (cond);
1110 if (gc != GIMPLE_ASSIGN)
1112 norm_cond->conds.safe_push (cond);
1113 return;
1116 cur_cond_code = gimple_assign_rhs_code (cond);
1117 rhs1 = gimple_assign_rhs1 (cond);
1118 rhs2 = gimple_assign_rhs2 (cond);
1119 if (cur_cond_code == NE_EXPR)
1121 if (integer_zerop (rhs2)
1122 && (TREE_CODE (rhs1) == SSA_NAME))
1123 normalize_cond_1 (
1124 SSA_NAME_DEF_STMT (rhs1),
1125 norm_cond, cond_code);
1126 else if (integer_zerop (rhs1)
1127 && (TREE_CODE (rhs2) == SSA_NAME))
1128 normalize_cond_1 (
1129 SSA_NAME_DEF_STMT (rhs2),
1130 norm_cond, cond_code);
1131 else
1132 norm_cond->conds.safe_push (cond);
1134 return;
1137 if (is_and_or_or (cur_cond_code, TREE_TYPE (rhs1))
1138 && (cond_code == cur_cond_code || cond_code == ERROR_MARK)
1139 && (TREE_CODE (rhs1) == SSA_NAME && TREE_CODE (rhs2) == SSA_NAME))
1141 normalize_cond_1 (SSA_NAME_DEF_STMT (rhs1),
1142 norm_cond, cur_cond_code);
1143 normalize_cond_1 (SSA_NAME_DEF_STMT (rhs2),
1144 norm_cond, cur_cond_code);
1145 norm_cond->cond_code = cur_cond_code;
1147 else
1148 norm_cond->conds.safe_push (cond);
1151 /* See normalize_cond_1 for details. INVERT is a flag to indicate
1152 if COND needs to be inverted or not. */
1154 static void
1155 normalize_cond (gimple cond, norm_cond_t norm_cond, bool invert)
1157 enum tree_code cond_code;
1159 norm_cond->cond_code = ERROR_MARK;
1160 norm_cond->invert = false;
1161 norm_cond->conds.create (0);
1162 gcc_assert (gimple_code (cond) == GIMPLE_COND);
1163 cond_code = gimple_cond_code (cond);
1164 if (invert)
1165 cond_code = invert_tree_comparison (cond_code, false);
1167 if (cond_code == NE_EXPR)
1169 if (integer_zerop (gimple_cond_rhs (cond))
1170 && (TREE_CODE (gimple_cond_lhs (cond)) == SSA_NAME))
1171 normalize_cond_1 (
1172 SSA_NAME_DEF_STMT (gimple_cond_lhs (cond)),
1173 norm_cond, ERROR_MARK);
1174 else if (integer_zerop (gimple_cond_lhs (cond))
1175 && (TREE_CODE (gimple_cond_rhs (cond)) == SSA_NAME))
1176 normalize_cond_1 (
1177 SSA_NAME_DEF_STMT (gimple_cond_rhs (cond)),
1178 norm_cond, ERROR_MARK);
1179 else
1181 norm_cond->conds.safe_push (cond);
1182 norm_cond->invert = invert;
1185 else
1187 norm_cond->conds.safe_push (cond);
1188 norm_cond->invert = invert;
1191 gcc_assert (norm_cond->conds.length () == 1
1192 || is_and_or_or (norm_cond->cond_code, NULL));
1195 /* Returns true if the domain for condition COND1 is a subset of
1196 COND2. REVERSE is a flag. when it is true the function checks
1197 if COND1 is a superset of COND2. INVERT1 and INVERT2 are flags
1198 to indicate if COND1 and COND2 need to be inverted or not. */
1200 static bool
1201 is_gcond_subset_of (gimple cond1, bool invert1,
1202 gimple cond2, bool invert2,
1203 bool reverse)
1205 enum gimple_code gc1, gc2;
1206 enum tree_code cond1_code, cond2_code;
1207 gimple tmp;
1208 tree cond1_lhs, cond1_rhs, cond2_lhs, cond2_rhs;
1210 /* Take the short cut. */
1211 if (cond1 == cond2)
1212 return true;
1214 if (reverse)
1216 tmp = cond1;
1217 cond1 = cond2;
1218 cond2 = tmp;
1221 gc1 = gimple_code (cond1);
1222 gc2 = gimple_code (cond2);
1224 if ((gc1 != GIMPLE_ASSIGN && gc1 != GIMPLE_COND)
1225 || (gc2 != GIMPLE_ASSIGN && gc2 != GIMPLE_COND))
1226 return cond1 == cond2;
1228 cond1_code = ((gc1 == GIMPLE_ASSIGN)
1229 ? gimple_assign_rhs_code (cond1)
1230 : gimple_cond_code (cond1));
1232 cond2_code = ((gc2 == GIMPLE_ASSIGN)
1233 ? gimple_assign_rhs_code (cond2)
1234 : gimple_cond_code (cond2));
1236 if (TREE_CODE_CLASS (cond1_code) != tcc_comparison
1237 || TREE_CODE_CLASS (cond2_code) != tcc_comparison)
1238 return false;
1240 if (invert1)
1241 cond1_code = invert_tree_comparison (cond1_code, false);
1242 if (invert2)
1243 cond2_code = invert_tree_comparison (cond2_code, false);
1245 cond1_lhs = ((gc1 == GIMPLE_ASSIGN)
1246 ? gimple_assign_rhs1 (cond1)
1247 : gimple_cond_lhs (cond1));
1248 cond1_rhs = ((gc1 == GIMPLE_ASSIGN)
1249 ? gimple_assign_rhs2 (cond1)
1250 : gimple_cond_rhs (cond1));
1251 cond2_lhs = ((gc2 == GIMPLE_ASSIGN)
1252 ? gimple_assign_rhs1 (cond2)
1253 : gimple_cond_lhs (cond2));
1254 cond2_rhs = ((gc2 == GIMPLE_ASSIGN)
1255 ? gimple_assign_rhs2 (cond2)
1256 : gimple_cond_rhs (cond2));
1258 /* Assuming const operands have been swapped to the
1259 rhs at this point of the analysis. */
1261 if (cond1_lhs != cond2_lhs)
1262 return false;
1264 if (!is_gimple_constant (cond1_rhs)
1265 || TREE_CODE (cond1_rhs) != INTEGER_CST)
1266 return (cond1_rhs == cond2_rhs);
1268 if (!is_gimple_constant (cond2_rhs)
1269 || TREE_CODE (cond2_rhs) != INTEGER_CST)
1270 return (cond1_rhs == cond2_rhs);
1272 if (cond1_code == EQ_EXPR)
1273 return is_value_included_in (cond1_rhs,
1274 cond2_rhs, cond2_code);
1275 if (cond1_code == NE_EXPR || cond2_code == EQ_EXPR)
1276 return ((cond2_code == cond1_code)
1277 && tree_int_cst_equal (cond1_rhs, cond2_rhs));
1279 if (((cond1_code == GE_EXPR || cond1_code == GT_EXPR)
1280 && (cond2_code == LE_EXPR || cond2_code == LT_EXPR))
1281 || ((cond1_code == LE_EXPR || cond1_code == LT_EXPR)
1282 && (cond2_code == GE_EXPR || cond2_code == GT_EXPR)))
1283 return false;
1285 if (cond1_code != GE_EXPR && cond1_code != GT_EXPR
1286 && cond1_code != LE_EXPR && cond1_code != LT_EXPR)
1287 return false;
1289 if (cond1_code == GT_EXPR)
1291 cond1_code = GE_EXPR;
1292 cond1_rhs = fold_binary (PLUS_EXPR, TREE_TYPE (cond1_rhs),
1293 cond1_rhs,
1294 fold_convert (TREE_TYPE (cond1_rhs),
1295 integer_one_node));
1297 else if (cond1_code == LT_EXPR)
1299 cond1_code = LE_EXPR;
1300 cond1_rhs = fold_binary (MINUS_EXPR, TREE_TYPE (cond1_rhs),
1301 cond1_rhs,
1302 fold_convert (TREE_TYPE (cond1_rhs),
1303 integer_one_node));
1306 if (!cond1_rhs)
1307 return false;
1309 gcc_assert (cond1_code == GE_EXPR || cond1_code == LE_EXPR);
1311 if (cond2_code == GE_EXPR || cond2_code == GT_EXPR ||
1312 cond2_code == LE_EXPR || cond2_code == LT_EXPR)
1313 return is_value_included_in (cond1_rhs,
1314 cond2_rhs, cond2_code);
1315 else if (cond2_code == NE_EXPR)
1316 return
1317 (is_value_included_in (cond1_rhs,
1318 cond2_rhs, cond2_code)
1319 && !is_value_included_in (cond2_rhs,
1320 cond1_rhs, cond1_code));
1321 return false;
1324 /* Returns true if the domain of the condition expression
1325 in COND is a subset of any of the sub-conditions
1326 of the normalized condtion NORM_COND. INVERT is a flag
1327 to indicate of the COND needs to be inverted.
1328 REVERSE is a flag. When it is true, the check is reversed --
1329 it returns true if COND is a superset of any of the subconditions
1330 of NORM_COND. */
1332 static bool
1333 is_subset_of_any (gimple cond, bool invert,
1334 norm_cond_t norm_cond, bool reverse)
1336 size_t i;
1337 size_t len = norm_cond->conds.length ();
1339 for (i = 0; i < len; i++)
1341 if (is_gcond_subset_of (cond, invert,
1342 norm_cond->conds[i],
1343 false, reverse))
1344 return true;
1346 return false;
1349 /* NORM_COND1 and NORM_COND2 are normalized logical/BIT OR
1350 expressions (formed by following UD chains not control
1351 dependence chains). The function returns true of domain
1352 of and expression NORM_COND1 is a subset of NORM_COND2's.
1353 The implementation is conservative, and it returns false if
1354 it the inclusion relationship may not hold. */
1356 static bool
1357 is_or_set_subset_of (norm_cond_t norm_cond1,
1358 norm_cond_t norm_cond2)
1360 size_t i;
1361 size_t len = norm_cond1->conds.length ();
1363 for (i = 0; i < len; i++)
1365 if (!is_subset_of_any (norm_cond1->conds[i],
1366 false, norm_cond2, false))
1367 return false;
1369 return true;
1372 /* NORM_COND1 and NORM_COND2 are normalized logical AND
1373 expressions (formed by following UD chains not control
1374 dependence chains). The function returns true of domain
1375 of and expression NORM_COND1 is a subset of NORM_COND2's. */
1377 static bool
1378 is_and_set_subset_of (norm_cond_t norm_cond1,
1379 norm_cond_t norm_cond2)
1381 size_t i;
1382 size_t len = norm_cond2->conds.length ();
1384 for (i = 0; i < len; i++)
1386 if (!is_subset_of_any (norm_cond2->conds[i],
1387 false, norm_cond1, true))
1388 return false;
1390 return true;
1393 /* Returns true of the domain if NORM_COND1 is a subset
1394 of that of NORM_COND2. Returns false if it can not be
1395 proved to be so. */
1397 static bool
1398 is_norm_cond_subset_of (norm_cond_t norm_cond1,
1399 norm_cond_t norm_cond2)
1401 size_t i;
1402 enum tree_code code1, code2;
1404 code1 = norm_cond1->cond_code;
1405 code2 = norm_cond2->cond_code;
1407 if (code1 == BIT_AND_EXPR)
1409 /* Both conditions are AND expressions. */
1410 if (code2 == BIT_AND_EXPR)
1411 return is_and_set_subset_of (norm_cond1, norm_cond2);
1412 /* NORM_COND1 is an AND expression, and NORM_COND2 is an OR
1413 expression. In this case, returns true if any subexpression
1414 of NORM_COND1 is a subset of any subexpression of NORM_COND2. */
1415 else if (code2 == BIT_IOR_EXPR)
1417 size_t len1;
1418 len1 = norm_cond1->conds.length ();
1419 for (i = 0; i < len1; i++)
1421 gimple cond1 = norm_cond1->conds[i];
1422 if (is_subset_of_any (cond1, false, norm_cond2, false))
1423 return true;
1425 return false;
1427 else
1429 gcc_assert (code2 == ERROR_MARK);
1430 gcc_assert (norm_cond2->conds.length () == 1);
1431 return is_subset_of_any (norm_cond2->conds[0],
1432 norm_cond2->invert, norm_cond1, true);
1435 /* NORM_COND1 is an OR expression */
1436 else if (code1 == BIT_IOR_EXPR)
1438 if (code2 != code1)
1439 return false;
1441 return is_or_set_subset_of (norm_cond1, norm_cond2);
1443 else
1445 gcc_assert (code1 == ERROR_MARK);
1446 gcc_assert (norm_cond1->conds.length () == 1);
1447 /* Conservatively returns false if NORM_COND1 is non-decomposible
1448 and NORM_COND2 is an AND expression. */
1449 if (code2 == BIT_AND_EXPR)
1450 return false;
1452 if (code2 == BIT_IOR_EXPR)
1453 return is_subset_of_any (norm_cond1->conds[0],
1454 norm_cond1->invert, norm_cond2, false);
1456 gcc_assert (code2 == ERROR_MARK);
1457 gcc_assert (norm_cond2->conds.length () == 1);
1458 return is_gcond_subset_of (norm_cond1->conds[0],
1459 norm_cond1->invert,
1460 norm_cond2->conds[0],
1461 norm_cond2->invert, false);
1465 /* Returns true of the domain of single predicate expression
1466 EXPR1 is a subset of that of EXPR2. Returns false if it
1467 can not be proved. */
1469 static bool
1470 is_pred_expr_subset_of (use_pred_info_t expr1,
1471 use_pred_info_t expr2)
1473 gimple cond1, cond2;
1474 enum tree_code code1, code2;
1475 struct norm_cond norm_cond1, norm_cond2;
1476 bool is_subset = false;
1478 cond1 = expr1->cond;
1479 cond2 = expr2->cond;
1480 code1 = gimple_cond_code (cond1);
1481 code2 = gimple_cond_code (cond2);
1483 if (expr1->invert)
1484 code1 = invert_tree_comparison (code1, false);
1485 if (expr2->invert)
1486 code2 = invert_tree_comparison (code2, false);
1488 /* Fast path -- match exactly */
1489 if ((gimple_cond_lhs (cond1) == gimple_cond_lhs (cond2))
1490 && (gimple_cond_rhs (cond1) == gimple_cond_rhs (cond2))
1491 && (code1 == code2))
1492 return true;
1494 /* Normalize conditions. To keep NE_EXPR, do not invert
1495 with both need inversion. */
1496 normalize_cond (cond1, &norm_cond1, (expr1->invert));
1497 normalize_cond (cond2, &norm_cond2, (expr2->invert));
1499 is_subset = is_norm_cond_subset_of (&norm_cond1, &norm_cond2);
1501 /* Free memory */
1502 norm_cond1.conds.release ();
1503 norm_cond2.conds.release ();
1504 return is_subset ;
1507 /* Returns true if the domain of PRED1 is a subset
1508 of that of PRED2. Returns false if it can not be proved so. */
1510 static bool
1511 is_pred_chain_subset_of (vec<use_pred_info_t> pred1,
1512 vec<use_pred_info_t> pred2)
1514 size_t np1, np2, i1, i2;
1516 np1 = pred1.length ();
1517 np2 = pred2.length ();
1519 for (i2 = 0; i2 < np2; i2++)
1521 bool found = false;
1522 use_pred_info_t info2
1523 = pred2[i2];
1524 for (i1 = 0; i1 < np1; i1++)
1526 use_pred_info_t info1
1527 = pred1[i1];
1528 if (is_pred_expr_subset_of (info1, info2))
1530 found = true;
1531 break;
1534 if (!found)
1535 return false;
1537 return true;
1540 /* Returns true if the domain defined by
1541 one pred chain ONE_PRED is a subset of the domain
1542 of *PREDS. It returns false if ONE_PRED's domain is
1543 not a subset of any of the sub-domains of PREDS (
1544 corresponding to each individual chains in it), even
1545 though it may be still be a subset of whole domain
1546 of PREDS which is the union (ORed) of all its subdomains.
1547 In other words, the result is conservative. */
1549 static bool
1550 is_included_in (vec<use_pred_info_t> one_pred,
1551 vec<use_pred_info_t> *preds,
1552 size_t n)
1554 size_t i;
1556 for (i = 0; i < n; i++)
1558 if (is_pred_chain_subset_of (one_pred, preds[i]))
1559 return true;
1562 return false;
1565 /* compares two predicate sets PREDS1 and PREDS2 and returns
1566 true if the domain defined by PREDS1 is a superset
1567 of PREDS2's domain. N1 and N2 are array sizes of PREDS1 and
1568 PREDS2 respectively. The implementation chooses not to build
1569 generic trees (and relying on the folding capability of the
1570 compiler), but instead performs brute force comparison of
1571 individual predicate chains (won't be a compile time problem
1572 as the chains are pretty short). When the function returns
1573 false, it does not necessarily mean *PREDS1 is not a superset
1574 of *PREDS2, but mean it may not be so since the analysis can
1575 not prove it. In such cases, false warnings may still be
1576 emitted. */
1578 static bool
1579 is_superset_of (vec<use_pred_info_t> *preds1,
1580 size_t n1,
1581 vec<use_pred_info_t> *preds2,
1582 size_t n2)
1584 size_t i;
1585 vec<use_pred_info_t> one_pred_chain;
1587 for (i = 0; i < n2; i++)
1589 one_pred_chain = preds2[i];
1590 if (!is_included_in (one_pred_chain, preds1, n1))
1591 return false;
1594 return true;
1597 /* Comparison function used by qsort. It is used to
1598 sort predicate chains to allow predicate
1599 simplification. */
1601 static int
1602 pred_chain_length_cmp (const void *p1, const void *p2)
1604 use_pred_info_t i1, i2;
1605 vec<use_pred_info_t> const *chain1
1606 = (vec<use_pred_info_t> const *)p1;
1607 vec<use_pred_info_t> const *chain2
1608 = (vec<use_pred_info_t> const *)p2;
1610 if (chain1->length () != chain2->length ())
1611 return (chain1->length () - chain2->length ());
1613 i1 = (*chain1)[0];
1614 i2 = (*chain2)[0];
1616 /* Allow predicates with similar prefix come together. */
1617 if (!i1->invert && i2->invert)
1618 return -1;
1619 else if (i1->invert && !i2->invert)
1620 return 1;
1622 return gimple_uid (i1->cond) - gimple_uid (i2->cond);
1625 /* x OR (!x AND y) is equivalent to x OR y.
1626 This function normalizes x1 OR (!x1 AND x2) OR (!x1 AND !x2 AND x3)
1627 into x1 OR x2 OR x3. PREDS is the predicate chains, and N is
1628 the number of chains. Returns true if normalization happens. */
1630 static bool
1631 normalize_preds (vec<use_pred_info_t> *preds, size_t *n)
1633 size_t i, j, ll;
1634 vec<use_pred_info_t> pred_chain;
1635 vec<use_pred_info_t> x = vNULL;
1636 use_pred_info_t xj = 0, nxj = 0;
1638 if (*n < 2)
1639 return false;
1641 /* First sort the chains in ascending order of lengths. */
1642 qsort (preds, *n, sizeof (void *), pred_chain_length_cmp);
1643 pred_chain = preds[0];
1644 ll = pred_chain.length ();
1645 if (ll != 1)
1647 if (ll == 2)
1649 use_pred_info_t xx, yy, xx2, nyy;
1650 vec<use_pred_info_t> pred_chain2 = preds[1];
1651 if (pred_chain2.length () != 2)
1652 return false;
1654 /* See if simplification x AND y OR x AND !y is possible. */
1655 xx = pred_chain[0];
1656 yy = pred_chain[1];
1657 xx2 = pred_chain2[0];
1658 nyy = pred_chain2[1];
1659 if (gimple_cond_lhs (xx->cond) != gimple_cond_lhs (xx2->cond)
1660 || gimple_cond_rhs (xx->cond) != gimple_cond_rhs (xx2->cond)
1661 || gimple_cond_code (xx->cond) != gimple_cond_code (xx2->cond)
1662 || (xx->invert != xx2->invert))
1663 return false;
1664 if (gimple_cond_lhs (yy->cond) != gimple_cond_lhs (nyy->cond)
1665 || gimple_cond_rhs (yy->cond) != gimple_cond_rhs (nyy->cond)
1666 || gimple_cond_code (yy->cond) != gimple_cond_code (nyy->cond)
1667 || (yy->invert == nyy->invert))
1668 return false;
1670 /* Now merge the first two chains. */
1671 free (yy);
1672 free (nyy);
1673 free (xx2);
1674 pred_chain.release ();
1675 pred_chain2.release ();
1676 pred_chain.safe_push (xx);
1677 preds[0] = pred_chain;
1678 for (i = 1; i < *n - 1; i++)
1679 preds[i] = preds[i + 1];
1681 preds[*n - 1].create (0);
1682 *n = *n - 1;
1684 else
1685 return false;
1688 x.safe_push (pred_chain[0]);
1690 /* The loop extracts x1, x2, x3, etc from chains
1691 x1 OR (!x1 AND x2) OR (!x1 AND !x2 AND x3) OR ... */
1692 for (i = 1; i < *n; i++)
1694 pred_chain = preds[i];
1695 if (pred_chain.length () != i + 1)
1696 return false;
1698 for (j = 0; j < i; j++)
1700 xj = x[j];
1701 nxj = pred_chain[j];
1703 /* Check if nxj is !xj */
1704 if (gimple_cond_lhs (xj->cond) != gimple_cond_lhs (nxj->cond)
1705 || gimple_cond_rhs (xj->cond) != gimple_cond_rhs (nxj->cond)
1706 || gimple_cond_code (xj->cond) != gimple_cond_code (nxj->cond)
1707 || (xj->invert == nxj->invert))
1708 return false;
1711 x.safe_push (pred_chain[i]);
1714 /* Now normalize the pred chains using the extraced x1, x2, x3 etc. */
1715 for (j = 0; j < *n; j++)
1717 use_pred_info_t t;
1718 xj = x[j];
1720 t = XNEW (struct use_pred_info);
1721 *t = *xj;
1723 x[j] = t;
1726 for (i = 0; i < *n; i++)
1728 pred_chain = preds[i];
1729 for (j = 0; j < pred_chain.length (); j++)
1730 free (pred_chain[j]);
1731 pred_chain.release ();
1732 /* A new chain. */
1733 pred_chain.safe_push (x[i]);
1734 preds[i] = pred_chain;
1736 return true;
1741 /* Computes the predicates that guard the use and checks
1742 if the incoming paths that have empty (or possibly
1743 empty) definition can be pruned/filtered. The function returns
1744 true if it can be determined that the use of PHI's def in
1745 USE_STMT is guarded with a predicate set not overlapping with
1746 predicate sets of all runtime paths that do not have a definition.
1747 Returns false if it is not or it can not be determined. USE_BB is
1748 the bb of the use (for phi operand use, the bb is not the bb of
1749 the phi stmt, but the src bb of the operand edge). UNINIT_OPNDS
1750 is a bit vector. If an operand of PHI is uninitialized, the
1751 corresponding bit in the vector is 1. VISIED_PHIS is a pointer
1752 set of phis being visted. */
1754 static bool
1755 is_use_properly_guarded (gimple use_stmt,
1756 basic_block use_bb,
1757 gimple phi,
1758 unsigned uninit_opnds,
1759 struct pointer_set_t *visited_phis)
1761 basic_block phi_bb;
1762 vec<use_pred_info_t> *preds = 0;
1763 vec<use_pred_info_t> *def_preds = 0;
1764 size_t num_preds = 0, num_def_preds = 0;
1765 bool has_valid_preds = false;
1766 bool is_properly_guarded = false;
1768 if (pointer_set_insert (visited_phis, phi))
1769 return false;
1771 phi_bb = gimple_bb (phi);
1773 if (is_non_loop_exit_postdominating (use_bb, phi_bb))
1774 return false;
1776 has_valid_preds = find_predicates (&preds, &num_preds,
1777 phi_bb, use_bb);
1779 if (!has_valid_preds)
1781 destroy_predicate_vecs (num_preds, preds);
1782 return false;
1785 if (dump_file)
1786 dump_predicates (use_stmt, num_preds, preds,
1787 "\nUse in stmt ");
1789 has_valid_preds = find_def_preds (&def_preds,
1790 &num_def_preds, phi);
1792 if (has_valid_preds)
1794 bool normed;
1795 if (dump_file)
1796 dump_predicates (phi, num_def_preds, def_preds,
1797 "Operand defs of phi ");
1799 normed = normalize_preds (def_preds, &num_def_preds);
1800 if (normed && dump_file)
1802 fprintf (dump_file, "\nNormalized to\n");
1803 dump_predicates (phi, num_def_preds, def_preds,
1804 "Operand defs of phi ");
1806 is_properly_guarded =
1807 is_superset_of (def_preds, num_def_preds,
1808 preds, num_preds);
1811 /* further prune the dead incoming phi edges. */
1812 if (!is_properly_guarded)
1813 is_properly_guarded
1814 = use_pred_not_overlap_with_undef_path_pred (
1815 num_preds, preds, phi, uninit_opnds, visited_phis);
1817 destroy_predicate_vecs (num_preds, preds);
1818 destroy_predicate_vecs (num_def_preds, def_preds);
1819 return is_properly_guarded;
1822 /* Searches through all uses of a potentially
1823 uninitialized variable defined by PHI and returns a use
1824 statement if the use is not properly guarded. It returns
1825 NULL if all uses are guarded. UNINIT_OPNDS is a bitvector
1826 holding the position(s) of uninit PHI operands. WORKLIST
1827 is the vector of candidate phis that may be updated by this
1828 function. ADDED_TO_WORKLIST is the pointer set tracking
1829 if the new phi is already in the worklist. */
1831 static gimple
1832 find_uninit_use (gimple phi, unsigned uninit_opnds,
1833 vec<gimple> *worklist,
1834 struct pointer_set_t *added_to_worklist)
1836 tree phi_result;
1837 use_operand_p use_p;
1838 gimple use_stmt;
1839 imm_use_iterator iter;
1841 phi_result = gimple_phi_result (phi);
1843 FOR_EACH_IMM_USE_FAST (use_p, iter, phi_result)
1845 struct pointer_set_t *visited_phis;
1846 basic_block use_bb;
1848 use_stmt = USE_STMT (use_p);
1849 if (is_gimple_debug (use_stmt))
1850 continue;
1852 visited_phis = pointer_set_create ();
1854 if (gimple_code (use_stmt) == GIMPLE_PHI)
1855 use_bb = gimple_phi_arg_edge (use_stmt,
1856 PHI_ARG_INDEX_FROM_USE (use_p))->src;
1857 else
1858 use_bb = gimple_bb (use_stmt);
1860 if (is_use_properly_guarded (use_stmt,
1861 use_bb,
1862 phi,
1863 uninit_opnds,
1864 visited_phis))
1866 pointer_set_destroy (visited_phis);
1867 continue;
1869 pointer_set_destroy (visited_phis);
1871 if (dump_file && (dump_flags & TDF_DETAILS))
1873 fprintf (dump_file, "[CHECK]: Found unguarded use: ");
1874 print_gimple_stmt (dump_file, use_stmt, 0, 0);
1876 /* Found one real use, return. */
1877 if (gimple_code (use_stmt) != GIMPLE_PHI)
1878 return use_stmt;
1880 /* Found a phi use that is not guarded,
1881 add the phi to the worklist. */
1882 if (!pointer_set_insert (added_to_worklist,
1883 use_stmt))
1885 if (dump_file && (dump_flags & TDF_DETAILS))
1887 fprintf (dump_file, "[WORKLIST]: Update worklist with phi: ");
1888 print_gimple_stmt (dump_file, use_stmt, 0, 0);
1891 worklist->safe_push (use_stmt);
1892 pointer_set_insert (possibly_undefined_names, phi_result);
1896 return NULL;
1899 /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions
1900 and gives warning if there exists a runtime path from the entry to a
1901 use of the PHI def that does not contain a definition. In other words,
1902 the warning is on the real use. The more dead paths that can be pruned
1903 by the compiler, the fewer false positives the warning is. WORKLIST
1904 is a vector of candidate phis to be examined. ADDED_TO_WORKLIST is
1905 a pointer set tracking if the new phi is added to the worklist or not. */
1907 static void
1908 warn_uninitialized_phi (gimple phi, vec<gimple> *worklist,
1909 struct pointer_set_t *added_to_worklist)
1911 unsigned uninit_opnds;
1912 gimple uninit_use_stmt = 0;
1913 tree uninit_op;
1915 /* Don't look at virtual operands. */
1916 if (virtual_operand_p (gimple_phi_result (phi)))
1917 return;
1919 uninit_opnds = compute_uninit_opnds_pos (phi);
1921 if (MASK_EMPTY (uninit_opnds))
1922 return;
1924 if (dump_file && (dump_flags & TDF_DETAILS))
1926 fprintf (dump_file, "[CHECK]: examining phi: ");
1927 print_gimple_stmt (dump_file, phi, 0, 0);
1930 /* Now check if we have any use of the value without proper guard. */
1931 uninit_use_stmt = find_uninit_use (phi, uninit_opnds,
1932 worklist, added_to_worklist);
1934 /* All uses are properly guarded. */
1935 if (!uninit_use_stmt)
1936 return;
1938 uninit_op = gimple_phi_arg_def (phi, MASK_FIRST_SET_BIT (uninit_opnds));
1939 if (SSA_NAME_VAR (uninit_op) == NULL_TREE)
1940 return;
1941 warn_uninit (OPT_Wmaybe_uninitialized, uninit_op, SSA_NAME_VAR (uninit_op),
1942 SSA_NAME_VAR (uninit_op),
1943 "%qD may be used uninitialized in this function",
1944 uninit_use_stmt);
1949 /* Entry point to the late uninitialized warning pass. */
1951 static unsigned int
1952 execute_late_warn_uninitialized (void)
1954 basic_block bb;
1955 gimple_stmt_iterator gsi;
1956 vec<gimple> worklist = vNULL;
1957 struct pointer_set_t *added_to_worklist;
1959 calculate_dominance_info (CDI_DOMINATORS);
1960 calculate_dominance_info (CDI_POST_DOMINATORS);
1961 /* Re-do the plain uninitialized variable check, as optimization may have
1962 straightened control flow. Do this first so that we don't accidentally
1963 get a "may be" warning when we'd have seen an "is" warning later. */
1964 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/1);
1966 timevar_push (TV_TREE_UNINIT);
1968 possibly_undefined_names = pointer_set_create ();
1969 added_to_worklist = pointer_set_create ();
1971 /* Initialize worklist */
1972 FOR_EACH_BB (bb)
1973 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1975 gimple phi = gsi_stmt (gsi);
1976 size_t n, i;
1978 n = gimple_phi_num_args (phi);
1980 /* Don't look at virtual operands. */
1981 if (virtual_operand_p (gimple_phi_result (phi)))
1982 continue;
1984 for (i = 0; i < n; ++i)
1986 tree op = gimple_phi_arg_def (phi, i);
1987 if (TREE_CODE (op) == SSA_NAME
1988 && ssa_undefined_value_p (op))
1990 worklist.safe_push (phi);
1991 pointer_set_insert (added_to_worklist, phi);
1992 if (dump_file && (dump_flags & TDF_DETAILS))
1994 fprintf (dump_file, "[WORKLIST]: add to initial list: ");
1995 print_gimple_stmt (dump_file, phi, 0, 0);
1997 break;
2002 while (worklist.length () != 0)
2004 gimple cur_phi = 0;
2005 cur_phi = worklist.pop ();
2006 warn_uninitialized_phi (cur_phi, &worklist, added_to_worklist);
2009 worklist.release ();
2010 pointer_set_destroy (added_to_worklist);
2011 pointer_set_destroy (possibly_undefined_names);
2012 possibly_undefined_names = NULL;
2013 free_dominance_info (CDI_POST_DOMINATORS);
2014 timevar_pop (TV_TREE_UNINIT);
2015 return 0;
2018 static bool
2019 gate_warn_uninitialized (void)
2021 return warn_uninitialized != 0;
2024 struct gimple_opt_pass pass_late_warn_uninitialized =
2027 GIMPLE_PASS,
2028 "uninit", /* name */
2029 OPTGROUP_NONE, /* optinfo_flags */
2030 gate_warn_uninitialized, /* gate */
2031 execute_late_warn_uninitialized, /* execute */
2032 NULL, /* sub */
2033 NULL, /* next */
2034 0, /* static_pass_number */
2035 TV_NONE, /* tv_id */
2036 PROP_ssa, /* properties_required */
2037 0, /* properties_provided */
2038 0, /* properties_destroyed */
2039 0, /* todo_flags_start */
2040 0 /* todo_flags_finish */