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