/cp
[official-gcc.git] / gcc / tree-ssa-threadedge.c
blobde671b9463773e8a5d22807a8824670e4dc3f705
1 /* SSA Jump Threading
2 Copyright (C) 2005-2016 Free Software Foundation, Inc.
3 Contributed by Jeff Law <law@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "predict.h"
28 #include "ssa.h"
29 #include "fold-const.h"
30 #include "cfgloop.h"
31 #include "gimple-iterator.h"
32 #include "tree-cfg.h"
33 #include "tree-ssa-threadupdate.h"
34 #include "params.h"
35 #include "tree-ssa-scopedtables.h"
36 #include "tree-ssa-threadedge.h"
37 #include "tree-ssa-dom.h"
38 #include "gimple-fold.h"
40 /* To avoid code explosion due to jump threading, we limit the
41 number of statements we are going to copy. This variable
42 holds the number of statements currently seen that we'll have
43 to copy as part of the jump threading process. */
44 static int stmt_count;
46 /* Array to record value-handles per SSA_NAME. */
47 vec<tree> ssa_name_values;
49 typedef tree (pfn_simplify) (gimple *, gimple *, class avail_exprs_stack *);
51 /* Set the value for the SSA name NAME to VALUE. */
53 void
54 set_ssa_name_value (tree name, tree value)
56 if (SSA_NAME_VERSION (name) >= ssa_name_values.length ())
57 ssa_name_values.safe_grow_cleared (SSA_NAME_VERSION (name) + 1);
58 if (value && TREE_OVERFLOW_P (value))
59 value = drop_tree_overflow (value);
60 ssa_name_values[SSA_NAME_VERSION (name)] = value;
63 /* Initialize the per SSA_NAME value-handles array. Returns it. */
64 void
65 threadedge_initialize_values (void)
67 gcc_assert (!ssa_name_values.exists ());
68 ssa_name_values.create (num_ssa_names);
71 /* Free the per SSA_NAME value-handle array. */
72 void
73 threadedge_finalize_values (void)
75 ssa_name_values.release ();
78 /* Return TRUE if we may be able to thread an incoming edge into
79 BB to an outgoing edge from BB. Return FALSE otherwise. */
81 bool
82 potentially_threadable_block (basic_block bb)
84 gimple_stmt_iterator gsi;
86 /* Special case. We can get blocks that are forwarders, but are
87 not optimized away because they forward from outside a loop
88 to the loop header. We want to thread through them as we can
89 sometimes thread to the loop exit, which is obviously profitable.
90 the interesting case here is when the block has PHIs. */
91 if (gsi_end_p (gsi_start_nondebug_bb (bb))
92 && !gsi_end_p (gsi_start_phis (bb)))
93 return true;
95 /* If BB has a single successor or a single predecessor, then
96 there is no threading opportunity. */
97 if (single_succ_p (bb) || single_pred_p (bb))
98 return false;
100 /* If BB does not end with a conditional, switch or computed goto,
101 then there is no threading opportunity. */
102 gsi = gsi_last_bb (bb);
103 if (gsi_end_p (gsi)
104 || ! gsi_stmt (gsi)
105 || (gimple_code (gsi_stmt (gsi)) != GIMPLE_COND
106 && gimple_code (gsi_stmt (gsi)) != GIMPLE_GOTO
107 && gimple_code (gsi_stmt (gsi)) != GIMPLE_SWITCH))
108 return false;
110 return true;
113 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
114 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
115 BB. If no such ASSERT_EXPR is found, return OP. */
117 static tree
118 lhs_of_dominating_assert (tree op, basic_block bb, gimple *stmt)
120 imm_use_iterator imm_iter;
121 gimple *use_stmt;
122 use_operand_p use_p;
124 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
126 use_stmt = USE_STMT (use_p);
127 if (use_stmt != stmt
128 && gimple_assign_single_p (use_stmt)
129 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ASSERT_EXPR
130 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) == op
131 && dominated_by_p (CDI_DOMINATORS, bb, gimple_bb (use_stmt)))
133 return gimple_assign_lhs (use_stmt);
136 return op;
139 /* Record temporary equivalences created by PHIs at the target of the
140 edge E. Record unwind information for the equivalences onto STACK.
142 If a PHI which prevents threading is encountered, then return FALSE
143 indicating we should not thread this edge, else return TRUE.
145 If SRC_MAP/DST_MAP exist, then mark the source and destination SSA_NAMEs
146 of any equivalences recorded. We use this to make invalidation after
147 traversing back edges less painful. */
149 static bool
150 record_temporary_equivalences_from_phis (edge e, const_and_copies *const_and_copies)
152 gphi_iterator gsi;
154 /* Each PHI creates a temporary equivalence, record them.
155 These are context sensitive equivalences and will be removed
156 later. */
157 for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
159 gphi *phi = gsi.phi ();
160 tree src = PHI_ARG_DEF_FROM_EDGE (phi, e);
161 tree dst = gimple_phi_result (phi);
163 /* If the desired argument is not the same as this PHI's result
164 and it is set by a PHI in E->dest, then we can not thread
165 through E->dest. */
166 if (src != dst
167 && TREE_CODE (src) == SSA_NAME
168 && gimple_code (SSA_NAME_DEF_STMT (src)) == GIMPLE_PHI
169 && gimple_bb (SSA_NAME_DEF_STMT (src)) == e->dest)
170 return false;
172 /* We consider any non-virtual PHI as a statement since it
173 count result in a constant assignment or copy operation. */
174 if (!virtual_operand_p (dst))
175 stmt_count++;
177 const_and_copies->record_const_or_copy (dst, src);
179 return true;
182 /* Valueize hook for gimple_fold_stmt_to_constant_1. */
184 static tree
185 threadedge_valueize (tree t)
187 if (TREE_CODE (t) == SSA_NAME)
189 tree tem = SSA_NAME_VALUE (t);
190 if (tem)
191 return tem;
193 return t;
196 /* Try to simplify each statement in E->dest, ultimately leading to
197 a simplification of the COND_EXPR at the end of E->dest.
199 Record unwind information for temporary equivalences onto STACK.
201 Use SIMPLIFY (a pointer to a callback function) to further simplify
202 statements using pass specific information.
204 We might consider marking just those statements which ultimately
205 feed the COND_EXPR. It's not clear if the overhead of bookkeeping
206 would be recovered by trying to simplify fewer statements.
208 If we are able to simplify a statement into the form
209 SSA_NAME = (SSA_NAME | gimple invariant), then we can record
210 a context sensitive equivalence which may help us simplify
211 later statements in E->dest. */
213 static gimple *
214 record_temporary_equivalences_from_stmts_at_dest (edge e,
215 const_and_copies *const_and_copies,
216 avail_exprs_stack *avail_exprs_stack,
217 pfn_simplify simplify)
219 gimple *stmt = NULL;
220 gimple_stmt_iterator gsi;
221 int max_stmt_count;
223 max_stmt_count = PARAM_VALUE (PARAM_MAX_JUMP_THREAD_DUPLICATION_STMTS);
225 /* Walk through each statement in the block recording equivalences
226 we discover. Note any equivalences we discover are context
227 sensitive (ie, are dependent on traversing E) and must be unwound
228 when we're finished processing E. */
229 for (gsi = gsi_start_bb (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
231 tree cached_lhs = NULL;
233 stmt = gsi_stmt (gsi);
235 /* Ignore empty statements and labels. */
236 if (gimple_code (stmt) == GIMPLE_NOP
237 || gimple_code (stmt) == GIMPLE_LABEL
238 || is_gimple_debug (stmt))
239 continue;
241 /* If the statement has volatile operands, then we assume we
242 can not thread through this block. This is overly
243 conservative in some ways. */
244 if (gimple_code (stmt) == GIMPLE_ASM
245 && gimple_asm_volatile_p (as_a <gasm *> (stmt)))
246 return NULL;
248 /* If the statement is a unique builtin, we can not thread
249 through here. */
250 if (gimple_code (stmt) == GIMPLE_CALL
251 && gimple_call_internal_p (stmt)
252 && gimple_call_internal_unique_p (stmt))
253 return NULL;
255 /* If duplicating this block is going to cause too much code
256 expansion, then do not thread through this block. */
257 stmt_count++;
258 if (stmt_count > max_stmt_count)
259 return NULL;
261 /* If this is not a statement that sets an SSA_NAME to a new
262 value, then do not try to simplify this statement as it will
263 not simplify in any way that is helpful for jump threading. */
264 if ((gimple_code (stmt) != GIMPLE_ASSIGN
265 || TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
266 && (gimple_code (stmt) != GIMPLE_CALL
267 || gimple_call_lhs (stmt) == NULL_TREE
268 || TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME))
269 continue;
271 /* The result of __builtin_object_size depends on all the arguments
272 of a phi node. Temporarily using only one edge produces invalid
273 results. For example
275 if (x < 6)
276 goto l;
277 else
278 goto l;
281 r = PHI <&w[2].a[1](2), &a.a[6](3)>
282 __builtin_object_size (r, 0)
284 The result of __builtin_object_size is defined to be the maximum of
285 remaining bytes. If we use only one edge on the phi, the result will
286 change to be the remaining bytes for the corresponding phi argument.
288 Similarly for __builtin_constant_p:
290 r = PHI <1(2), 2(3)>
291 __builtin_constant_p (r)
293 Both PHI arguments are constant, but x ? 1 : 2 is still not
294 constant. */
296 if (is_gimple_call (stmt))
298 tree fndecl = gimple_call_fndecl (stmt);
299 if (fndecl
300 && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_OBJECT_SIZE
301 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P))
302 continue;
305 /* At this point we have a statement which assigns an RHS to an
306 SSA_VAR on the LHS. We want to try and simplify this statement
307 to expose more context sensitive equivalences which in turn may
308 allow us to simplify the condition at the end of the loop.
310 Handle simple copy operations as well as implied copies from
311 ASSERT_EXPRs. */
312 if (gimple_assign_single_p (stmt)
313 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
314 cached_lhs = gimple_assign_rhs1 (stmt);
315 else if (gimple_assign_single_p (stmt)
316 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
317 cached_lhs = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
318 else
320 /* A statement that is not a trivial copy or ASSERT_EXPR.
321 Try to fold the new expression. Inserting the
322 expression into the hash table is unlikely to help. */
323 /* ??? The DOM callback below can be changed to setting
324 the mprts_hook around the call to thread_across_edge,
325 avoiding the use substitution. The VRP hook should be
326 changed to properly valueize operands itself using
327 SSA_NAME_VALUE in addition to its own lattice. */
328 cached_lhs = gimple_fold_stmt_to_constant_1 (stmt,
329 threadedge_valueize);
330 if (!cached_lhs
331 || (TREE_CODE (cached_lhs) != SSA_NAME
332 && !is_gimple_min_invariant (cached_lhs)))
334 /* We're going to temporarily copy propagate the operands
335 and see if that allows us to simplify this statement. */
336 tree *copy;
337 ssa_op_iter iter;
338 use_operand_p use_p;
339 unsigned int num, i = 0;
341 num = NUM_SSA_OPERANDS (stmt, SSA_OP_ALL_USES);
342 copy = XALLOCAVEC (tree, num);
344 /* Make a copy of the uses & vuses into USES_COPY, then cprop into
345 the operands. */
346 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
348 tree tmp = NULL;
349 tree use = USE_FROM_PTR (use_p);
351 copy[i++] = use;
352 if (TREE_CODE (use) == SSA_NAME)
353 tmp = SSA_NAME_VALUE (use);
354 if (tmp)
355 SET_USE (use_p, tmp);
358 cached_lhs = (*simplify) (stmt, stmt, avail_exprs_stack);
360 /* Restore the statement's original uses/defs. */
361 i = 0;
362 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
363 SET_USE (use_p, copy[i++]);
367 /* Record the context sensitive equivalence if we were able
368 to simplify this statement. */
369 if (cached_lhs
370 && (TREE_CODE (cached_lhs) == SSA_NAME
371 || is_gimple_min_invariant (cached_lhs)))
372 const_and_copies->record_const_or_copy (gimple_get_lhs (stmt),
373 cached_lhs);
375 return stmt;
378 static tree simplify_control_stmt_condition_1 (edge, gimple *,
379 class avail_exprs_stack *,
380 tree, enum tree_code, tree,
381 gcond *, pfn_simplify, bool,
382 unsigned);
384 /* Simplify the control statement at the end of the block E->dest.
386 To avoid allocating memory unnecessarily, a scratch GIMPLE_COND
387 is available to use/clobber in DUMMY_COND.
389 Use SIMPLIFY (a pointer to a callback function) to further simplify
390 a condition using pass specific information.
392 Return the simplified condition or NULL if simplification could
393 not be performed.
395 The available expression table is referenced via AVAIL_EXPRS_STACK. */
397 static tree
398 simplify_control_stmt_condition (edge e,
399 gimple *stmt,
400 class avail_exprs_stack *avail_exprs_stack,
401 gcond *dummy_cond,
402 pfn_simplify simplify,
403 bool handle_dominating_asserts)
405 tree cond, cached_lhs;
406 enum gimple_code code = gimple_code (stmt);
408 /* For comparisons, we have to update both operands, then try
409 to simplify the comparison. */
410 if (code == GIMPLE_COND)
412 tree op0, op1;
413 enum tree_code cond_code;
415 op0 = gimple_cond_lhs (stmt);
416 op1 = gimple_cond_rhs (stmt);
417 cond_code = gimple_cond_code (stmt);
419 /* Get the current value of both operands. */
420 if (TREE_CODE (op0) == SSA_NAME)
422 for (int i = 0; i < 2; i++)
424 if (TREE_CODE (op0) == SSA_NAME
425 && SSA_NAME_VALUE (op0))
426 op0 = SSA_NAME_VALUE (op0);
427 else
428 break;
432 if (TREE_CODE (op1) == SSA_NAME)
434 for (int i = 0; i < 2; i++)
436 if (TREE_CODE (op1) == SSA_NAME
437 && SSA_NAME_VALUE (op1))
438 op1 = SSA_NAME_VALUE (op1);
439 else
440 break;
444 const unsigned recursion_limit = 4;
446 cached_lhs
447 = simplify_control_stmt_condition_1 (e, stmt, avail_exprs_stack,
448 op0, cond_code, op1,
449 dummy_cond, simplify,
450 handle_dominating_asserts,
451 recursion_limit);
453 /* If we were testing an integer/pointer against a constant, then
454 we can use the FSM code to trace the value of the SSA_NAME. If
455 a value is found, then the condition will collapse to a constant.
457 Return the SSA_NAME we want to trace back rather than the full
458 expression and give the FSM threader a chance to find its value. */
459 if (cached_lhs == NULL)
461 /* Recover the original operands. They may have been simplified
462 using context sensitive equivalences. Those context sensitive
463 equivalences may not be valid on paths found by the FSM optimizer. */
464 tree op0 = gimple_cond_lhs (stmt);
465 tree op1 = gimple_cond_rhs (stmt);
467 if ((INTEGRAL_TYPE_P (TREE_TYPE (op0))
468 || POINTER_TYPE_P (TREE_TYPE (op0)))
469 && TREE_CODE (op0) == SSA_NAME
470 && TREE_CODE (op1) == INTEGER_CST)
471 return op0;
474 return cached_lhs;
477 if (code == GIMPLE_SWITCH)
478 cond = gimple_switch_index (as_a <gswitch *> (stmt));
479 else if (code == GIMPLE_GOTO)
480 cond = gimple_goto_dest (stmt);
481 else
482 gcc_unreachable ();
484 /* We can have conditionals which just test the state of a variable
485 rather than use a relational operator. These are simpler to handle. */
486 if (TREE_CODE (cond) == SSA_NAME)
488 tree original_lhs = cond;
489 cached_lhs = cond;
491 /* Get the variable's current value from the equivalence chains.
493 It is possible to get loops in the SSA_NAME_VALUE chains
494 (consider threading the backedge of a loop where we have
495 a loop invariant SSA_NAME used in the condition). */
496 if (cached_lhs)
498 for (int i = 0; i < 2; i++)
500 if (TREE_CODE (cached_lhs) == SSA_NAME
501 && SSA_NAME_VALUE (cached_lhs))
502 cached_lhs = SSA_NAME_VALUE (cached_lhs);
503 else
504 break;
508 /* If we're dominated by a suitable ASSERT_EXPR, then
509 update CACHED_LHS appropriately. */
510 if (handle_dominating_asserts && TREE_CODE (cached_lhs) == SSA_NAME)
511 cached_lhs = lhs_of_dominating_assert (cached_lhs, e->src, stmt);
513 /* If we haven't simplified to an invariant yet, then use the
514 pass specific callback to try and simplify it further. */
515 if (cached_lhs && ! is_gimple_min_invariant (cached_lhs))
516 cached_lhs = (*simplify) (stmt, stmt, avail_exprs_stack);
518 /* We couldn't find an invariant. But, callers of this
519 function may be able to do something useful with the
520 unmodified destination. */
521 if (!cached_lhs)
522 cached_lhs = original_lhs;
524 else
525 cached_lhs = NULL;
527 return cached_lhs;
530 /* Recursive helper for simplify_control_stmt_condition. */
532 static tree
533 simplify_control_stmt_condition_1 (edge e,
534 gimple *stmt,
535 class avail_exprs_stack *avail_exprs_stack,
536 tree op0,
537 enum tree_code cond_code,
538 tree op1,
539 gcond *dummy_cond,
540 pfn_simplify simplify,
541 bool handle_dominating_asserts,
542 unsigned limit)
544 if (limit == 0)
545 return NULL_TREE;
547 /* We may need to canonicalize the comparison. For
548 example, op0 might be a constant while op1 is an
549 SSA_NAME. Failure to canonicalize will cause us to
550 miss threading opportunities. */
551 if (tree_swap_operands_p (op0, op1, false))
553 cond_code = swap_tree_comparison (cond_code);
554 std::swap (op0, op1);
557 /* If the condition has the form (A & B) CMP 0 or (A | B) CMP 0 then
558 recurse into the LHS to see if there is a dominating ASSERT_EXPR
559 of A or of B that makes this condition always true or always false
560 along the edge E. */
561 if (handle_dominating_asserts
562 && (cond_code == EQ_EXPR || cond_code == NE_EXPR)
563 && TREE_CODE (op0) == SSA_NAME
564 && integer_zerop (op1))
566 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
567 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
569 else if (gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR
570 || gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR)
572 enum tree_code rhs_code = gimple_assign_rhs_code (def_stmt);
573 const tree rhs1 = gimple_assign_rhs1 (def_stmt);
574 const tree rhs2 = gimple_assign_rhs2 (def_stmt);
576 /* Is A != 0 ? */
577 const tree res1
578 = simplify_control_stmt_condition_1 (e, def_stmt, avail_exprs_stack,
579 rhs1, NE_EXPR, op1,
580 dummy_cond, simplify,
581 handle_dominating_asserts,
582 limit - 1);
583 if (res1 == NULL_TREE)
585 else if (rhs_code == BIT_AND_EXPR && integer_zerop (res1))
587 /* If A == 0 then (A & B) != 0 is always false. */
588 if (cond_code == NE_EXPR)
589 return boolean_false_node;
590 /* If A == 0 then (A & B) == 0 is always true. */
591 if (cond_code == EQ_EXPR)
592 return boolean_true_node;
594 else if (rhs_code == BIT_IOR_EXPR && integer_nonzerop (res1))
596 /* If A != 0 then (A | B) != 0 is always true. */
597 if (cond_code == NE_EXPR)
598 return boolean_true_node;
599 /* If A != 0 then (A | B) == 0 is always false. */
600 if (cond_code == EQ_EXPR)
601 return boolean_false_node;
604 /* Is B != 0 ? */
605 const tree res2
606 = simplify_control_stmt_condition_1 (e, def_stmt, avail_exprs_stack,
607 rhs2, NE_EXPR, op1,
608 dummy_cond, simplify,
609 handle_dominating_asserts,
610 limit - 1);
611 if (res2 == NULL_TREE)
613 else if (rhs_code == BIT_AND_EXPR && integer_zerop (res2))
615 /* If B == 0 then (A & B) != 0 is always false. */
616 if (cond_code == NE_EXPR)
617 return boolean_false_node;
618 /* If B == 0 then (A & B) == 0 is always true. */
619 if (cond_code == EQ_EXPR)
620 return boolean_true_node;
622 else if (rhs_code == BIT_IOR_EXPR && integer_nonzerop (res2))
624 /* If B != 0 then (A | B) != 0 is always true. */
625 if (cond_code == NE_EXPR)
626 return boolean_true_node;
627 /* If B != 0 then (A | B) == 0 is always false. */
628 if (cond_code == EQ_EXPR)
629 return boolean_false_node;
632 if (res1 != NULL_TREE && res2 != NULL_TREE)
634 if (rhs_code == BIT_AND_EXPR
635 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
636 && integer_nonzerop (res1)
637 && integer_nonzerop (res2))
639 /* If A != 0 and B != 0 then (bool)(A & B) != 0 is true. */
640 if (cond_code == NE_EXPR)
641 return boolean_true_node;
642 /* If A != 0 and B != 0 then (bool)(A & B) == 0 is false. */
643 if (cond_code == EQ_EXPR)
644 return boolean_false_node;
647 if (rhs_code == BIT_IOR_EXPR
648 && integer_zerop (res1)
649 && integer_zerop (res2))
651 /* If A == 0 and B == 0 then (A | B) != 0 is false. */
652 if (cond_code == NE_EXPR)
653 return boolean_false_node;
654 /* If A == 0 and B == 0 then (A | B) == 0 is true. */
655 if (cond_code == EQ_EXPR)
656 return boolean_true_node;
660 /* Handle (A CMP B) CMP 0. */
661 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt))
662 == tcc_comparison)
664 tree rhs1 = gimple_assign_rhs1 (def_stmt);
665 tree rhs2 = gimple_assign_rhs2 (def_stmt);
667 tree_code new_cond = gimple_assign_rhs_code (def_stmt);
668 if (cond_code == EQ_EXPR)
669 new_cond = invert_tree_comparison (new_cond, false);
671 tree res
672 = simplify_control_stmt_condition_1 (e, def_stmt, avail_exprs_stack,
673 rhs1, new_cond, rhs2,
674 dummy_cond, simplify,
675 handle_dominating_asserts,
676 limit - 1);
677 if (res != NULL_TREE && is_gimple_min_invariant (res))
678 return res;
682 if (handle_dominating_asserts)
684 /* Now see if the operand was consumed by an ASSERT_EXPR
685 which dominates E->src. If so, we want to replace the
686 operand with the LHS of the ASSERT_EXPR. */
687 if (TREE_CODE (op0) == SSA_NAME)
688 op0 = lhs_of_dominating_assert (op0, e->src, stmt);
690 if (TREE_CODE (op1) == SSA_NAME)
691 op1 = lhs_of_dominating_assert (op1, e->src, stmt);
694 gimple_cond_set_code (dummy_cond, cond_code);
695 gimple_cond_set_lhs (dummy_cond, op0);
696 gimple_cond_set_rhs (dummy_cond, op1);
698 /* We absolutely do not care about any type conversions
699 we only care about a zero/nonzero value. */
700 fold_defer_overflow_warnings ();
702 tree res = fold_binary (cond_code, boolean_type_node, op0, op1);
703 if (res)
704 while (CONVERT_EXPR_P (res))
705 res = TREE_OPERAND (res, 0);
707 fold_undefer_overflow_warnings ((res && is_gimple_min_invariant (res)),
708 stmt, WARN_STRICT_OVERFLOW_CONDITIONAL);
710 /* If we have not simplified the condition down to an invariant,
711 then use the pass specific callback to simplify the condition. */
712 if (!res
713 || !is_gimple_min_invariant (res))
714 res = (*simplify) (dummy_cond, stmt, avail_exprs_stack);
716 return res;
719 /* Copy debug stmts from DEST's chain of single predecessors up to
720 SRC, so that we don't lose the bindings as PHI nodes are introduced
721 when DEST gains new predecessors. */
722 void
723 propagate_threaded_block_debug_into (basic_block dest, basic_block src)
725 if (!MAY_HAVE_DEBUG_STMTS)
726 return;
728 if (!single_pred_p (dest))
729 return;
731 gcc_checking_assert (dest != src);
733 gimple_stmt_iterator gsi = gsi_after_labels (dest);
734 int i = 0;
735 const int alloc_count = 16; // ?? Should this be a PARAM?
737 /* Estimate the number of debug vars overridden in the beginning of
738 DEST, to tell how many we're going to need to begin with. */
739 for (gimple_stmt_iterator si = gsi;
740 i * 4 <= alloc_count * 3 && !gsi_end_p (si); gsi_next (&si))
742 gimple *stmt = gsi_stmt (si);
743 if (!is_gimple_debug (stmt))
744 break;
745 i++;
748 auto_vec<tree, alloc_count> fewvars;
749 hash_set<tree> *vars = NULL;
751 /* If we're already starting with 3/4 of alloc_count, go for a
752 hash_set, otherwise start with an unordered stack-allocated
753 VEC. */
754 if (i * 4 > alloc_count * 3)
755 vars = new hash_set<tree>;
757 /* Now go through the initial debug stmts in DEST again, this time
758 actually inserting in VARS or FEWVARS. Don't bother checking for
759 duplicates in FEWVARS. */
760 for (gimple_stmt_iterator si = gsi; !gsi_end_p (si); gsi_next (&si))
762 gimple *stmt = gsi_stmt (si);
763 if (!is_gimple_debug (stmt))
764 break;
766 tree var;
768 if (gimple_debug_bind_p (stmt))
769 var = gimple_debug_bind_get_var (stmt);
770 else if (gimple_debug_source_bind_p (stmt))
771 var = gimple_debug_source_bind_get_var (stmt);
772 else
773 gcc_unreachable ();
775 if (vars)
776 vars->add (var);
777 else
778 fewvars.quick_push (var);
781 basic_block bb = dest;
785 bb = single_pred (bb);
786 for (gimple_stmt_iterator si = gsi_last_bb (bb);
787 !gsi_end_p (si); gsi_prev (&si))
789 gimple *stmt = gsi_stmt (si);
790 if (!is_gimple_debug (stmt))
791 continue;
793 tree var;
795 if (gimple_debug_bind_p (stmt))
796 var = gimple_debug_bind_get_var (stmt);
797 else if (gimple_debug_source_bind_p (stmt))
798 var = gimple_debug_source_bind_get_var (stmt);
799 else
800 gcc_unreachable ();
802 /* Discard debug bind overlaps. ??? Unlike stmts from src,
803 copied into a new block that will precede BB, debug bind
804 stmts in bypassed BBs may actually be discarded if
805 they're overwritten by subsequent debug bind stmts, which
806 might be a problem once we introduce stmt frontier notes
807 or somesuch. Adding `&& bb == src' to the condition
808 below will preserve all potentially relevant debug
809 notes. */
810 if (vars && vars->add (var))
811 continue;
812 else if (!vars)
814 int i = fewvars.length ();
815 while (i--)
816 if (fewvars[i] == var)
817 break;
818 if (i >= 0)
819 continue;
821 if (fewvars.length () < (unsigned) alloc_count)
822 fewvars.quick_push (var);
823 else
825 vars = new hash_set<tree>;
826 for (i = 0; i < alloc_count; i++)
827 vars->add (fewvars[i]);
828 fewvars.release ();
829 vars->add (var);
833 stmt = gimple_copy (stmt);
834 /* ??? Should we drop the location of the copy to denote
835 they're artificial bindings? */
836 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
839 while (bb != src && single_pred_p (bb));
841 if (vars)
842 delete vars;
843 else if (fewvars.exists ())
844 fewvars.release ();
847 /* See if TAKEN_EDGE->dest is a threadable block with no side effecs (ie, it
848 need not be duplicated as part of the CFG/SSA updating process).
850 If it is threadable, add it to PATH and VISITED and recurse, ultimately
851 returning TRUE from the toplevel call. Otherwise do nothing and
852 return false.
854 DUMMY_COND, HANDLE_DOMINATING_ASSERTS and SIMPLIFY are used to
855 try and simplify the condition at the end of TAKEN_EDGE->dest.
857 The available expression table is referenced via AVAIL_EXPRS_STACK. */
859 static bool
860 thread_around_empty_blocks (edge taken_edge,
861 gcond *dummy_cond,
862 class avail_exprs_stack *avail_exprs_stack,
863 bool handle_dominating_asserts,
864 pfn_simplify simplify,
865 bitmap visited,
866 vec<jump_thread_edge *> *path)
868 basic_block bb = taken_edge->dest;
869 gimple_stmt_iterator gsi;
870 gimple *stmt;
871 tree cond;
873 /* The key property of these blocks is that they need not be duplicated
874 when threading. Thus they can not have visible side effects such
875 as PHI nodes. */
876 if (!gsi_end_p (gsi_start_phis (bb)))
877 return false;
879 /* Skip over DEBUG statements at the start of the block. */
880 gsi = gsi_start_nondebug_bb (bb);
882 /* If the block has no statements, but does have a single successor, then
883 it's just a forwarding block and we can thread through it trivially.
885 However, note that just threading through empty blocks with single
886 successors is not inherently profitable. For the jump thread to
887 be profitable, we must avoid a runtime conditional.
889 By taking the return value from the recursive call, we get the
890 desired effect of returning TRUE when we found a profitable jump
891 threading opportunity and FALSE otherwise.
893 This is particularly important when this routine is called after
894 processing a joiner block. Returning TRUE too aggressively in
895 that case results in pointless duplication of the joiner block. */
896 if (gsi_end_p (gsi))
898 if (single_succ_p (bb))
900 taken_edge = single_succ_edge (bb);
902 if ((taken_edge->flags & EDGE_DFS_BACK) != 0)
903 return false;
905 if (!bitmap_bit_p (visited, taken_edge->dest->index))
907 jump_thread_edge *x
908 = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
909 path->safe_push (x);
910 bitmap_set_bit (visited, taken_edge->dest->index);
911 return thread_around_empty_blocks (taken_edge,
912 dummy_cond,
913 avail_exprs_stack,
914 handle_dominating_asserts,
915 simplify,
916 visited,
917 path);
921 /* We have a block with no statements, but multiple successors? */
922 return false;
925 /* The only real statements this block can have are a control
926 flow altering statement. Anything else stops the thread. */
927 stmt = gsi_stmt (gsi);
928 if (gimple_code (stmt) != GIMPLE_COND
929 && gimple_code (stmt) != GIMPLE_GOTO
930 && gimple_code (stmt) != GIMPLE_SWITCH)
931 return false;
933 /* Extract and simplify the condition. */
934 cond = simplify_control_stmt_condition (taken_edge, stmt,
935 avail_exprs_stack, dummy_cond,
936 simplify, handle_dominating_asserts);
938 /* If the condition can be statically computed and we have not already
939 visited the destination edge, then add the taken edge to our thread
940 path. */
941 if (cond && is_gimple_min_invariant (cond))
943 taken_edge = find_taken_edge (bb, cond);
945 if ((taken_edge->flags & EDGE_DFS_BACK) != 0)
946 return false;
948 if (bitmap_bit_p (visited, taken_edge->dest->index))
949 return false;
950 bitmap_set_bit (visited, taken_edge->dest->index);
952 jump_thread_edge *x
953 = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
954 path->safe_push (x);
956 thread_around_empty_blocks (taken_edge,
957 dummy_cond,
958 avail_exprs_stack,
959 handle_dominating_asserts,
960 simplify,
961 visited,
962 path);
963 return true;
966 return false;
969 /* We are exiting E->src, see if E->dest ends with a conditional
970 jump which has a known value when reached via E.
972 E->dest can have arbitrary side effects which, if threading is
973 successful, will be maintained.
975 Special care is necessary if E is a back edge in the CFG as we
976 may have already recorded equivalences for E->dest into our
977 various tables, including the result of the conditional at
978 the end of E->dest. Threading opportunities are severely
979 limited in that case to avoid short-circuiting the loop
980 incorrectly.
982 DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
983 to avoid allocating memory.
985 HANDLE_DOMINATING_ASSERTS is true if we should try to replace operands of
986 the simplified condition with left-hand sides of ASSERT_EXPRs they are
987 used in.
989 STACK is used to undo temporary equivalences created during the walk of
990 E->dest.
992 SIMPLIFY is a pass-specific function used to simplify statements.
994 Our caller is responsible for restoring the state of the expression
995 and const_and_copies stacks.
997 Positive return value is success. Zero return value is failure, but
998 the block can still be duplicated as a joiner in a jump thread path,
999 negative indicates the block should not be duplicated and thus is not
1000 suitable for a joiner in a jump threading path. */
1002 static int
1003 thread_through_normal_block (edge e,
1004 gcond *dummy_cond,
1005 bool handle_dominating_asserts,
1006 const_and_copies *const_and_copies,
1007 avail_exprs_stack *avail_exprs_stack,
1008 pfn_simplify simplify,
1009 vec<jump_thread_edge *> *path,
1010 bitmap visited)
1012 /* We want to record any equivalences created by traversing E. */
1013 if (!handle_dominating_asserts)
1014 record_temporary_equivalences (e, const_and_copies, avail_exprs_stack);
1016 /* PHIs create temporary equivalences.
1017 Note that if we found a PHI that made the block non-threadable, then
1018 we need to bubble that up to our caller in the same manner we do
1019 when we prematurely stop processing statements below. */
1020 if (!record_temporary_equivalences_from_phis (e, const_and_copies))
1021 return -1;
1023 /* Now walk each statement recording any context sensitive
1024 temporary equivalences we can detect. */
1025 gimple *stmt
1026 = record_temporary_equivalences_from_stmts_at_dest (e, const_and_copies,
1027 avail_exprs_stack,
1028 simplify);
1030 /* There's two reasons STMT might be null, and distinguishing
1031 between them is important.
1033 First the block may not have had any statements. For example, it
1034 might have some PHIs and unconditionally transfer control elsewhere.
1035 Such blocks are suitable for jump threading, particularly as a
1036 joiner block.
1038 The second reason would be if we did not process all the statements
1039 in the block (because there were too many to make duplicating the
1040 block profitable. If we did not look at all the statements, then
1041 we may not have invalidated everything needing invalidation. Thus
1042 we must signal to our caller that this block is not suitable for
1043 use as a joiner in a threading path. */
1044 if (!stmt)
1046 /* First case. The statement simply doesn't have any instructions, but
1047 does have PHIs. */
1048 if (gsi_end_p (gsi_start_nondebug_bb (e->dest))
1049 && !gsi_end_p (gsi_start_phis (e->dest)))
1050 return 0;
1052 /* Second case. */
1053 return -1;
1056 /* If we stopped at a COND_EXPR or SWITCH_EXPR, see if we know which arm
1057 will be taken. */
1058 if (gimple_code (stmt) == GIMPLE_COND
1059 || gimple_code (stmt) == GIMPLE_GOTO
1060 || gimple_code (stmt) == GIMPLE_SWITCH)
1062 tree cond;
1064 /* Extract and simplify the condition. */
1065 cond = simplify_control_stmt_condition (e, stmt, avail_exprs_stack,
1066 dummy_cond, simplify,
1067 handle_dominating_asserts);
1069 if (!cond)
1070 return 0;
1072 if (is_gimple_min_invariant (cond))
1074 edge taken_edge = find_taken_edge (e->dest, cond);
1075 basic_block dest = (taken_edge ? taken_edge->dest : NULL);
1077 /* DEST could be NULL for a computed jump to an absolute
1078 address. */
1079 if (dest == NULL
1080 || dest == e->dest
1081 || (taken_edge->flags & EDGE_DFS_BACK) != 0
1082 || bitmap_bit_p (visited, dest->index))
1083 return 0;
1085 /* Only push the EDGE_START_JUMP_THREAD marker if this is
1086 first edge on the path. */
1087 if (path->length () == 0)
1089 jump_thread_edge *x
1090 = new jump_thread_edge (e, EDGE_START_JUMP_THREAD);
1091 path->safe_push (x);
1094 jump_thread_edge *x
1095 = new jump_thread_edge (taken_edge, EDGE_COPY_SRC_BLOCK);
1096 path->safe_push (x);
1098 /* See if we can thread through DEST as well, this helps capture
1099 secondary effects of threading without having to re-run DOM or
1100 VRP.
1102 We don't want to thread back to a block we have already
1103 visited. This may be overly conservative. */
1104 bitmap_set_bit (visited, dest->index);
1105 bitmap_set_bit (visited, e->dest->index);
1106 thread_around_empty_blocks (taken_edge,
1107 dummy_cond,
1108 avail_exprs_stack,
1109 handle_dominating_asserts,
1110 simplify,
1111 visited,
1112 path);
1113 return 1;
1116 return 0;
1119 /* We are exiting E->src, see if E->dest ends with a conditional
1120 jump which has a known value when reached via E.
1122 DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
1123 to avoid allocating memory.
1125 HANDLE_DOMINATING_ASSERTS is true if we should try to replace operands of
1126 the simplified condition with left-hand sides of ASSERT_EXPRs they are
1127 used in.
1129 CONST_AND_COPIES is used to undo temporary equivalences created during the
1130 walk of E->dest.
1132 The available expression table is referenced vai AVAIL_EXPRS_STACK.
1134 SIMPLIFY is a pass-specific function used to simplify statements. */
1136 void
1137 thread_across_edge (gcond *dummy_cond,
1138 edge e,
1139 bool handle_dominating_asserts,
1140 class const_and_copies *const_and_copies,
1141 class avail_exprs_stack *avail_exprs_stack,
1142 tree (*simplify) (gimple *, gimple *,
1143 class avail_exprs_stack *))
1145 bitmap visited = BITMAP_ALLOC (NULL);
1147 stmt_count = 0;
1149 vec<jump_thread_edge *> *path = new vec<jump_thread_edge *> ();
1150 bitmap_clear (visited);
1151 bitmap_set_bit (visited, e->src->index);
1152 bitmap_set_bit (visited, e->dest->index);
1154 int threaded;
1155 if ((e->flags & EDGE_DFS_BACK) == 0)
1156 threaded = thread_through_normal_block (e, dummy_cond,
1157 handle_dominating_asserts,
1158 const_and_copies,
1159 avail_exprs_stack,
1160 simplify, path,
1161 visited);
1162 else
1163 threaded = 0;
1165 if (threaded > 0)
1167 propagate_threaded_block_debug_into (path->last ()->e->dest,
1168 e->dest);
1169 const_and_copies->pop_to_marker ();
1170 BITMAP_FREE (visited);
1171 register_jump_thread (path);
1172 return;
1174 else
1176 /* Negative and zero return values indicate no threading was possible,
1177 thus there should be no edges on the thread path and no need to walk
1178 through the vector entries. */
1179 gcc_assert (path->length () == 0);
1180 path->release ();
1181 delete path;
1183 /* A negative status indicates the target block was deemed too big to
1184 duplicate. Just quit now rather than trying to use the block as
1185 a joiner in a jump threading path.
1187 This prevents unnecessary code growth, but more importantly if we
1188 do not look at all the statements in the block, then we may have
1189 missed some invalidations if we had traversed a backedge! */
1190 if (threaded < 0)
1192 BITMAP_FREE (visited);
1193 const_and_copies->pop_to_marker ();
1194 return;
1198 /* We were unable to determine what out edge from E->dest is taken. However,
1199 we might still be able to thread through successors of E->dest. This
1200 often occurs when E->dest is a joiner block which then fans back out
1201 based on redundant tests.
1203 If so, we'll copy E->dest and redirect the appropriate predecessor to
1204 the copy. Within the copy of E->dest, we'll thread one or more edges
1205 to points deeper in the CFG.
1207 This is a stopgap until we have a more structured approach to path
1208 isolation. */
1210 edge taken_edge;
1211 edge_iterator ei;
1212 bool found;
1214 /* If E->dest has abnormal outgoing edges, then there's no guarantee
1215 we can safely redirect any of the edges. Just punt those cases. */
1216 FOR_EACH_EDGE (taken_edge, ei, e->dest->succs)
1217 if (taken_edge->flags & EDGE_ABNORMAL)
1219 const_and_copies->pop_to_marker ();
1220 BITMAP_FREE (visited);
1221 return;
1224 /* Look at each successor of E->dest to see if we can thread through it. */
1225 FOR_EACH_EDGE (taken_edge, ei, e->dest->succs)
1227 if ((e->flags & EDGE_DFS_BACK) != 0
1228 || (taken_edge->flags & EDGE_DFS_BACK) != 0)
1229 continue;
1231 /* Push a fresh marker so we can unwind the equivalences created
1232 for each of E->dest's successors. */
1233 const_and_copies->push_marker ();
1234 if (avail_exprs_stack)
1235 avail_exprs_stack->push_marker ();
1237 /* Avoid threading to any block we have already visited. */
1238 bitmap_clear (visited);
1239 bitmap_set_bit (visited, e->src->index);
1240 bitmap_set_bit (visited, e->dest->index);
1241 bitmap_set_bit (visited, taken_edge->dest->index);
1242 vec<jump_thread_edge *> *path = new vec<jump_thread_edge *> ();
1244 /* Record whether or not we were able to thread through a successor
1245 of E->dest. */
1246 jump_thread_edge *x = new jump_thread_edge (e, EDGE_START_JUMP_THREAD);
1247 path->safe_push (x);
1249 x = new jump_thread_edge (taken_edge, EDGE_COPY_SRC_JOINER_BLOCK);
1250 path->safe_push (x);
1251 found = false;
1252 found = thread_around_empty_blocks (taken_edge,
1253 dummy_cond,
1254 avail_exprs_stack,
1255 handle_dominating_asserts,
1256 simplify,
1257 visited,
1258 path);
1260 if (!found)
1261 found = thread_through_normal_block (path->last ()->e, dummy_cond,
1262 handle_dominating_asserts,
1263 const_and_copies,
1264 avail_exprs_stack,
1265 simplify, path,
1266 visited) > 0;
1268 /* If we were able to thread through a successor of E->dest, then
1269 record the jump threading opportunity. */
1270 if (found)
1272 propagate_threaded_block_debug_into (path->last ()->e->dest,
1273 taken_edge->dest);
1274 register_jump_thread (path);
1276 else
1277 delete_jump_thread_path (path);
1279 /* And unwind the equivalence table. */
1280 if (avail_exprs_stack)
1281 avail_exprs_stack->pop_to_marker ();
1282 const_and_copies->pop_to_marker ();
1284 BITMAP_FREE (visited);
1287 const_and_copies->pop_to_marker ();