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