]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree-ssa-uninit.c
remove has_gate
[thirdparty/gcc.git] / gcc / tree-ssa-uninit.c
1 /* Predicate aware uninitialized variable warning.
2 Copyright (C) 2001-2014 Free Software Foundation, Inc.
3 Contributed by Xinliang David Li <davidxl@google.com>
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
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-ssa-alias.h"
34 #include "internal-fn.h"
35 #include "gimple-expr.h"
36 #include "is-a.h"
37 #include "gimple.h"
38 #include "gimple-iterator.h"
39 #include "gimple-ssa.h"
40 #include "tree-phinodes.h"
41 #include "ssa-iterators.h"
42 #include "tree-ssa.h"
43 #include "tree-inline.h"
44 #include "hashtab.h"
45 #include "tree-pass.h"
46 #include "diagnostic-core.h"
47 #include "params.h"
48
49 /* This implements the pass that does predicate aware warning on uses of
50 possibly uninitialized variables. The pass first collects the set of
51 possibly uninitialized SSA names. For each such name, it walks through
52 all its immediate uses. For each immediate use, it rebuilds the condition
53 expression (the predicate) that guards the use. The predicate is then
54 examined to see if the variable is always defined under that same condition.
55 This is done either by pruning the unrealizable paths that lead to the
56 default definitions or by checking if the predicate set that guards the
57 defining paths is a superset of the use predicate. */
58
59
60 /* Pointer set of potentially undefined ssa names, i.e.,
61 ssa names that are defined by phi with operands that
62 are not defined or potentially undefined. */
63 static pointer_set_t *possibly_undefined_names = 0;
64
65 /* Bit mask handling macros. */
66 #define MASK_SET_BIT(mask, pos) mask |= (1 << pos)
67 #define MASK_TEST_BIT(mask, pos) (mask & (1 << pos))
68 #define MASK_EMPTY(mask) (mask == 0)
69
70 /* Returns the first bit position (starting from LSB)
71 in mask that is non zero. Returns -1 if the mask is empty. */
72 static int
73 get_mask_first_set_bit (unsigned mask)
74 {
75 int pos = 0;
76 if (mask == 0)
77 return -1;
78
79 while ((mask & (1 << pos)) == 0)
80 pos++;
81
82 return pos;
83 }
84 #define MASK_FIRST_SET_BIT(mask) get_mask_first_set_bit (mask)
85
86 /* Return true if T, an SSA_NAME, has an undefined value. */
87 static bool
88 has_undefined_value_p (tree t)
89 {
90 return (ssa_undefined_value_p (t)
91 || (possibly_undefined_names
92 && pointer_set_contains (possibly_undefined_names, t)));
93 }
94
95
96
97 /* Like has_undefined_value_p, but don't return true if TREE_NO_WARNING
98 is set on SSA_NAME_VAR. */
99
100 static inline bool
101 uninit_undefined_value_p (tree t) {
102 if (!has_undefined_value_p (t))
103 return false;
104 if (SSA_NAME_VAR (t) && TREE_NO_WARNING (SSA_NAME_VAR (t)))
105 return false;
106 return true;
107 }
108
109 /* Emit warnings for uninitialized variables. This is done in two passes.
110
111 The first pass notices real uses of SSA names with undefined values.
112 Such uses are unconditionally uninitialized, and we can be certain that
113 such a use is a mistake. This pass is run before most optimizations,
114 so that we catch as many as we can.
115
116 The second pass follows PHI nodes to find uses that are potentially
117 uninitialized. In this case we can't necessarily prove that the use
118 is really uninitialized. This pass is run after most optimizations,
119 so that we thread as many jumps and possible, and delete as much dead
120 code as possible, in order to reduce false positives. We also look
121 again for plain uninitialized variables, since optimization may have
122 changed conditionally uninitialized to unconditionally uninitialized. */
123
124 /* Emit a warning for EXPR based on variable VAR at the point in the
125 program T, an SSA_NAME, is used being uninitialized. The exact
126 warning text is in MSGID and LOCUS may contain a location or be null.
127 WC is the warning code. */
128
129 static void
130 warn_uninit (enum opt_code wc, tree t,
131 tree expr, tree var, const char *gmsgid, void *data)
132 {
133 gimple context = (gimple) data;
134 location_t location, cfun_loc;
135 expanded_location xloc, floc;
136
137 if (!has_undefined_value_p (t))
138 return;
139
140 /* TREE_NO_WARNING either means we already warned, or the front end
141 wishes to suppress the warning. */
142 if ((context
143 && (gimple_no_warning_p (context)
144 || (gimple_assign_single_p (context)
145 && TREE_NO_WARNING (gimple_assign_rhs1 (context)))))
146 || TREE_NO_WARNING (expr))
147 return;
148
149 location = (context != NULL && gimple_has_location (context))
150 ? gimple_location (context)
151 : DECL_SOURCE_LOCATION (var);
152 location = linemap_resolve_location (line_table, location,
153 LRK_SPELLING_LOCATION,
154 NULL);
155 cfun_loc = DECL_SOURCE_LOCATION (cfun->decl);
156 xloc = expand_location (location);
157 floc = expand_location (cfun_loc);
158 if (warning_at (location, wc, gmsgid, expr))
159 {
160 TREE_NO_WARNING (expr) = 1;
161
162 if (location == DECL_SOURCE_LOCATION (var))
163 return;
164 if (xloc.file != floc.file
165 || linemap_location_before_p (line_table,
166 location, cfun_loc)
167 || linemap_location_before_p (line_table,
168 cfun->function_end_locus,
169 location))
170 inform (DECL_SOURCE_LOCATION (var), "%qD was declared here", var);
171 }
172 }
173
174 static unsigned int
175 warn_uninitialized_vars (bool warn_possibly_uninitialized)
176 {
177 gimple_stmt_iterator gsi;
178 basic_block bb;
179
180 FOR_EACH_BB_FN (bb, cfun)
181 {
182 bool always_executed = dominated_by_p (CDI_POST_DOMINATORS,
183 single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)), bb);
184 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
185 {
186 gimple stmt = gsi_stmt (gsi);
187 use_operand_p use_p;
188 ssa_op_iter op_iter;
189 tree use;
190
191 if (is_gimple_debug (stmt))
192 continue;
193
194 /* We only do data flow with SSA_NAMEs, so that's all we
195 can warn about. */
196 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, op_iter, SSA_OP_USE)
197 {
198 use = USE_FROM_PTR (use_p);
199 if (always_executed)
200 warn_uninit (OPT_Wuninitialized, use,
201 SSA_NAME_VAR (use), SSA_NAME_VAR (use),
202 "%qD is used uninitialized in this function",
203 stmt);
204 else if (warn_possibly_uninitialized)
205 warn_uninit (OPT_Wmaybe_uninitialized, use,
206 SSA_NAME_VAR (use), SSA_NAME_VAR (use),
207 "%qD may be used uninitialized in this function",
208 stmt);
209 }
210
211 /* For memory the only cheap thing we can do is see if we
212 have a use of the default def of the virtual operand.
213 ??? Not so cheap would be to use the alias oracle via
214 walk_aliased_vdefs, if we don't find any aliasing vdef
215 warn as is-used-uninitialized, if we don't find an aliasing
216 vdef that kills our use (stmt_kills_ref_p), warn as
217 may-be-used-uninitialized. But this walk is quadratic and
218 so must be limited which means we would miss warning
219 opportunities. */
220 use = gimple_vuse (stmt);
221 if (use
222 && gimple_assign_single_p (stmt)
223 && !gimple_vdef (stmt)
224 && SSA_NAME_IS_DEFAULT_DEF (use))
225 {
226 tree rhs = gimple_assign_rhs1 (stmt);
227 tree base = get_base_address (rhs);
228
229 /* Do not warn if it can be initialized outside this function. */
230 if (TREE_CODE (base) != VAR_DECL
231 || DECL_HARD_REGISTER (base)
232 || is_global_var (base))
233 continue;
234
235 if (always_executed)
236 warn_uninit (OPT_Wuninitialized, use,
237 gimple_assign_rhs1 (stmt), base,
238 "%qE is used uninitialized in this function",
239 stmt);
240 else if (warn_possibly_uninitialized)
241 warn_uninit (OPT_Wmaybe_uninitialized, use,
242 gimple_assign_rhs1 (stmt), base,
243 "%qE may be used uninitialized in this function",
244 stmt);
245 }
246 }
247 }
248
249 return 0;
250 }
251
252 /* Checks if the operand OPND of PHI is defined by
253 another phi with one operand defined by this PHI,
254 but the rest operands are all defined. If yes,
255 returns true to skip this this operand as being
256 redundant. Can be enhanced to be more general. */
257
258 static bool
259 can_skip_redundant_opnd (tree opnd, gimple phi)
260 {
261 gimple op_def;
262 tree phi_def;
263 int i, n;
264
265 phi_def = gimple_phi_result (phi);
266 op_def = SSA_NAME_DEF_STMT (opnd);
267 if (gimple_code (op_def) != GIMPLE_PHI)
268 return false;
269 n = gimple_phi_num_args (op_def);
270 for (i = 0; i < n; ++i)
271 {
272 tree op = gimple_phi_arg_def (op_def, i);
273 if (TREE_CODE (op) != SSA_NAME)
274 continue;
275 if (op != phi_def && uninit_undefined_value_p (op))
276 return false;
277 }
278
279 return true;
280 }
281
282 /* Returns a bit mask holding the positions of arguments in PHI
283 that have empty (or possibly empty) definitions. */
284
285 static unsigned
286 compute_uninit_opnds_pos (gimple phi)
287 {
288 size_t i, n;
289 unsigned uninit_opnds = 0;
290
291 n = gimple_phi_num_args (phi);
292 /* Bail out for phi with too many args. */
293 if (n > 32)
294 return 0;
295
296 for (i = 0; i < n; ++i)
297 {
298 tree op = gimple_phi_arg_def (phi, i);
299 if (TREE_CODE (op) == SSA_NAME
300 && uninit_undefined_value_p (op)
301 && !can_skip_redundant_opnd (op, phi))
302 {
303 if (cfun->has_nonlocal_label || cfun->calls_setjmp)
304 {
305 /* Ignore SSA_NAMEs that appear on abnormal edges
306 somewhere. */
307 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
308 continue;
309 }
310 MASK_SET_BIT (uninit_opnds, i);
311 }
312 }
313 return uninit_opnds;
314 }
315
316 /* Find the immediate postdominator PDOM of the specified
317 basic block BLOCK. */
318
319 static inline basic_block
320 find_pdom (basic_block block)
321 {
322 if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
323 return EXIT_BLOCK_PTR_FOR_FN (cfun);
324 else
325 {
326 basic_block bb
327 = get_immediate_dominator (CDI_POST_DOMINATORS, block);
328 if (! bb)
329 return EXIT_BLOCK_PTR_FOR_FN (cfun);
330 return bb;
331 }
332 }
333
334 /* Find the immediate DOM of the specified
335 basic block BLOCK. */
336
337 static inline basic_block
338 find_dom (basic_block block)
339 {
340 if (block == ENTRY_BLOCK_PTR_FOR_FN (cfun))
341 return ENTRY_BLOCK_PTR_FOR_FN (cfun);
342 else
343 {
344 basic_block bb = get_immediate_dominator (CDI_DOMINATORS, block);
345 if (! bb)
346 return ENTRY_BLOCK_PTR_FOR_FN (cfun);
347 return bb;
348 }
349 }
350
351 /* Returns true if BB1 is postdominating BB2 and BB1 is
352 not a loop exit bb. The loop exit bb check is simple and does
353 not cover all cases. */
354
355 static bool
356 is_non_loop_exit_postdominating (basic_block bb1, basic_block bb2)
357 {
358 if (!dominated_by_p (CDI_POST_DOMINATORS, bb2, bb1))
359 return false;
360
361 if (single_pred_p (bb1) && !single_succ_p (bb2))
362 return false;
363
364 return true;
365 }
366
367 /* Find the closest postdominator of a specified BB, which is control
368 equivalent to BB. */
369
370 static inline basic_block
371 find_control_equiv_block (basic_block bb)
372 {
373 basic_block pdom;
374
375 pdom = find_pdom (bb);
376
377 /* Skip the postdominating bb that is also loop exit. */
378 if (!is_non_loop_exit_postdominating (pdom, bb))
379 return NULL;
380
381 if (dominated_by_p (CDI_DOMINATORS, pdom, bb))
382 return pdom;
383
384 return NULL;
385 }
386
387 #define MAX_NUM_CHAINS 8
388 #define MAX_CHAIN_LEN 5
389 #define MAX_POSTDOM_CHECK 8
390
391 /* Computes the control dependence chains (paths of edges)
392 for DEP_BB up to the dominating basic block BB (the head node of a
393 chain should be dominated by it). CD_CHAINS is pointer to an
394 array holding the result chains. CUR_CD_CHAIN is the current
395 chain being computed. *NUM_CHAINS is total number of chains. The
396 function returns true if the information is successfully computed,
397 return false if there is no control dependence or not computed. */
398
399 static bool
400 compute_control_dep_chain (basic_block bb, basic_block dep_bb,
401 vec<edge> *cd_chains,
402 size_t *num_chains,
403 vec<edge> *cur_cd_chain,
404 int *num_calls)
405 {
406 edge_iterator ei;
407 edge e;
408 size_t i;
409 bool found_cd_chain = false;
410 size_t cur_chain_len = 0;
411
412 if (EDGE_COUNT (bb->succs) < 2)
413 return false;
414
415 if (*num_calls > PARAM_VALUE (PARAM_UNINIT_CONTROL_DEP_ATTEMPTS))
416 return false;
417 ++*num_calls;
418
419 /* Could use a set instead. */
420 cur_chain_len = cur_cd_chain->length ();
421 if (cur_chain_len > MAX_CHAIN_LEN)
422 return false;
423
424 for (i = 0; i < cur_chain_len; i++)
425 {
426 edge e = (*cur_cd_chain)[i];
427 /* Cycle detected. */
428 if (e->src == bb)
429 return false;
430 }
431
432 FOR_EACH_EDGE (e, ei, bb->succs)
433 {
434 basic_block cd_bb;
435 int post_dom_check = 0;
436 if (e->flags & (EDGE_FAKE | EDGE_ABNORMAL))
437 continue;
438
439 cd_bb = e->dest;
440 cur_cd_chain->safe_push (e);
441 while (!is_non_loop_exit_postdominating (cd_bb, bb))
442 {
443 if (cd_bb == dep_bb)
444 {
445 /* Found a direct control dependence. */
446 if (*num_chains < MAX_NUM_CHAINS)
447 {
448 cd_chains[*num_chains] = cur_cd_chain->copy ();
449 (*num_chains)++;
450 }
451 found_cd_chain = true;
452 /* Check path from next edge. */
453 break;
454 }
455
456 /* Now check if DEP_BB is indirectly control dependent on BB. */
457 if (compute_control_dep_chain (cd_bb, dep_bb, cd_chains,
458 num_chains, cur_cd_chain, num_calls))
459 {
460 found_cd_chain = true;
461 break;
462 }
463
464 cd_bb = find_pdom (cd_bb);
465 post_dom_check++;
466 if (cd_bb == EXIT_BLOCK_PTR_FOR_FN (cfun) || post_dom_check >
467 MAX_POSTDOM_CHECK)
468 break;
469 }
470 cur_cd_chain->pop ();
471 gcc_assert (cur_cd_chain->length () == cur_chain_len);
472 }
473 gcc_assert (cur_cd_chain->length () == cur_chain_len);
474
475 return found_cd_chain;
476 }
477
478 /* The type to represent a simple predicate */
479
480 typedef struct use_def_pred_info
481 {
482 tree pred_lhs;
483 tree pred_rhs;
484 enum tree_code cond_code;
485 bool invert;
486 } pred_info;
487
488 /* The type to represent a sequence of predicates grouped
489 with .AND. operation. */
490
491 typedef vec<pred_info, va_heap, vl_ptr> pred_chain;
492
493 /* The type to represent a sequence of pred_chains grouped
494 with .OR. operation. */
495
496 typedef vec<pred_chain, va_heap, vl_ptr> pred_chain_union;
497
498 /* Converts the chains of control dependence edges into a set of
499 predicates. A control dependence chain is represented by a vector
500 edges. DEP_CHAINS points to an array of dependence chains.
501 NUM_CHAINS is the size of the chain array. One edge in a dependence
502 chain is mapped to predicate expression represented by pred_info
503 type. One dependence chain is converted to a composite predicate that
504 is the result of AND operation of pred_info mapped to each edge.
505 A composite predicate is presented by a vector of pred_info. On
506 return, *PREDS points to the resulting array of composite predicates.
507 *NUM_PREDS is the number of composite predictes. */
508
509 static bool
510 convert_control_dep_chain_into_preds (vec<edge> *dep_chains,
511 size_t num_chains,
512 pred_chain_union *preds)
513 {
514 bool has_valid_pred = false;
515 size_t i, j;
516 if (num_chains == 0 || num_chains >= MAX_NUM_CHAINS)
517 return false;
518
519 /* Now convert the control dep chain into a set
520 of predicates. */
521 preds->reserve (num_chains);
522
523 for (i = 0; i < num_chains; i++)
524 {
525 vec<edge> one_cd_chain = dep_chains[i];
526
527 has_valid_pred = false;
528 pred_chain t_chain = vNULL;
529 for (j = 0; j < one_cd_chain.length (); j++)
530 {
531 gimple cond_stmt;
532 gimple_stmt_iterator gsi;
533 basic_block guard_bb;
534 pred_info one_pred;
535 edge e;
536
537 e = one_cd_chain[j];
538 guard_bb = e->src;
539 gsi = gsi_last_bb (guard_bb);
540 if (gsi_end_p (gsi))
541 {
542 has_valid_pred = false;
543 break;
544 }
545 cond_stmt = gsi_stmt (gsi);
546 if (is_gimple_call (cond_stmt)
547 && EDGE_COUNT (e->src->succs) >= 2)
548 {
549 /* Ignore EH edge. Can add assertion
550 on the other edge's flag. */
551 continue;
552 }
553 /* Skip if there is essentially one succesor. */
554 if (EDGE_COUNT (e->src->succs) == 2)
555 {
556 edge e1;
557 edge_iterator ei1;
558 bool skip = false;
559
560 FOR_EACH_EDGE (e1, ei1, e->src->succs)
561 {
562 if (EDGE_COUNT (e1->dest->succs) == 0)
563 {
564 skip = true;
565 break;
566 }
567 }
568 if (skip)
569 continue;
570 }
571 if (gimple_code (cond_stmt) != GIMPLE_COND)
572 {
573 has_valid_pred = false;
574 break;
575 }
576 one_pred.pred_lhs = gimple_cond_lhs (cond_stmt);
577 one_pred.pred_rhs = gimple_cond_rhs (cond_stmt);
578 one_pred.cond_code = gimple_cond_code (cond_stmt);
579 one_pred.invert = !!(e->flags & EDGE_FALSE_VALUE);
580 t_chain.safe_push (one_pred);
581 has_valid_pred = true;
582 }
583
584 if (!has_valid_pred)
585 break;
586 else
587 preds->safe_push (t_chain);
588 }
589 return has_valid_pred;
590 }
591
592 /* Computes all control dependence chains for USE_BB. The control
593 dependence chains are then converted to an array of composite
594 predicates pointed to by PREDS. PHI_BB is the basic block of
595 the phi whose result is used in USE_BB. */
596
597 static bool
598 find_predicates (pred_chain_union *preds,
599 basic_block phi_bb,
600 basic_block use_bb)
601 {
602 size_t num_chains = 0, i;
603 int num_calls = 0;
604 vec<edge> dep_chains[MAX_NUM_CHAINS];
605 auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
606 bool has_valid_pred = false;
607 basic_block cd_root = 0;
608
609 /* First find the closest bb that is control equivalent to PHI_BB
610 that also dominates USE_BB. */
611 cd_root = phi_bb;
612 while (dominated_by_p (CDI_DOMINATORS, use_bb, cd_root))
613 {
614 basic_block ctrl_eq_bb = find_control_equiv_block (cd_root);
615 if (ctrl_eq_bb && dominated_by_p (CDI_DOMINATORS, use_bb, ctrl_eq_bb))
616 cd_root = ctrl_eq_bb;
617 else
618 break;
619 }
620
621 compute_control_dep_chain (cd_root, use_bb, dep_chains, &num_chains,
622 &cur_chain, &num_calls);
623
624 has_valid_pred
625 = convert_control_dep_chain_into_preds (dep_chains, num_chains, preds);
626 for (i = 0; i < num_chains; i++)
627 dep_chains[i].release ();
628 return has_valid_pred;
629 }
630
631 /* Computes the set of incoming edges of PHI that have non empty
632 definitions of a phi chain. The collection will be done
633 recursively on operands that are defined by phis. CD_ROOT
634 is the control dependence root. *EDGES holds the result, and
635 VISITED_PHIS is a pointer set for detecting cycles. */
636
637 static void
638 collect_phi_def_edges (gimple phi, basic_block cd_root,
639 vec<edge> *edges,
640 pointer_set_t *visited_phis)
641 {
642 size_t i, n;
643 edge opnd_edge;
644 tree opnd;
645
646 if (pointer_set_insert (visited_phis, phi))
647 return;
648
649 n = gimple_phi_num_args (phi);
650 for (i = 0; i < n; i++)
651 {
652 opnd_edge = gimple_phi_arg_edge (phi, i);
653 opnd = gimple_phi_arg_def (phi, i);
654
655 if (TREE_CODE (opnd) != SSA_NAME)
656 {
657 if (dump_file && (dump_flags & TDF_DETAILS))
658 {
659 fprintf (dump_file, "\n[CHECK] Found def edge %d in ", (int)i);
660 print_gimple_stmt (dump_file, phi, 0, 0);
661 }
662 edges->safe_push (opnd_edge);
663 }
664 else
665 {
666 gimple def = SSA_NAME_DEF_STMT (opnd);
667
668 if (gimple_code (def) == GIMPLE_PHI
669 && dominated_by_p (CDI_DOMINATORS,
670 gimple_bb (def), cd_root))
671 collect_phi_def_edges (def, cd_root, edges,
672 visited_phis);
673 else if (!uninit_undefined_value_p (opnd))
674 {
675 if (dump_file && (dump_flags & TDF_DETAILS))
676 {
677 fprintf (dump_file, "\n[CHECK] Found def edge %d in ", (int)i);
678 print_gimple_stmt (dump_file, phi, 0, 0);
679 }
680 edges->safe_push (opnd_edge);
681 }
682 }
683 }
684 }
685
686 /* For each use edge of PHI, computes all control dependence chains.
687 The control dependence chains are then converted to an array of
688 composite predicates pointed to by PREDS. */
689
690 static bool
691 find_def_preds (pred_chain_union *preds, gimple phi)
692 {
693 size_t num_chains = 0, i, n;
694 vec<edge> dep_chains[MAX_NUM_CHAINS];
695 auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
696 vec<edge> def_edges = vNULL;
697 bool has_valid_pred = false;
698 basic_block phi_bb, cd_root = 0;
699 pointer_set_t *visited_phis;
700
701 phi_bb = gimple_bb (phi);
702 /* First find the closest dominating bb to be
703 the control dependence root */
704 cd_root = find_dom (phi_bb);
705 if (!cd_root)
706 return false;
707
708 visited_phis = pointer_set_create ();
709 collect_phi_def_edges (phi, cd_root, &def_edges, visited_phis);
710 pointer_set_destroy (visited_phis);
711
712 n = def_edges.length ();
713 if (n == 0)
714 return false;
715
716 for (i = 0; i < n; i++)
717 {
718 size_t prev_nc, j;
719 int num_calls = 0;
720 edge opnd_edge;
721
722 opnd_edge = def_edges[i];
723 prev_nc = num_chains;
724 compute_control_dep_chain (cd_root, opnd_edge->src, dep_chains,
725 &num_chains, &cur_chain, &num_calls);
726
727 /* Now update the newly added chains with
728 the phi operand edge: */
729 if (EDGE_COUNT (opnd_edge->src->succs) > 1)
730 {
731 if (prev_nc == num_chains && num_chains < MAX_NUM_CHAINS)
732 dep_chains[num_chains++] = vNULL;
733 for (j = prev_nc; j < num_chains; j++)
734 dep_chains[j].safe_push (opnd_edge);
735 }
736 }
737
738 has_valid_pred
739 = convert_control_dep_chain_into_preds (dep_chains, num_chains, preds);
740 for (i = 0; i < num_chains; i++)
741 dep_chains[i].release ();
742 return has_valid_pred;
743 }
744
745 /* Dumps the predicates (PREDS) for USESTMT. */
746
747 static void
748 dump_predicates (gimple usestmt, pred_chain_union preds,
749 const char* msg)
750 {
751 size_t i, j;
752 pred_chain one_pred_chain = vNULL;
753 fprintf (dump_file, msg);
754 print_gimple_stmt (dump_file, usestmt, 0, 0);
755 fprintf (dump_file, "is guarded by :\n\n");
756 size_t num_preds = preds.length ();
757 /* Do some dumping here: */
758 for (i = 0; i < num_preds; i++)
759 {
760 size_t np;
761
762 one_pred_chain = preds[i];
763 np = one_pred_chain.length ();
764
765 for (j = 0; j < np; j++)
766 {
767 pred_info one_pred = one_pred_chain[j];
768 if (one_pred.invert)
769 fprintf (dump_file, " (.NOT.) ");
770 print_generic_expr (dump_file, one_pred.pred_lhs, 0);
771 fprintf (dump_file, " %s ", op_symbol_code (one_pred.cond_code));
772 print_generic_expr (dump_file, one_pred.pred_rhs, 0);
773 if (j < np - 1)
774 fprintf (dump_file, " (.AND.) ");
775 else
776 fprintf (dump_file, "\n");
777 }
778 if (i < num_preds - 1)
779 fprintf (dump_file, "(.OR.)\n");
780 else
781 fprintf (dump_file, "\n\n");
782 }
783 }
784
785 /* Destroys the predicate set *PREDS. */
786
787 static void
788 destroy_predicate_vecs (pred_chain_union preds)
789 {
790 size_t i;
791
792 size_t n = preds.length ();
793 for (i = 0; i < n; i++)
794 preds[i].release ();
795 preds.release ();
796 }
797
798
799 /* Computes the 'normalized' conditional code with operand
800 swapping and condition inversion. */
801
802 static enum tree_code
803 get_cmp_code (enum tree_code orig_cmp_code,
804 bool swap_cond, bool invert)
805 {
806 enum tree_code tc = orig_cmp_code;
807
808 if (swap_cond)
809 tc = swap_tree_comparison (orig_cmp_code);
810 if (invert)
811 tc = invert_tree_comparison (tc, false);
812
813 switch (tc)
814 {
815 case LT_EXPR:
816 case LE_EXPR:
817 case GT_EXPR:
818 case GE_EXPR:
819 case EQ_EXPR:
820 case NE_EXPR:
821 break;
822 default:
823 return ERROR_MARK;
824 }
825 return tc;
826 }
827
828 /* Returns true if VAL falls in the range defined by BOUNDARY and CMPC, i.e.
829 all values in the range satisfies (x CMPC BOUNDARY) == true. */
830
831 static bool
832 is_value_included_in (tree val, tree boundary, enum tree_code cmpc)
833 {
834 bool inverted = false;
835 bool is_unsigned;
836 bool result;
837
838 /* Only handle integer constant here. */
839 if (TREE_CODE (val) != INTEGER_CST
840 || TREE_CODE (boundary) != INTEGER_CST)
841 return true;
842
843 is_unsigned = TYPE_UNSIGNED (TREE_TYPE (val));
844
845 if (cmpc == GE_EXPR || cmpc == GT_EXPR
846 || cmpc == NE_EXPR)
847 {
848 cmpc = invert_tree_comparison (cmpc, false);
849 inverted = true;
850 }
851
852 if (is_unsigned)
853 {
854 if (cmpc == EQ_EXPR)
855 result = tree_int_cst_equal (val, boundary);
856 else if (cmpc == LT_EXPR)
857 result = INT_CST_LT_UNSIGNED (val, boundary);
858 else
859 {
860 gcc_assert (cmpc == LE_EXPR);
861 result = (tree_int_cst_equal (val, boundary)
862 || INT_CST_LT_UNSIGNED (val, boundary));
863 }
864 }
865 else
866 {
867 if (cmpc == EQ_EXPR)
868 result = tree_int_cst_equal (val, boundary);
869 else if (cmpc == LT_EXPR)
870 result = INT_CST_LT (val, boundary);
871 else
872 {
873 gcc_assert (cmpc == LE_EXPR);
874 result = (tree_int_cst_equal (val, boundary)
875 || INT_CST_LT (val, boundary));
876 }
877 }
878
879 if (inverted)
880 result ^= 1;
881
882 return result;
883 }
884
885 /* Returns true if PRED is common among all the predicate
886 chains (PREDS) (and therefore can be factored out).
887 NUM_PRED_CHAIN is the size of array PREDS. */
888
889 static bool
890 find_matching_predicate_in_rest_chains (pred_info pred,
891 pred_chain_union preds,
892 size_t num_pred_chains)
893 {
894 size_t i, j, n;
895
896 /* Trival case. */
897 if (num_pred_chains == 1)
898 return true;
899
900 for (i = 1; i < num_pred_chains; i++)
901 {
902 bool found = false;
903 pred_chain one_chain = preds[i];
904 n = one_chain.length ();
905 for (j = 0; j < n; j++)
906 {
907 pred_info pred2 = one_chain[j];
908 /* Can relax the condition comparison to not
909 use address comparison. However, the most common
910 case is that multiple control dependent paths share
911 a common path prefix, so address comparison should
912 be ok. */
913
914 if (operand_equal_p (pred2.pred_lhs, pred.pred_lhs, 0)
915 && operand_equal_p (pred2.pred_rhs, pred.pred_rhs, 0)
916 && pred2.invert == pred.invert)
917 {
918 found = true;
919 break;
920 }
921 }
922 if (!found)
923 return false;
924 }
925 return true;
926 }
927
928 /* Forward declaration. */
929 static bool
930 is_use_properly_guarded (gimple use_stmt,
931 basic_block use_bb,
932 gimple phi,
933 unsigned uninit_opnds,
934 pointer_set_t *visited_phis);
935
936 /* Returns true if all uninitialized opnds are pruned. Returns false
937 otherwise. PHI is the phi node with uninitialized operands,
938 UNINIT_OPNDS is the bitmap of the uninitialize operand positions,
939 FLAG_DEF is the statement defining the flag guarding the use of the
940 PHI output, BOUNDARY_CST is the const value used in the predicate
941 associated with the flag, CMP_CODE is the comparison code used in
942 the predicate, VISITED_PHIS is the pointer set of phis visited, and
943 VISITED_FLAG_PHIS is the pointer to the pointer set of flag definitions
944 that are also phis.
945
946 Example scenario:
947
948 BB1:
949 flag_1 = phi <0, 1> // (1)
950 var_1 = phi <undef, some_val>
951
952
953 BB2:
954 flag_2 = phi <0, flag_1, flag_1> // (2)
955 var_2 = phi <undef, var_1, var_1>
956 if (flag_2 == 1)
957 goto BB3;
958
959 BB3:
960 use of var_2 // (3)
961
962 Because some flag arg in (1) is not constant, if we do not look into the
963 flag phis recursively, it is conservatively treated as unknown and var_1
964 is thought to be flowed into use at (3). Since var_1 is potentially uninitialized
965 a false warning will be emitted. Checking recursively into (1), the compiler can
966 find out that only some_val (which is defined) can flow into (3) which is OK.
967
968 */
969
970 static bool
971 prune_uninit_phi_opnds_in_unrealizable_paths (gimple phi,
972 unsigned uninit_opnds,
973 gimple flag_def,
974 tree boundary_cst,
975 enum tree_code cmp_code,
976 pointer_set_t *visited_phis,
977 bitmap *visited_flag_phis)
978 {
979 unsigned i;
980
981 for (i = 0; i < MIN (32, gimple_phi_num_args (flag_def)); i++)
982 {
983 tree flag_arg;
984
985 if (!MASK_TEST_BIT (uninit_opnds, i))
986 continue;
987
988 flag_arg = gimple_phi_arg_def (flag_def, i);
989 if (!is_gimple_constant (flag_arg))
990 {
991 gimple flag_arg_def, phi_arg_def;
992 tree phi_arg;
993 unsigned uninit_opnds_arg_phi;
994
995 if (TREE_CODE (flag_arg) != SSA_NAME)
996 return false;
997 flag_arg_def = SSA_NAME_DEF_STMT (flag_arg);
998 if (gimple_code (flag_arg_def) != GIMPLE_PHI)
999 return false;
1000
1001 phi_arg = gimple_phi_arg_def (phi, i);
1002 if (TREE_CODE (phi_arg) != SSA_NAME)
1003 return false;
1004
1005 phi_arg_def = SSA_NAME_DEF_STMT (phi_arg);
1006 if (gimple_code (phi_arg_def) != GIMPLE_PHI)
1007 return false;
1008
1009 if (gimple_bb (phi_arg_def) != gimple_bb (flag_arg_def))
1010 return false;
1011
1012 if (!*visited_flag_phis)
1013 *visited_flag_phis = BITMAP_ALLOC (NULL);
1014
1015 if (bitmap_bit_p (*visited_flag_phis,
1016 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def))))
1017 return false;
1018
1019 bitmap_set_bit (*visited_flag_phis,
1020 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def)));
1021
1022 /* Now recursively prune the uninitialized phi args. */
1023 uninit_opnds_arg_phi = compute_uninit_opnds_pos (phi_arg_def);
1024 if (!prune_uninit_phi_opnds_in_unrealizable_paths
1025 (phi_arg_def, uninit_opnds_arg_phi, flag_arg_def,
1026 boundary_cst, cmp_code, visited_phis, visited_flag_phis))
1027 return false;
1028
1029 bitmap_clear_bit (*visited_flag_phis,
1030 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def)));
1031 continue;
1032 }
1033
1034 /* Now check if the constant is in the guarded range. */
1035 if (is_value_included_in (flag_arg, boundary_cst, cmp_code))
1036 {
1037 tree opnd;
1038 gimple opnd_def;
1039
1040 /* Now that we know that this undefined edge is not
1041 pruned. If the operand is defined by another phi,
1042 we can further prune the incoming edges of that
1043 phi by checking the predicates of this operands. */
1044
1045 opnd = gimple_phi_arg_def (phi, i);
1046 opnd_def = SSA_NAME_DEF_STMT (opnd);
1047 if (gimple_code (opnd_def) == GIMPLE_PHI)
1048 {
1049 edge opnd_edge;
1050 unsigned uninit_opnds2
1051 = compute_uninit_opnds_pos (opnd_def);
1052 gcc_assert (!MASK_EMPTY (uninit_opnds2));
1053 opnd_edge = gimple_phi_arg_edge (phi, i);
1054 if (!is_use_properly_guarded (phi,
1055 opnd_edge->src,
1056 opnd_def,
1057 uninit_opnds2,
1058 visited_phis))
1059 return false;
1060 }
1061 else
1062 return false;
1063 }
1064 }
1065
1066 return true;
1067 }
1068
1069 /* A helper function that determines if the predicate set
1070 of the use is not overlapping with that of the uninit paths.
1071 The most common senario of guarded use is in Example 1:
1072 Example 1:
1073 if (some_cond)
1074 {
1075 x = ...;
1076 flag = true;
1077 }
1078
1079 ... some code ...
1080
1081 if (flag)
1082 use (x);
1083
1084 The real world examples are usually more complicated, but similar
1085 and usually result from inlining:
1086
1087 bool init_func (int * x)
1088 {
1089 if (some_cond)
1090 return false;
1091 *x = ..
1092 return true;
1093 }
1094
1095 void foo(..)
1096 {
1097 int x;
1098
1099 if (!init_func(&x))
1100 return;
1101
1102 .. some_code ...
1103 use (x);
1104 }
1105
1106 Another possible use scenario is in the following trivial example:
1107
1108 Example 2:
1109 if (n > 0)
1110 x = 1;
1111 ...
1112 if (n > 0)
1113 {
1114 if (m < 2)
1115 .. = x;
1116 }
1117
1118 Predicate analysis needs to compute the composite predicate:
1119
1120 1) 'x' use predicate: (n > 0) .AND. (m < 2)
1121 2) 'x' default value (non-def) predicate: .NOT. (n > 0)
1122 (the predicate chain for phi operand defs can be computed
1123 starting from a bb that is control equivalent to the phi's
1124 bb and is dominating the operand def.)
1125
1126 and check overlapping:
1127 (n > 0) .AND. (m < 2) .AND. (.NOT. (n > 0))
1128 <==> false
1129
1130 This implementation provides framework that can handle
1131 scenarios. (Note that many simple cases are handled properly
1132 without the predicate analysis -- this is due to jump threading
1133 transformation which eliminates the merge point thus makes
1134 path sensitive analysis unnecessary.)
1135
1136 NUM_PREDS is the number is the number predicate chains, PREDS is
1137 the array of chains, PHI is the phi node whose incoming (undefined)
1138 paths need to be pruned, and UNINIT_OPNDS is the bitmap holding
1139 uninit operand positions. VISITED_PHIS is the pointer set of phi
1140 stmts being checked. */
1141
1142
1143 static bool
1144 use_pred_not_overlap_with_undef_path_pred (pred_chain_union preds,
1145 gimple phi, unsigned uninit_opnds,
1146 pointer_set_t *visited_phis)
1147 {
1148 unsigned int i, n;
1149 gimple flag_def = 0;
1150 tree boundary_cst = 0;
1151 enum tree_code cmp_code;
1152 bool swap_cond = false;
1153 bool invert = false;
1154 pred_chain the_pred_chain = vNULL;
1155 bitmap visited_flag_phis = NULL;
1156 bool all_pruned = false;
1157 size_t num_preds = preds.length ();
1158
1159 gcc_assert (num_preds > 0);
1160 /* Find within the common prefix of multiple predicate chains
1161 a predicate that is a comparison of a flag variable against
1162 a constant. */
1163 the_pred_chain = preds[0];
1164 n = the_pred_chain.length ();
1165 for (i = 0; i < n; i++)
1166 {
1167 tree cond_lhs, cond_rhs, flag = 0;
1168
1169 pred_info the_pred = the_pred_chain[i];
1170
1171 invert = the_pred.invert;
1172 cond_lhs = the_pred.pred_lhs;
1173 cond_rhs = the_pred.pred_rhs;
1174 cmp_code = the_pred.cond_code;
1175
1176 if (cond_lhs != NULL_TREE && TREE_CODE (cond_lhs) == SSA_NAME
1177 && cond_rhs != NULL_TREE && is_gimple_constant (cond_rhs))
1178 {
1179 boundary_cst = cond_rhs;
1180 flag = cond_lhs;
1181 }
1182 else if (cond_rhs != NULL_TREE && TREE_CODE (cond_rhs) == SSA_NAME
1183 && cond_lhs != NULL_TREE && is_gimple_constant (cond_lhs))
1184 {
1185 boundary_cst = cond_lhs;
1186 flag = cond_rhs;
1187 swap_cond = true;
1188 }
1189
1190 if (!flag)
1191 continue;
1192
1193 flag_def = SSA_NAME_DEF_STMT (flag);
1194
1195 if (!flag_def)
1196 continue;
1197
1198 if ((gimple_code (flag_def) == GIMPLE_PHI)
1199 && (gimple_bb (flag_def) == gimple_bb (phi))
1200 && find_matching_predicate_in_rest_chains (the_pred, preds,
1201 num_preds))
1202 break;
1203
1204 flag_def = 0;
1205 }
1206
1207 if (!flag_def)
1208 return false;
1209
1210 /* Now check all the uninit incoming edge has a constant flag value
1211 that is in conflict with the use guard/predicate. */
1212 cmp_code = get_cmp_code (cmp_code, swap_cond, invert);
1213
1214 if (cmp_code == ERROR_MARK)
1215 return false;
1216
1217 all_pruned = prune_uninit_phi_opnds_in_unrealizable_paths (phi,
1218 uninit_opnds,
1219 flag_def,
1220 boundary_cst,
1221 cmp_code,
1222 visited_phis,
1223 &visited_flag_phis);
1224
1225 if (visited_flag_phis)
1226 BITMAP_FREE (visited_flag_phis);
1227
1228 return all_pruned;
1229 }
1230
1231 /* The helper function returns true if two predicates X1 and X2
1232 are equivalent. It assumes the expressions have already
1233 properly re-associated. */
1234
1235 static inline bool
1236 pred_equal_p (pred_info x1, pred_info x2)
1237 {
1238 enum tree_code c1, c2;
1239 if (!operand_equal_p (x1.pred_lhs, x2.pred_lhs, 0)
1240 || !operand_equal_p (x1.pred_rhs, x2.pred_rhs, 0))
1241 return false;
1242
1243 c1 = x1.cond_code;
1244 if (x1.invert != x2.invert)
1245 c2 = invert_tree_comparison (x2.cond_code, false);
1246 else
1247 c2 = x2.cond_code;
1248
1249 return c1 == c2;
1250 }
1251
1252 /* Returns true if the predication is testing !=. */
1253
1254 static inline bool
1255 is_neq_relop_p (pred_info pred)
1256 {
1257
1258 return (pred.cond_code == NE_EXPR && !pred.invert)
1259 || (pred.cond_code == EQ_EXPR && pred.invert);
1260 }
1261
1262 /* Returns true if pred is of the form X != 0. */
1263
1264 static inline bool
1265 is_neq_zero_form_p (pred_info pred)
1266 {
1267 if (!is_neq_relop_p (pred) || !integer_zerop (pred.pred_rhs)
1268 || TREE_CODE (pred.pred_lhs) != SSA_NAME)
1269 return false;
1270 return true;
1271 }
1272
1273 /* The helper function returns true if two predicates X1
1274 is equivalent to X2 != 0. */
1275
1276 static inline bool
1277 pred_expr_equal_p (pred_info x1, tree x2)
1278 {
1279 if (!is_neq_zero_form_p (x1))
1280 return false;
1281
1282 return operand_equal_p (x1.pred_lhs, x2, 0);
1283 }
1284
1285 /* Returns true of the domain of single predicate expression
1286 EXPR1 is a subset of that of EXPR2. Returns false if it
1287 can not be proved. */
1288
1289 static bool
1290 is_pred_expr_subset_of (pred_info expr1, pred_info expr2)
1291 {
1292 enum tree_code code1, code2;
1293
1294 if (pred_equal_p (expr1, expr2))
1295 return true;
1296
1297 if ((TREE_CODE (expr1.pred_rhs) != INTEGER_CST)
1298 || (TREE_CODE (expr2.pred_rhs) != INTEGER_CST))
1299 return false;
1300
1301 if (!operand_equal_p (expr1.pred_lhs, expr2.pred_lhs, 0))
1302 return false;
1303
1304 code1 = expr1.cond_code;
1305 if (expr1.invert)
1306 code1 = invert_tree_comparison (code1, false);
1307 code2 = expr2.cond_code;
1308 if (expr2.invert)
1309 code2 = invert_tree_comparison (code2, false);
1310
1311 if (code1 != code2 && code2 != NE_EXPR)
1312 return false;
1313
1314 if (is_value_included_in (expr1.pred_rhs, expr2.pred_rhs, code2))
1315 return true;
1316
1317 return false;
1318 }
1319
1320 /* Returns true if the domain of PRED1 is a subset
1321 of that of PRED2. Returns false if it can not be proved so. */
1322
1323 static bool
1324 is_pred_chain_subset_of (pred_chain pred1,
1325 pred_chain pred2)
1326 {
1327 size_t np1, np2, i1, i2;
1328
1329 np1 = pred1.length ();
1330 np2 = pred2.length ();
1331
1332 for (i2 = 0; i2 < np2; i2++)
1333 {
1334 bool found = false;
1335 pred_info info2 = pred2[i2];
1336 for (i1 = 0; i1 < np1; i1++)
1337 {
1338 pred_info info1 = pred1[i1];
1339 if (is_pred_expr_subset_of (info1, info2))
1340 {
1341 found = true;
1342 break;
1343 }
1344 }
1345 if (!found)
1346 return false;
1347 }
1348 return true;
1349 }
1350
1351 /* Returns true if the domain defined by
1352 one pred chain ONE_PRED is a subset of the domain
1353 of *PREDS. It returns false if ONE_PRED's domain is
1354 not a subset of any of the sub-domains of PREDS
1355 (corresponding to each individual chains in it), even
1356 though it may be still be a subset of whole domain
1357 of PREDS which is the union (ORed) of all its subdomains.
1358 In other words, the result is conservative. */
1359
1360 static bool
1361 is_included_in (pred_chain one_pred, pred_chain_union preds)
1362 {
1363 size_t i;
1364 size_t n = preds.length ();
1365
1366 for (i = 0; i < n; i++)
1367 {
1368 if (is_pred_chain_subset_of (one_pred, preds[i]))
1369 return true;
1370 }
1371
1372 return false;
1373 }
1374
1375 /* Compares two predicate sets PREDS1 and PREDS2 and returns
1376 true if the domain defined by PREDS1 is a superset
1377 of PREDS2's domain. N1 and N2 are array sizes of PREDS1 and
1378 PREDS2 respectively. The implementation chooses not to build
1379 generic trees (and relying on the folding capability of the
1380 compiler), but instead performs brute force comparison of
1381 individual predicate chains (won't be a compile time problem
1382 as the chains are pretty short). When the function returns
1383 false, it does not necessarily mean *PREDS1 is not a superset
1384 of *PREDS2, but mean it may not be so since the analysis can
1385 not prove it. In such cases, false warnings may still be
1386 emitted. */
1387
1388 static bool
1389 is_superset_of (pred_chain_union preds1, pred_chain_union preds2)
1390 {
1391 size_t i, n2;
1392 pred_chain one_pred_chain = vNULL;
1393
1394 n2 = preds2.length ();
1395
1396 for (i = 0; i < n2; i++)
1397 {
1398 one_pred_chain = preds2[i];
1399 if (!is_included_in (one_pred_chain, preds1))
1400 return false;
1401 }
1402
1403 return true;
1404 }
1405
1406 /* Returns true if TC is AND or OR. */
1407
1408 static inline bool
1409 is_and_or_or_p (enum tree_code tc, tree type)
1410 {
1411 return (tc == BIT_IOR_EXPR
1412 || (tc == BIT_AND_EXPR
1413 && (type == 0 || TREE_CODE (type) == BOOLEAN_TYPE)));
1414 }
1415
1416 /* Returns true if X1 is the negate of X2. */
1417
1418 static inline bool
1419 pred_neg_p (pred_info x1, pred_info x2)
1420 {
1421 enum tree_code c1, c2;
1422 if (!operand_equal_p (x1.pred_lhs, x2.pred_lhs, 0)
1423 || !operand_equal_p (x1.pred_rhs, x2.pred_rhs, 0))
1424 return false;
1425
1426 c1 = x1.cond_code;
1427 if (x1.invert == x2.invert)
1428 c2 = invert_tree_comparison (x2.cond_code, false);
1429 else
1430 c2 = x2.cond_code;
1431
1432 return c1 == c2;
1433 }
1434
1435 /* 1) ((x IOR y) != 0) AND (x != 0) is equivalent to (x != 0);
1436 2) (X AND Y) OR (!X AND Y) is equivalent to Y;
1437 3) X OR (!X AND Y) is equivalent to (X OR Y);
1438 4) ((x IAND y) != 0) || (x != 0 AND y != 0)) is equivalent to
1439 (x != 0 AND y != 0)
1440 5) (X AND Y) OR (!X AND Z) OR (!Y AND Z) is equivalent to
1441 (X AND Y) OR Z
1442
1443 PREDS is the predicate chains, and N is the number of chains. */
1444
1445 /* Helper function to implement rule 1 above. ONE_CHAIN is
1446 the AND predication to be simplified. */
1447
1448 static void
1449 simplify_pred (pred_chain *one_chain)
1450 {
1451 size_t i, j, n;
1452 bool simplified = false;
1453 pred_chain s_chain = vNULL;
1454
1455 n = one_chain->length ();
1456
1457 for (i = 0; i < n; i++)
1458 {
1459 pred_info *a_pred = &(*one_chain)[i];
1460
1461 if (!a_pred->pred_lhs)
1462 continue;
1463 if (!is_neq_zero_form_p (*a_pred))
1464 continue;
1465
1466 gimple def_stmt = SSA_NAME_DEF_STMT (a_pred->pred_lhs);
1467 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1468 continue;
1469 if (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR)
1470 {
1471 for (j = 0; j < n; j++)
1472 {
1473 pred_info *b_pred = &(*one_chain)[j];
1474
1475 if (!b_pred->pred_lhs)
1476 continue;
1477 if (!is_neq_zero_form_p (*b_pred))
1478 continue;
1479
1480 if (pred_expr_equal_p (*b_pred, gimple_assign_rhs1 (def_stmt))
1481 || pred_expr_equal_p (*b_pred, gimple_assign_rhs2 (def_stmt)))
1482 {
1483 /* Mark a_pred for removal. */
1484 a_pred->pred_lhs = NULL;
1485 a_pred->pred_rhs = NULL;
1486 simplified = true;
1487 break;
1488 }
1489 }
1490 }
1491 }
1492
1493 if (!simplified)
1494 return;
1495
1496 for (i = 0; i < n; i++)
1497 {
1498 pred_info *a_pred = &(*one_chain)[i];
1499 if (!a_pred->pred_lhs)
1500 continue;
1501 s_chain.safe_push (*a_pred);
1502 }
1503
1504 one_chain->release ();
1505 *one_chain = s_chain;
1506 }
1507
1508 /* The helper function implements the rule 2 for the
1509 OR predicate PREDS.
1510
1511 2) (X AND Y) OR (!X AND Y) is equivalent to Y. */
1512
1513 static bool
1514 simplify_preds_2 (pred_chain_union *preds)
1515 {
1516 size_t i, j, n;
1517 bool simplified = false;
1518 pred_chain_union s_preds = vNULL;
1519
1520 /* (X AND Y) OR (!X AND Y) is equivalent to Y.
1521 (X AND Y) OR (X AND !Y) is equivalent to X. */
1522
1523 n = preds->length ();
1524 for (i = 0; i < n; i++)
1525 {
1526 pred_info x, y;
1527 pred_chain *a_chain = &(*preds)[i];
1528
1529 if (a_chain->length () != 2)
1530 continue;
1531
1532 x = (*a_chain)[0];
1533 y = (*a_chain)[1];
1534
1535 for (j = 0; j < n; j++)
1536 {
1537 pred_chain *b_chain;
1538 pred_info x2, y2;
1539
1540 if (j == i)
1541 continue;
1542
1543 b_chain = &(*preds)[j];
1544 if (b_chain->length () != 2)
1545 continue;
1546
1547 x2 = (*b_chain)[0];
1548 y2 = (*b_chain)[1];
1549
1550 if (pred_equal_p (x, x2) && pred_neg_p (y, y2))
1551 {
1552 /* Kill a_chain. */
1553 a_chain->release ();
1554 b_chain->release ();
1555 b_chain->safe_push (x);
1556 simplified = true;
1557 break;
1558 }
1559 if (pred_neg_p (x, x2) && pred_equal_p (y, y2))
1560 {
1561 /* Kill a_chain. */
1562 a_chain->release ();
1563 b_chain->release ();
1564 b_chain->safe_push (y);
1565 simplified = true;
1566 break;
1567 }
1568 }
1569 }
1570 /* Now clean up the chain. */
1571 if (simplified)
1572 {
1573 for (i = 0; i < n; i++)
1574 {
1575 if ((*preds)[i].is_empty ())
1576 continue;
1577 s_preds.safe_push ((*preds)[i]);
1578 }
1579 preds->release ();
1580 (*preds) = s_preds;
1581 s_preds = vNULL;
1582 }
1583
1584 return simplified;
1585 }
1586
1587 /* The helper function implements the rule 2 for the
1588 OR predicate PREDS.
1589
1590 3) x OR (!x AND y) is equivalent to x OR y. */
1591
1592 static bool
1593 simplify_preds_3 (pred_chain_union *preds)
1594 {
1595 size_t i, j, n;
1596 bool simplified = false;
1597
1598 /* Now iteratively simplify X OR (!X AND Z ..)
1599 into X OR (Z ...). */
1600
1601 n = preds->length ();
1602 if (n < 2)
1603 return false;
1604
1605 for (i = 0; i < n; i++)
1606 {
1607 pred_info x;
1608 pred_chain *a_chain = &(*preds)[i];
1609
1610 if (a_chain->length () != 1)
1611 continue;
1612
1613 x = (*a_chain)[0];
1614
1615 for (j = 0; j < n; j++)
1616 {
1617 pred_chain *b_chain;
1618 pred_info x2;
1619 size_t k;
1620
1621 if (j == i)
1622 continue;
1623
1624 b_chain = &(*preds)[j];
1625 if (b_chain->length () < 2)
1626 continue;
1627
1628 for (k = 0; k < b_chain->length (); k++)
1629 {
1630 x2 = (*b_chain)[k];
1631 if (pred_neg_p (x, x2))
1632 {
1633 b_chain->unordered_remove (k);
1634 simplified = true;
1635 break;
1636 }
1637 }
1638 }
1639 }
1640 return simplified;
1641 }
1642
1643 /* The helper function implements the rule 4 for the
1644 OR predicate PREDS.
1645
1646 2) ((x AND y) != 0) OR (x != 0 AND y != 0) is equivalent to
1647 (x != 0 ANd y != 0). */
1648
1649 static bool
1650 simplify_preds_4 (pred_chain_union *preds)
1651 {
1652 size_t i, j, n;
1653 bool simplified = false;
1654 pred_chain_union s_preds = vNULL;
1655 gimple def_stmt;
1656
1657 n = preds->length ();
1658 for (i = 0; i < n; i++)
1659 {
1660 pred_info z;
1661 pred_chain *a_chain = &(*preds)[i];
1662
1663 if (a_chain->length () != 1)
1664 continue;
1665
1666 z = (*a_chain)[0];
1667
1668 if (!is_neq_zero_form_p (z))
1669 continue;
1670
1671 def_stmt = SSA_NAME_DEF_STMT (z.pred_lhs);
1672 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1673 continue;
1674
1675 if (gimple_assign_rhs_code (def_stmt) != BIT_AND_EXPR)
1676 continue;
1677
1678 for (j = 0; j < n; j++)
1679 {
1680 pred_chain *b_chain;
1681 pred_info x2, y2;
1682
1683 if (j == i)
1684 continue;
1685
1686 b_chain = &(*preds)[j];
1687 if (b_chain->length () != 2)
1688 continue;
1689
1690 x2 = (*b_chain)[0];
1691 y2 = (*b_chain)[1];
1692 if (!is_neq_zero_form_p (x2)
1693 || !is_neq_zero_form_p (y2))
1694 continue;
1695
1696 if ((pred_expr_equal_p (x2, gimple_assign_rhs1 (def_stmt))
1697 && pred_expr_equal_p (y2, gimple_assign_rhs2 (def_stmt)))
1698 || (pred_expr_equal_p (x2, gimple_assign_rhs2 (def_stmt))
1699 && pred_expr_equal_p (y2, gimple_assign_rhs1 (def_stmt))))
1700 {
1701 /* Kill a_chain. */
1702 a_chain->release ();
1703 simplified = true;
1704 break;
1705 }
1706 }
1707 }
1708 /* Now clean up the chain. */
1709 if (simplified)
1710 {
1711 for (i = 0; i < n; i++)
1712 {
1713 if ((*preds)[i].is_empty ())
1714 continue;
1715 s_preds.safe_push ((*preds)[i]);
1716 }
1717 preds->release ();
1718 (*preds) = s_preds;
1719 s_preds = vNULL;
1720 }
1721
1722 return simplified;
1723 }
1724
1725
1726 /* This function simplifies predicates in PREDS. */
1727
1728 static void
1729 simplify_preds (pred_chain_union *preds, gimple use_or_def, bool is_use)
1730 {
1731 size_t i, n;
1732 bool changed = false;
1733
1734 if (dump_file && dump_flags & TDF_DETAILS)
1735 {
1736 fprintf (dump_file, "[BEFORE SIMPLICATION -- ");
1737 dump_predicates (use_or_def, *preds, is_use ? "[USE]:\n" : "[DEF]:\n");
1738 }
1739
1740 for (i = 0; i < preds->length (); i++)
1741 simplify_pred (&(*preds)[i]);
1742
1743 n = preds->length ();
1744 if (n < 2)
1745 return;
1746
1747 do
1748 {
1749 changed = false;
1750 if (simplify_preds_2 (preds))
1751 changed = true;
1752
1753 /* Now iteratively simplify X OR (!X AND Z ..)
1754 into X OR (Z ...). */
1755 if (simplify_preds_3 (preds))
1756 changed = true;
1757
1758 if (simplify_preds_4 (preds))
1759 changed = true;
1760
1761 } while (changed);
1762
1763 return;
1764 }
1765
1766 /* This is a helper function which attempts to normalize predicate chains
1767 by following UD chains. It basically builds up a big tree of either IOR
1768 operations or AND operations, and convert the IOR tree into a
1769 pred_chain_union or BIT_AND tree into a pred_chain.
1770 Example:
1771
1772 _3 = _2 RELOP1 _1;
1773 _6 = _5 RELOP2 _4;
1774 _9 = _8 RELOP3 _7;
1775 _10 = _3 | _6;
1776 _12 = _9 | _0;
1777 _t = _10 | _12;
1778
1779 then _t != 0 will be normalized into a pred_chain_union
1780
1781 (_2 RELOP1 _1) OR (_5 RELOP2 _4) OR (_8 RELOP3 _7) OR (_0 != 0)
1782
1783 Similarly given,
1784
1785 _3 = _2 RELOP1 _1;
1786 _6 = _5 RELOP2 _4;
1787 _9 = _8 RELOP3 _7;
1788 _10 = _3 & _6;
1789 _12 = _9 & _0;
1790
1791 then _t != 0 will be normalized into a pred_chain:
1792 (_2 RELOP1 _1) AND (_5 RELOP2 _4) AND (_8 RELOP3 _7) AND (_0 != 0)
1793
1794 */
1795
1796 /* This is a helper function that stores a PRED into NORM_PREDS. */
1797
1798 inline static void
1799 push_pred (pred_chain_union *norm_preds, pred_info pred)
1800 {
1801 pred_chain pred_chain = vNULL;
1802 pred_chain.safe_push (pred);
1803 norm_preds->safe_push (pred_chain);
1804 }
1805
1806 /* A helper function that creates a predicate of the form
1807 OP != 0 and push it WORK_LIST. */
1808
1809 inline static void
1810 push_to_worklist (tree op, vec<pred_info, va_heap, vl_ptr> *work_list,
1811 pointer_set_t *mark_set)
1812 {
1813 if (pointer_set_contains (mark_set, op))
1814 return;
1815 pointer_set_insert (mark_set, op);
1816
1817 pred_info arg_pred;
1818 arg_pred.pred_lhs = op;
1819 arg_pred.pred_rhs = integer_zero_node;
1820 arg_pred.cond_code = NE_EXPR;
1821 arg_pred.invert = false;
1822 work_list->safe_push (arg_pred);
1823 }
1824
1825 /* A helper that generates a pred_info from a gimple assignment
1826 CMP_ASSIGN with comparison rhs. */
1827
1828 static pred_info
1829 get_pred_info_from_cmp (gimple cmp_assign)
1830 {
1831 pred_info n_pred;
1832 n_pred.pred_lhs = gimple_assign_rhs1 (cmp_assign);
1833 n_pred.pred_rhs = gimple_assign_rhs2 (cmp_assign);
1834 n_pred.cond_code = gimple_assign_rhs_code (cmp_assign);
1835 n_pred.invert = false;
1836 return n_pred;
1837 }
1838
1839 /* Returns true if the PHI is a degenerated phi with
1840 all args with the same value (relop). In that case, *PRED
1841 will be updated to that value. */
1842
1843 static bool
1844 is_degenerated_phi (gimple phi, pred_info *pred_p)
1845 {
1846 int i, n;
1847 tree op0;
1848 gimple def0;
1849 pred_info pred0;
1850
1851 n = gimple_phi_num_args (phi);
1852 op0 = gimple_phi_arg_def (phi, 0);
1853
1854 if (TREE_CODE (op0) != SSA_NAME)
1855 return false;
1856
1857 def0 = SSA_NAME_DEF_STMT (op0);
1858 if (gimple_code (def0) != GIMPLE_ASSIGN)
1859 return false;
1860 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def0))
1861 != tcc_comparison)
1862 return false;
1863 pred0 = get_pred_info_from_cmp (def0);
1864
1865 for (i = 1; i < n; ++i)
1866 {
1867 gimple def;
1868 pred_info pred;
1869 tree op = gimple_phi_arg_def (phi, i);
1870
1871 if (TREE_CODE (op) != SSA_NAME)
1872 return false;
1873
1874 def = SSA_NAME_DEF_STMT (op);
1875 if (gimple_code (def) != GIMPLE_ASSIGN)
1876 return false;
1877 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def))
1878 != tcc_comparison)
1879 return false;
1880 pred = get_pred_info_from_cmp (def);
1881 if (!pred_equal_p (pred, pred0))
1882 return false;
1883 }
1884
1885 *pred_p = pred0;
1886 return true;
1887 }
1888
1889 /* Normalize one predicate PRED
1890 1) if PRED can no longer be normlized, put it into NORM_PREDS.
1891 2) otherwise if PRED is of the form x != 0, follow x's definition
1892 and put normalized predicates into WORK_LIST. */
1893
1894 static void
1895 normalize_one_pred_1 (pred_chain_union *norm_preds,
1896 pred_chain *norm_chain,
1897 pred_info pred,
1898 enum tree_code and_or_code,
1899 vec<pred_info, va_heap, vl_ptr> *work_list,
1900 pointer_set_t *mark_set)
1901 {
1902 if (!is_neq_zero_form_p (pred))
1903 {
1904 if (and_or_code == BIT_IOR_EXPR)
1905 push_pred (norm_preds, pred);
1906 else
1907 norm_chain->safe_push (pred);
1908 return;
1909 }
1910
1911 gimple def_stmt = SSA_NAME_DEF_STMT (pred.pred_lhs);
1912
1913 if (gimple_code (def_stmt) == GIMPLE_PHI
1914 && is_degenerated_phi (def_stmt, &pred))
1915 work_list->safe_push (pred);
1916 else if (gimple_code (def_stmt) == GIMPLE_PHI
1917 && and_or_code == BIT_IOR_EXPR)
1918 {
1919 int i, n;
1920 n = gimple_phi_num_args (def_stmt);
1921
1922 /* If we see non zero constant, we should punt. The predicate
1923 * should be one guarding the phi edge. */
1924 for (i = 0; i < n; ++i)
1925 {
1926 tree op = gimple_phi_arg_def (def_stmt, i);
1927 if (TREE_CODE (op) == INTEGER_CST && !integer_zerop (op))
1928 {
1929 push_pred (norm_preds, pred);
1930 return;
1931 }
1932 }
1933
1934 for (i = 0; i < n; ++i)
1935 {
1936 tree op = gimple_phi_arg_def (def_stmt, i);
1937 if (integer_zerop (op))
1938 continue;
1939
1940 push_to_worklist (op, work_list, mark_set);
1941 }
1942 }
1943 else if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1944 {
1945 if (and_or_code == BIT_IOR_EXPR)
1946 push_pred (norm_preds, pred);
1947 else
1948 norm_chain->safe_push (pred);
1949 }
1950 else if (gimple_assign_rhs_code (def_stmt) == and_or_code)
1951 {
1952 push_to_worklist (gimple_assign_rhs1 (def_stmt), work_list, mark_set);
1953 push_to_worklist (gimple_assign_rhs2 (def_stmt), work_list, mark_set);
1954 }
1955 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt))
1956 == tcc_comparison)
1957 {
1958 pred_info n_pred = get_pred_info_from_cmp (def_stmt);
1959 if (and_or_code == BIT_IOR_EXPR)
1960 push_pred (norm_preds, n_pred);
1961 else
1962 norm_chain->safe_push (n_pred);
1963 }
1964 else
1965 {
1966 if (and_or_code == BIT_IOR_EXPR)
1967 push_pred (norm_preds, pred);
1968 else
1969 norm_chain->safe_push (pred);
1970 }
1971 }
1972
1973 /* Normalize PRED and store the normalized predicates into NORM_PREDS. */
1974
1975 static void
1976 normalize_one_pred (pred_chain_union *norm_preds,
1977 pred_info pred)
1978 {
1979 vec<pred_info, va_heap, vl_ptr> work_list = vNULL;
1980 pointer_set_t *mark_set = NULL;
1981 enum tree_code and_or_code = ERROR_MARK;
1982 pred_chain norm_chain = vNULL;
1983
1984 if (!is_neq_zero_form_p (pred))
1985 {
1986 push_pred (norm_preds, pred);
1987 return;
1988 }
1989
1990 gimple def_stmt = SSA_NAME_DEF_STMT (pred.pred_lhs);
1991 if (gimple_code (def_stmt) == GIMPLE_ASSIGN)
1992 and_or_code = gimple_assign_rhs_code (def_stmt);
1993 if (and_or_code != BIT_IOR_EXPR
1994 && and_or_code != BIT_AND_EXPR)
1995 {
1996 if (TREE_CODE_CLASS (and_or_code)
1997 == tcc_comparison)
1998 {
1999 pred_info n_pred = get_pred_info_from_cmp (def_stmt);
2000 push_pred (norm_preds, n_pred);
2001 }
2002 else
2003 push_pred (norm_preds, pred);
2004 return;
2005 }
2006
2007 work_list.safe_push (pred);
2008 mark_set = pointer_set_create ();
2009
2010 while (!work_list.is_empty ())
2011 {
2012 pred_info a_pred = work_list.pop ();
2013 normalize_one_pred_1 (norm_preds, &norm_chain, a_pred,
2014 and_or_code, &work_list, mark_set);
2015 }
2016 if (and_or_code == BIT_AND_EXPR)
2017 norm_preds->safe_push (norm_chain);
2018
2019 work_list.release ();
2020 pointer_set_destroy (mark_set);
2021 }
2022
2023 static void
2024 normalize_one_pred_chain (pred_chain_union *norm_preds,
2025 pred_chain one_chain)
2026 {
2027 vec<pred_info, va_heap, vl_ptr> work_list = vNULL;
2028 pointer_set_t *mark_set = pointer_set_create ();
2029 pred_chain norm_chain = vNULL;
2030 size_t i;
2031
2032 for (i = 0; i < one_chain.length (); i++)
2033 {
2034 work_list.safe_push (one_chain[i]);
2035 pointer_set_insert (mark_set, one_chain[i].pred_lhs);
2036 }
2037
2038 while (!work_list.is_empty ())
2039 {
2040 pred_info a_pred = work_list.pop ();
2041 normalize_one_pred_1 (0, &norm_chain, a_pred,
2042 BIT_AND_EXPR, &work_list, mark_set);
2043 }
2044
2045 norm_preds->safe_push (norm_chain);
2046 work_list.release ();
2047 pointer_set_destroy (mark_set);
2048 }
2049
2050 /* Normalize predicate chains PREDS and returns the normalized one. */
2051
2052 static pred_chain_union
2053 normalize_preds (pred_chain_union preds, gimple use_or_def, bool is_use)
2054 {
2055 pred_chain_union norm_preds = vNULL;
2056 size_t n = preds.length ();
2057 size_t i;
2058
2059 if (dump_file && dump_flags & TDF_DETAILS)
2060 {
2061 fprintf (dump_file, "[BEFORE NORMALIZATION --");
2062 dump_predicates (use_or_def, preds, is_use ? "[USE]:\n" : "[DEF]:\n");
2063 }
2064
2065 for (i = 0; i < n; i++)
2066 {
2067 if (preds[i].length () != 1)
2068 normalize_one_pred_chain (&norm_preds, preds[i]);
2069 else
2070 {
2071 normalize_one_pred (&norm_preds, preds[i][0]);
2072 preds[i].release ();
2073 }
2074 }
2075
2076 if (dump_file)
2077 {
2078 fprintf (dump_file, "[AFTER NORMALIZATION -- ");
2079 dump_predicates (use_or_def, norm_preds, is_use ? "[USE]:\n" : "[DEF]:\n");
2080 }
2081
2082 preds.release ();
2083 return norm_preds;
2084 }
2085
2086
2087 /* Computes the predicates that guard the use and checks
2088 if the incoming paths that have empty (or possibly
2089 empty) definition can be pruned/filtered. The function returns
2090 true if it can be determined that the use of PHI's def in
2091 USE_STMT is guarded with a predicate set not overlapping with
2092 predicate sets of all runtime paths that do not have a definition.
2093 Returns false if it is not or it can not be determined. USE_BB is
2094 the bb of the use (for phi operand use, the bb is not the bb of
2095 the phi stmt, but the src bb of the operand edge). UNINIT_OPNDS
2096 is a bit vector. If an operand of PHI is uninitialized, the
2097 corresponding bit in the vector is 1. VISIED_PHIS is a pointer
2098 set of phis being visted. */
2099
2100 static bool
2101 is_use_properly_guarded (gimple use_stmt,
2102 basic_block use_bb,
2103 gimple phi,
2104 unsigned uninit_opnds,
2105 pointer_set_t *visited_phis)
2106 {
2107 basic_block phi_bb;
2108 pred_chain_union preds = vNULL;
2109 pred_chain_union def_preds = vNULL;
2110 bool has_valid_preds = false;
2111 bool is_properly_guarded = false;
2112
2113 if (pointer_set_insert (visited_phis, phi))
2114 return false;
2115
2116 phi_bb = gimple_bb (phi);
2117
2118 if (is_non_loop_exit_postdominating (use_bb, phi_bb))
2119 return false;
2120
2121 has_valid_preds = find_predicates (&preds, phi_bb, use_bb);
2122
2123 if (!has_valid_preds)
2124 {
2125 destroy_predicate_vecs (preds);
2126 return false;
2127 }
2128
2129 /* Try to prune the dead incoming phi edges. */
2130 is_properly_guarded
2131 = use_pred_not_overlap_with_undef_path_pred (preds, phi, uninit_opnds,
2132 visited_phis);
2133
2134 if (is_properly_guarded)
2135 {
2136 destroy_predicate_vecs (preds);
2137 return true;
2138 }
2139
2140 has_valid_preds = find_def_preds (&def_preds, phi);
2141
2142 if (!has_valid_preds)
2143 {
2144 destroy_predicate_vecs (preds);
2145 destroy_predicate_vecs (def_preds);
2146 return false;
2147 }
2148
2149 simplify_preds (&preds, use_stmt, true);
2150 preds = normalize_preds (preds, use_stmt, true);
2151
2152 simplify_preds (&def_preds, phi, false);
2153 def_preds = normalize_preds (def_preds, phi, false);
2154
2155 is_properly_guarded = is_superset_of (def_preds, preds);
2156
2157 destroy_predicate_vecs (preds);
2158 destroy_predicate_vecs (def_preds);
2159 return is_properly_guarded;
2160 }
2161
2162 /* Searches through all uses of a potentially
2163 uninitialized variable defined by PHI and returns a use
2164 statement if the use is not properly guarded. It returns
2165 NULL if all uses are guarded. UNINIT_OPNDS is a bitvector
2166 holding the position(s) of uninit PHI operands. WORKLIST
2167 is the vector of candidate phis that may be updated by this
2168 function. ADDED_TO_WORKLIST is the pointer set tracking
2169 if the new phi is already in the worklist. */
2170
2171 static gimple
2172 find_uninit_use (gimple phi, unsigned uninit_opnds,
2173 vec<gimple> *worklist,
2174 pointer_set_t *added_to_worklist)
2175 {
2176 tree phi_result;
2177 use_operand_p use_p;
2178 gimple use_stmt;
2179 imm_use_iterator iter;
2180
2181 phi_result = gimple_phi_result (phi);
2182
2183 FOR_EACH_IMM_USE_FAST (use_p, iter, phi_result)
2184 {
2185 pointer_set_t *visited_phis;
2186 basic_block use_bb;
2187
2188 use_stmt = USE_STMT (use_p);
2189 if (is_gimple_debug (use_stmt))
2190 continue;
2191
2192 visited_phis = pointer_set_create ();
2193
2194 if (gimple_code (use_stmt) == GIMPLE_PHI)
2195 use_bb = gimple_phi_arg_edge (use_stmt,
2196 PHI_ARG_INDEX_FROM_USE (use_p))->src;
2197 else
2198 use_bb = gimple_bb (use_stmt);
2199
2200 if (is_use_properly_guarded (use_stmt, use_bb, phi, uninit_opnds,
2201 visited_phis))
2202 {
2203 pointer_set_destroy (visited_phis);
2204 continue;
2205 }
2206 pointer_set_destroy (visited_phis);
2207
2208 if (dump_file && (dump_flags & TDF_DETAILS))
2209 {
2210 fprintf (dump_file, "[CHECK]: Found unguarded use: ");
2211 print_gimple_stmt (dump_file, use_stmt, 0, 0);
2212 }
2213 /* Found one real use, return. */
2214 if (gimple_code (use_stmt) != GIMPLE_PHI)
2215 return use_stmt;
2216
2217 /* Found a phi use that is not guarded,
2218 add the phi to the worklist. */
2219 if (!pointer_set_insert (added_to_worklist, use_stmt))
2220 {
2221 if (dump_file && (dump_flags & TDF_DETAILS))
2222 {
2223 fprintf (dump_file, "[WORKLIST]: Update worklist with phi: ");
2224 print_gimple_stmt (dump_file, use_stmt, 0, 0);
2225 }
2226
2227 worklist->safe_push (use_stmt);
2228 pointer_set_insert (possibly_undefined_names, phi_result);
2229 }
2230 }
2231
2232 return NULL;
2233 }
2234
2235 /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions
2236 and gives warning if there exists a runtime path from the entry to a
2237 use of the PHI def that does not contain a definition. In other words,
2238 the warning is on the real use. The more dead paths that can be pruned
2239 by the compiler, the fewer false positives the warning is. WORKLIST
2240 is a vector of candidate phis to be examined. ADDED_TO_WORKLIST is
2241 a pointer set tracking if the new phi is added to the worklist or not. */
2242
2243 static void
2244 warn_uninitialized_phi (gimple phi, vec<gimple> *worklist,
2245 pointer_set_t *added_to_worklist)
2246 {
2247 unsigned uninit_opnds;
2248 gimple uninit_use_stmt = 0;
2249 tree uninit_op;
2250
2251 /* Don't look at virtual operands. */
2252 if (virtual_operand_p (gimple_phi_result (phi)))
2253 return;
2254
2255 uninit_opnds = compute_uninit_opnds_pos (phi);
2256
2257 if (MASK_EMPTY (uninit_opnds))
2258 return;
2259
2260 if (dump_file && (dump_flags & TDF_DETAILS))
2261 {
2262 fprintf (dump_file, "[CHECK]: examining phi: ");
2263 print_gimple_stmt (dump_file, phi, 0, 0);
2264 }
2265
2266 /* Now check if we have any use of the value without proper guard. */
2267 uninit_use_stmt = find_uninit_use (phi, uninit_opnds,
2268 worklist, added_to_worklist);
2269
2270 /* All uses are properly guarded. */
2271 if (!uninit_use_stmt)
2272 return;
2273
2274 uninit_op = gimple_phi_arg_def (phi, MASK_FIRST_SET_BIT (uninit_opnds));
2275 if (SSA_NAME_VAR (uninit_op) == NULL_TREE)
2276 return;
2277 warn_uninit (OPT_Wmaybe_uninitialized, uninit_op, SSA_NAME_VAR (uninit_op),
2278 SSA_NAME_VAR (uninit_op),
2279 "%qD may be used uninitialized in this function",
2280 uninit_use_stmt);
2281
2282 }
2283
2284
2285 /* Entry point to the late uninitialized warning pass. */
2286
2287 static unsigned int
2288 execute_late_warn_uninitialized (void)
2289 {
2290 basic_block bb;
2291 gimple_stmt_iterator gsi;
2292 vec<gimple> worklist = vNULL;
2293 pointer_set_t *added_to_worklist;
2294
2295 calculate_dominance_info (CDI_DOMINATORS);
2296 calculate_dominance_info (CDI_POST_DOMINATORS);
2297 /* Re-do the plain uninitialized variable check, as optimization may have
2298 straightened control flow. Do this first so that we don't accidentally
2299 get a "may be" warning when we'd have seen an "is" warning later. */
2300 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/1);
2301
2302 timevar_push (TV_TREE_UNINIT);
2303
2304 possibly_undefined_names = pointer_set_create ();
2305 added_to_worklist = pointer_set_create ();
2306
2307 /* Initialize worklist */
2308 FOR_EACH_BB_FN (bb, cfun)
2309 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2310 {
2311 gimple phi = gsi_stmt (gsi);
2312 size_t n, i;
2313
2314 n = gimple_phi_num_args (phi);
2315
2316 /* Don't look at virtual operands. */
2317 if (virtual_operand_p (gimple_phi_result (phi)))
2318 continue;
2319
2320 for (i = 0; i < n; ++i)
2321 {
2322 tree op = gimple_phi_arg_def (phi, i);
2323 if (TREE_CODE (op) == SSA_NAME
2324 && uninit_undefined_value_p (op))
2325 {
2326 worklist.safe_push (phi);
2327 pointer_set_insert (added_to_worklist, phi);
2328 if (dump_file && (dump_flags & TDF_DETAILS))
2329 {
2330 fprintf (dump_file, "[WORKLIST]: add to initial list: ");
2331 print_gimple_stmt (dump_file, phi, 0, 0);
2332 }
2333 break;
2334 }
2335 }
2336 }
2337
2338 while (worklist.length () != 0)
2339 {
2340 gimple cur_phi = 0;
2341 cur_phi = worklist.pop ();
2342 warn_uninitialized_phi (cur_phi, &worklist, added_to_worklist);
2343 }
2344
2345 worklist.release ();
2346 pointer_set_destroy (added_to_worklist);
2347 pointer_set_destroy (possibly_undefined_names);
2348 possibly_undefined_names = NULL;
2349 free_dominance_info (CDI_POST_DOMINATORS);
2350 timevar_pop (TV_TREE_UNINIT);
2351 return 0;
2352 }
2353
2354 static bool
2355 gate_warn_uninitialized (void)
2356 {
2357 return warn_uninitialized || warn_maybe_uninitialized;
2358 }
2359
2360 namespace {
2361
2362 const pass_data pass_data_late_warn_uninitialized =
2363 {
2364 GIMPLE_PASS, /* type */
2365 "uninit", /* name */
2366 OPTGROUP_NONE, /* optinfo_flags */
2367 true, /* has_execute */
2368 TV_NONE, /* tv_id */
2369 PROP_ssa, /* properties_required */
2370 0, /* properties_provided */
2371 0, /* properties_destroyed */
2372 0, /* todo_flags_start */
2373 0, /* todo_flags_finish */
2374 };
2375
2376 class pass_late_warn_uninitialized : public gimple_opt_pass
2377 {
2378 public:
2379 pass_late_warn_uninitialized (gcc::context *ctxt)
2380 : gimple_opt_pass (pass_data_late_warn_uninitialized, ctxt)
2381 {}
2382
2383 /* opt_pass methods: */
2384 opt_pass * clone () { return new pass_late_warn_uninitialized (m_ctxt); }
2385 bool gate () { return gate_warn_uninitialized (); }
2386 unsigned int execute () { return execute_late_warn_uninitialized (); }
2387
2388 }; // class pass_late_warn_uninitialized
2389
2390 } // anon namespace
2391
2392 gimple_opt_pass *
2393 make_pass_late_warn_uninitialized (gcc::context *ctxt)
2394 {
2395 return new pass_late_warn_uninitialized (ctxt);
2396 }
2397
2398
2399 static unsigned int
2400 execute_early_warn_uninitialized (void)
2401 {
2402 /* Currently, this pass runs always but
2403 execute_late_warn_uninitialized only runs with optimization. With
2404 optimization we want to warn about possible uninitialized as late
2405 as possible, thus don't do it here. However, without
2406 optimization we need to warn here about "may be uninitialized". */
2407 calculate_dominance_info (CDI_POST_DOMINATORS);
2408
2409 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/!optimize);
2410
2411 /* Post-dominator information can not be reliably updated. Free it
2412 after the use. */
2413
2414 free_dominance_info (CDI_POST_DOMINATORS);
2415 return 0;
2416 }
2417
2418
2419 namespace {
2420
2421 const pass_data pass_data_early_warn_uninitialized =
2422 {
2423 GIMPLE_PASS, /* type */
2424 "*early_warn_uninitialized", /* name */
2425 OPTGROUP_NONE, /* optinfo_flags */
2426 true, /* has_execute */
2427 TV_TREE_UNINIT, /* tv_id */
2428 PROP_ssa, /* properties_required */
2429 0, /* properties_provided */
2430 0, /* properties_destroyed */
2431 0, /* todo_flags_start */
2432 0, /* todo_flags_finish */
2433 };
2434
2435 class pass_early_warn_uninitialized : public gimple_opt_pass
2436 {
2437 public:
2438 pass_early_warn_uninitialized (gcc::context *ctxt)
2439 : gimple_opt_pass (pass_data_early_warn_uninitialized, ctxt)
2440 {}
2441
2442 /* opt_pass methods: */
2443 bool gate () { return gate_warn_uninitialized (); }
2444 unsigned int execute () { return execute_early_warn_uninitialized (); }
2445
2446 }; // class pass_early_warn_uninitialized
2447
2448 } // anon namespace
2449
2450 gimple_opt_pass *
2451 make_pass_early_warn_uninitialized (gcc::context *ctxt)
2452 {
2453 return new pass_early_warn_uninitialized (ctxt);
2454 }