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