/cp
[official-gcc.git] / gcc / tree-ssa-threadedge.c
blob4f839910a84c686644c59daf3c2b63e74250eeb1
1 /* SSA Jump Threading
2 Copyright (C) 2005-2015 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 "tm.h"
25 #include "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "flags.h"
37 #include "tm_p.h"
38 #include "predict.h"
39 #include "hard-reg-set.h"
40 #include "input.h"
41 #include "function.h"
42 #include "dominance.h"
43 #include "basic-block.h"
44 #include "cfgloop.h"
45 #include "timevar.h"
46 #include "dumpfile.h"
47 #include "tree-ssa-alias.h"
48 #include "internal-fn.h"
49 #include "gimple-expr.h"
50 #include "is-a.h"
51 #include "gimple.h"
52 #include "gimple-iterator.h"
53 #include "gimple-ssa.h"
54 #include "tree-cfg.h"
55 #include "tree-phinodes.h"
56 #include "ssa-iterators.h"
57 #include "stringpool.h"
58 #include "tree-ssanames.h"
59 #include "tree-ssa-propagate.h"
60 #include "tree-ssa-threadupdate.h"
61 #include "langhooks.h"
62 #include "params.h"
63 #include "tree-ssa-threadedge.h"
64 #include "tree-ssa-loop.h"
65 #include "builtins.h"
66 #include "cfg.h"
67 #include "cfganal.h"
69 /* To avoid code explosion due to jump threading, we limit the
70 number of statements we are going to copy. This variable
71 holds the number of statements currently seen that we'll have
72 to copy as part of the jump threading process. */
73 static int stmt_count;
75 /* Array to record value-handles per SSA_NAME. */
76 vec<tree> ssa_name_values;
78 /* Set the value for the SSA name NAME to VALUE. */
80 void
81 set_ssa_name_value (tree name, tree value)
83 if (SSA_NAME_VERSION (name) >= ssa_name_values.length ())
84 ssa_name_values.safe_grow_cleared (SSA_NAME_VERSION (name) + 1);
85 if (value && TREE_OVERFLOW_P (value))
86 value = drop_tree_overflow (value);
87 ssa_name_values[SSA_NAME_VERSION (name)] = value;
90 /* Initialize the per SSA_NAME value-handles array. Returns it. */
91 void
92 threadedge_initialize_values (void)
94 gcc_assert (!ssa_name_values.exists ());
95 ssa_name_values.create (num_ssa_names);
98 /* Free the per SSA_NAME value-handle array. */
99 void
100 threadedge_finalize_values (void)
102 ssa_name_values.release ();
105 /* Return TRUE if we may be able to thread an incoming edge into
106 BB to an outgoing edge from BB. Return FALSE otherwise. */
108 bool
109 potentially_threadable_block (basic_block bb)
111 gimple_stmt_iterator gsi;
113 /* If BB has a single successor or a single predecessor, then
114 there is no threading opportunity. */
115 if (single_succ_p (bb) || single_pred_p (bb))
116 return false;
118 /* If BB does not end with a conditional, switch or computed goto,
119 then there is no threading opportunity. */
120 gsi = gsi_last_bb (bb);
121 if (gsi_end_p (gsi)
122 || ! gsi_stmt (gsi)
123 || (gimple_code (gsi_stmt (gsi)) != GIMPLE_COND
124 && gimple_code (gsi_stmt (gsi)) != GIMPLE_GOTO
125 && gimple_code (gsi_stmt (gsi)) != GIMPLE_SWITCH))
126 return false;
128 return true;
131 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
132 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
133 BB. If no such ASSERT_EXPR is found, return OP. */
135 static tree
136 lhs_of_dominating_assert (tree op, basic_block bb, gimple stmt)
138 imm_use_iterator imm_iter;
139 gimple use_stmt;
140 use_operand_p use_p;
142 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
144 use_stmt = USE_STMT (use_p);
145 if (use_stmt != stmt
146 && gimple_assign_single_p (use_stmt)
147 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ASSERT_EXPR
148 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) == op
149 && dominated_by_p (CDI_DOMINATORS, bb, gimple_bb (use_stmt)))
151 return gimple_assign_lhs (use_stmt);
154 return op;
157 /* We record temporary equivalences created by PHI nodes or
158 statements within the target block. Doing so allows us to
159 identify more jump threading opportunities, even in blocks
160 with side effects.
162 We keep track of those temporary equivalences in a stack
163 structure so that we can unwind them when we're done processing
164 a particular edge. This routine handles unwinding the data
165 structures. */
167 static void
168 remove_temporary_equivalences (vec<tree> *stack)
170 while (stack->length () > 0)
172 tree prev_value, dest;
174 dest = stack->pop ();
176 /* A NULL value indicates we should stop unwinding, otherwise
177 pop off the next entry as they're recorded in pairs. */
178 if (dest == NULL)
179 break;
181 prev_value = stack->pop ();
182 set_ssa_name_value (dest, prev_value);
186 /* Record a temporary equivalence, saving enough information so that
187 we can restore the state of recorded equivalences when we're
188 done processing the current edge. */
190 static void
191 record_temporary_equivalence (tree x, tree y, vec<tree> *stack)
193 tree prev_x = SSA_NAME_VALUE (x);
195 /* Y may be NULL if we are invalidating entries in the table. */
196 if (y && TREE_CODE (y) == SSA_NAME)
198 tree tmp = SSA_NAME_VALUE (y);
199 y = tmp ? tmp : y;
202 set_ssa_name_value (x, y);
203 stack->reserve (2);
204 stack->quick_push (prev_x);
205 stack->quick_push (x);
208 /* Record temporary equivalences created by PHIs at the target of the
209 edge E. Record unwind information for the equivalences onto STACK.
211 If a PHI which prevents threading is encountered, then return FALSE
212 indicating we should not thread this edge, else return TRUE.
214 If SRC_MAP/DST_MAP exist, then mark the source and destination SSA_NAMEs
215 of any equivalences recorded. We use this to make invalidation after
216 traversing back edges less painful. */
218 static bool
219 record_temporary_equivalences_from_phis (edge e, vec<tree> *stack)
221 gphi_iterator gsi;
223 /* Each PHI creates a temporary equivalence, record them.
224 These are context sensitive equivalences and will be removed
225 later. */
226 for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
228 gphi *phi = gsi.phi ();
229 tree src = PHI_ARG_DEF_FROM_EDGE (phi, e);
230 tree dst = gimple_phi_result (phi);
232 /* If the desired argument is not the same as this PHI's result
233 and it is set by a PHI in E->dest, then we can not thread
234 through E->dest. */
235 if (src != dst
236 && TREE_CODE (src) == SSA_NAME
237 && gimple_code (SSA_NAME_DEF_STMT (src)) == GIMPLE_PHI
238 && gimple_bb (SSA_NAME_DEF_STMT (src)) == e->dest)
239 return false;
241 /* We consider any non-virtual PHI as a statement since it
242 count result in a constant assignment or copy operation. */
243 if (!virtual_operand_p (dst))
244 stmt_count++;
246 record_temporary_equivalence (dst, src, stack);
248 return true;
251 /* Fold the RHS of an assignment statement and return it as a tree.
252 May return NULL_TREE if no simplification is possible. */
254 static tree
255 fold_assignment_stmt (gimple stmt)
257 enum tree_code subcode = gimple_assign_rhs_code (stmt);
259 switch (get_gimple_rhs_class (subcode))
261 case GIMPLE_SINGLE_RHS:
262 return fold (gimple_assign_rhs1 (stmt));
264 case GIMPLE_UNARY_RHS:
266 tree lhs = gimple_assign_lhs (stmt);
267 tree op0 = gimple_assign_rhs1 (stmt);
268 return fold_unary (subcode, TREE_TYPE (lhs), op0);
271 case GIMPLE_BINARY_RHS:
273 tree lhs = gimple_assign_lhs (stmt);
274 tree op0 = gimple_assign_rhs1 (stmt);
275 tree op1 = gimple_assign_rhs2 (stmt);
276 return fold_binary (subcode, TREE_TYPE (lhs), op0, op1);
279 case GIMPLE_TERNARY_RHS:
281 tree lhs = gimple_assign_lhs (stmt);
282 tree op0 = gimple_assign_rhs1 (stmt);
283 tree op1 = gimple_assign_rhs2 (stmt);
284 tree op2 = gimple_assign_rhs3 (stmt);
286 /* Sadly, we have to handle conditional assignments specially
287 here, because fold expects all the operands of an expression
288 to be folded before the expression itself is folded, but we
289 can't just substitute the folded condition here. */
290 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
291 op0 = fold (op0);
293 return fold_ternary (subcode, TREE_TYPE (lhs), op0, op1, op2);
296 default:
297 gcc_unreachable ();
301 /* A new value has been assigned to LHS. If necessary, invalidate any
302 equivalences that are no longer valid. This includes invaliding
303 LHS and any objects that are currently equivalent to LHS.
305 Finding the objects that are currently marked as equivalent to LHS
306 is a bit tricky. We could walk the ssa names and see if any have
307 SSA_NAME_VALUE that is the same as LHS. That's expensive.
309 However, it's far more efficient to look at the unwinding stack as
310 that will have all context sensitive equivalences which are the only
311 ones that we really have to worry about here. */
312 static void
313 invalidate_equivalences (tree lhs, vec<tree> *stack)
316 /* The stack is an unwinding stack. If the current element is NULL
317 then it's a "stop unwinding" marker. Else the current marker is
318 the SSA_NAME with an equivalence and the prior entry in the stack
319 is what the current element is equivalent to. */
320 for (int i = stack->length() - 1; i >= 0; i--)
322 /* Ignore the stop unwinding markers. */
323 if ((*stack)[i] == NULL)
324 continue;
326 /* We want to check the current value of stack[i] to see if
327 it matches LHS. If so, then invalidate. */
328 if (SSA_NAME_VALUE ((*stack)[i]) == lhs)
329 record_temporary_equivalence ((*stack)[i], NULL_TREE, stack);
331 /* Remember, we're dealing with two elements in this case. */
332 i--;
335 /* And invalidate any known value for LHS itself. */
336 if (SSA_NAME_VALUE (lhs))
337 record_temporary_equivalence (lhs, NULL_TREE, stack);
340 /* Try to simplify each statement in E->dest, ultimately leading to
341 a simplification of the COND_EXPR at the end of E->dest.
343 Record unwind information for temporary equivalences onto STACK.
345 Use SIMPLIFY (a pointer to a callback function) to further simplify
346 statements using pass specific information.
348 We might consider marking just those statements which ultimately
349 feed the COND_EXPR. It's not clear if the overhead of bookkeeping
350 would be recovered by trying to simplify fewer statements.
352 If we are able to simplify a statement into the form
353 SSA_NAME = (SSA_NAME | gimple invariant), then we can record
354 a context sensitive equivalence which may help us simplify
355 later statements in E->dest. */
357 static gimple
358 record_temporary_equivalences_from_stmts_at_dest (edge e,
359 vec<tree> *stack,
360 tree (*simplify) (gimple,
361 gimple),
362 bool backedge_seen)
364 gimple stmt = NULL;
365 gimple_stmt_iterator gsi;
366 int max_stmt_count;
368 max_stmt_count = PARAM_VALUE (PARAM_MAX_JUMP_THREAD_DUPLICATION_STMTS);
370 /* Walk through each statement in the block recording equivalences
371 we discover. Note any equivalences we discover are context
372 sensitive (ie, are dependent on traversing E) and must be unwound
373 when we're finished processing E. */
374 for (gsi = gsi_start_bb (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
376 tree cached_lhs = NULL;
378 stmt = gsi_stmt (gsi);
380 /* Ignore empty statements and labels. */
381 if (gimple_code (stmt) == GIMPLE_NOP
382 || gimple_code (stmt) == GIMPLE_LABEL
383 || is_gimple_debug (stmt))
384 continue;
386 /* If the statement has volatile operands, then we assume we
387 can not thread through this block. This is overly
388 conservative in some ways. */
389 if (gimple_code (stmt) == GIMPLE_ASM
390 && gimple_asm_volatile_p (as_a <gasm *> (stmt)))
391 return NULL;
393 /* If duplicating this block is going to cause too much code
394 expansion, then do not thread through this block. */
395 stmt_count++;
396 if (stmt_count > max_stmt_count)
397 return NULL;
399 /* If this is not a statement that sets an SSA_NAME to a new
400 value, then do not try to simplify this statement as it will
401 not simplify in any way that is helpful for jump threading. */
402 if ((gimple_code (stmt) != GIMPLE_ASSIGN
403 || TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
404 && (gimple_code (stmt) != GIMPLE_CALL
405 || gimple_call_lhs (stmt) == NULL_TREE
406 || TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME))
408 /* STMT might still have DEFS and we need to invalidate any known
409 equivalences for them.
411 Consider if STMT is a GIMPLE_ASM with one or more outputs that
412 feeds a conditional inside a loop. We might derive an equivalence
413 due to the conditional. */
414 tree op;
415 ssa_op_iter iter;
417 if (backedge_seen)
418 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
419 invalidate_equivalences (op, stack);
421 continue;
424 /* The result of __builtin_object_size depends on all the arguments
425 of a phi node. Temporarily using only one edge produces invalid
426 results. For example
428 if (x < 6)
429 goto l;
430 else
431 goto l;
434 r = PHI <&w[2].a[1](2), &a.a[6](3)>
435 __builtin_object_size (r, 0)
437 The result of __builtin_object_size is defined to be the maximum of
438 remaining bytes. If we use only one edge on the phi, the result will
439 change to be the remaining bytes for the corresponding phi argument.
441 Similarly for __builtin_constant_p:
443 r = PHI <1(2), 2(3)>
444 __builtin_constant_p (r)
446 Both PHI arguments are constant, but x ? 1 : 2 is still not
447 constant. */
449 if (is_gimple_call (stmt))
451 tree fndecl = gimple_call_fndecl (stmt);
452 if (fndecl
453 && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_OBJECT_SIZE
454 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P))
456 if (backedge_seen)
458 tree lhs = gimple_get_lhs (stmt);
459 invalidate_equivalences (lhs, stack);
461 continue;
465 /* At this point we have a statement which assigns an RHS to an
466 SSA_VAR on the LHS. We want to try and simplify this statement
467 to expose more context sensitive equivalences which in turn may
468 allow us to simplify the condition at the end of the loop.
470 Handle simple copy operations as well as implied copies from
471 ASSERT_EXPRs. */
472 if (gimple_assign_single_p (stmt)
473 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
474 cached_lhs = gimple_assign_rhs1 (stmt);
475 else if (gimple_assign_single_p (stmt)
476 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
477 cached_lhs = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
478 else
480 /* A statement that is not a trivial copy or ASSERT_EXPR.
481 We're going to temporarily copy propagate the operands
482 and see if that allows us to simplify this statement. */
483 tree *copy;
484 ssa_op_iter iter;
485 use_operand_p use_p;
486 unsigned int num, i = 0;
488 num = NUM_SSA_OPERANDS (stmt, (SSA_OP_USE | SSA_OP_VUSE));
489 copy = XCNEWVEC (tree, num);
491 /* Make a copy of the uses & vuses into USES_COPY, then cprop into
492 the operands. */
493 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE | SSA_OP_VUSE)
495 tree tmp = NULL;
496 tree use = USE_FROM_PTR (use_p);
498 copy[i++] = use;
499 if (TREE_CODE (use) == SSA_NAME)
500 tmp = SSA_NAME_VALUE (use);
501 if (tmp)
502 SET_USE (use_p, tmp);
505 /* Try to fold/lookup the new expression. Inserting the
506 expression into the hash table is unlikely to help. */
507 if (is_gimple_call (stmt))
508 cached_lhs = fold_call_stmt (as_a <gcall *> (stmt), false);
509 else
510 cached_lhs = fold_assignment_stmt (stmt);
512 if (!cached_lhs
513 || (TREE_CODE (cached_lhs) != SSA_NAME
514 && !is_gimple_min_invariant (cached_lhs)))
515 cached_lhs = (*simplify) (stmt, stmt);
517 /* Restore the statement's original uses/defs. */
518 i = 0;
519 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE | SSA_OP_VUSE)
520 SET_USE (use_p, copy[i++]);
522 free (copy);
525 /* Record the context sensitive equivalence if we were able
526 to simplify this statement.
528 If we have traversed a backedge at some point during threading,
529 then always enter something here. Either a real equivalence,
530 or a NULL_TREE equivalence which is effectively invalidation of
531 prior equivalences. */
532 if (cached_lhs
533 && (TREE_CODE (cached_lhs) == SSA_NAME
534 || is_gimple_min_invariant (cached_lhs)))
535 record_temporary_equivalence (gimple_get_lhs (stmt), cached_lhs, stack);
536 else if (backedge_seen)
537 invalidate_equivalences (gimple_get_lhs (stmt), stack);
539 return stmt;
542 /* Once we have passed a backedge in the CFG when threading, we do not want to
543 utilize edge equivalences for simplification purpose. They are no longer
544 necessarily valid. We use this callback rather than the ones provided by
545 DOM/VRP to achieve that effect. */
546 static tree
547 dummy_simplify (gimple stmt1 ATTRIBUTE_UNUSED, gimple stmt2 ATTRIBUTE_UNUSED)
549 return NULL_TREE;
552 /* Simplify the control statement at the end of the block E->dest.
554 To avoid allocating memory unnecessarily, a scratch GIMPLE_COND
555 is available to use/clobber in DUMMY_COND.
557 Use SIMPLIFY (a pointer to a callback function) to further simplify
558 a condition using pass specific information.
560 Return the simplified condition or NULL if simplification could
561 not be performed. */
563 static tree
564 simplify_control_stmt_condition (edge e,
565 gimple stmt,
566 gcond *dummy_cond,
567 tree (*simplify) (gimple, gimple),
568 bool handle_dominating_asserts)
570 tree cond, cached_lhs;
571 enum gimple_code code = gimple_code (stmt);
573 /* For comparisons, we have to update both operands, then try
574 to simplify the comparison. */
575 if (code == GIMPLE_COND)
577 tree op0, op1;
578 enum tree_code cond_code;
580 op0 = gimple_cond_lhs (stmt);
581 op1 = gimple_cond_rhs (stmt);
582 cond_code = gimple_cond_code (stmt);
584 /* Get the current value of both operands. */
585 if (TREE_CODE (op0) == SSA_NAME)
587 for (int i = 0; i < 2; i++)
589 if (TREE_CODE (op0) == SSA_NAME
590 && SSA_NAME_VALUE (op0))
591 op0 = SSA_NAME_VALUE (op0);
592 else
593 break;
597 if (TREE_CODE (op1) == SSA_NAME)
599 for (int i = 0; i < 2; i++)
601 if (TREE_CODE (op1) == SSA_NAME
602 && SSA_NAME_VALUE (op1))
603 op1 = SSA_NAME_VALUE (op1);
604 else
605 break;
609 if (handle_dominating_asserts)
611 /* Now see if the operand was consumed by an ASSERT_EXPR
612 which dominates E->src. If so, we want to replace the
613 operand with the LHS of the ASSERT_EXPR. */
614 if (TREE_CODE (op0) == SSA_NAME)
615 op0 = lhs_of_dominating_assert (op0, e->src, stmt);
617 if (TREE_CODE (op1) == SSA_NAME)
618 op1 = lhs_of_dominating_assert (op1, e->src, stmt);
621 /* We may need to canonicalize the comparison. For
622 example, op0 might be a constant while op1 is an
623 SSA_NAME. Failure to canonicalize will cause us to
624 miss threading opportunities. */
625 if (tree_swap_operands_p (op0, op1, false))
627 tree tmp;
628 cond_code = swap_tree_comparison (cond_code);
629 tmp = op0;
630 op0 = op1;
631 op1 = tmp;
634 /* Stuff the operator and operands into our dummy conditional
635 expression. */
636 gimple_cond_set_code (dummy_cond, cond_code);
637 gimple_cond_set_lhs (dummy_cond, op0);
638 gimple_cond_set_rhs (dummy_cond, op1);
640 /* We absolutely do not care about any type conversions
641 we only care about a zero/nonzero value. */
642 fold_defer_overflow_warnings ();
644 cached_lhs = fold_binary (cond_code, boolean_type_node, op0, op1);
645 if (cached_lhs)
646 while (CONVERT_EXPR_P (cached_lhs))
647 cached_lhs = TREE_OPERAND (cached_lhs, 0);
649 fold_undefer_overflow_warnings ((cached_lhs
650 && is_gimple_min_invariant (cached_lhs)),
651 stmt, WARN_STRICT_OVERFLOW_CONDITIONAL);
653 /* If we have not simplified the condition down to an invariant,
654 then use the pass specific callback to simplify the condition. */
655 if (!cached_lhs
656 || !is_gimple_min_invariant (cached_lhs))
657 cached_lhs = (*simplify) (dummy_cond, stmt);
659 return cached_lhs;
662 if (code == GIMPLE_SWITCH)
663 cond = gimple_switch_index (as_a <gswitch *> (stmt));
664 else if (code == GIMPLE_GOTO)
665 cond = gimple_goto_dest (stmt);
666 else
667 gcc_unreachable ();
669 /* We can have conditionals which just test the state of a variable
670 rather than use a relational operator. These are simpler to handle. */
671 if (TREE_CODE (cond) == SSA_NAME)
673 tree original_lhs = cond;
674 cached_lhs = cond;
676 /* Get the variable's current value from the equivalence chains.
678 It is possible to get loops in the SSA_NAME_VALUE chains
679 (consider threading the backedge of a loop where we have
680 a loop invariant SSA_NAME used in the condition. */
681 if (cached_lhs)
683 for (int i = 0; i < 2; i++)
685 if (TREE_CODE (cached_lhs) == SSA_NAME
686 && SSA_NAME_VALUE (cached_lhs))
687 cached_lhs = SSA_NAME_VALUE (cached_lhs);
688 else
689 break;
693 /* If we're dominated by a suitable ASSERT_EXPR, then
694 update CACHED_LHS appropriately. */
695 if (handle_dominating_asserts && TREE_CODE (cached_lhs) == SSA_NAME)
696 cached_lhs = lhs_of_dominating_assert (cached_lhs, e->src, stmt);
698 /* If we haven't simplified to an invariant yet, then use the
699 pass specific callback to try and simplify it further. */
700 if (cached_lhs && ! is_gimple_min_invariant (cached_lhs))
701 cached_lhs = (*simplify) (stmt, stmt);
703 /* We couldn't find an invariant. But, callers of this
704 function may be able to do something useful with the
705 unmodified destination. */
706 if (!cached_lhs)
707 cached_lhs = original_lhs;
709 else
710 cached_lhs = NULL;
712 return cached_lhs;
715 /* Copy debug stmts from DEST's chain of single predecessors up to
716 SRC, so that we don't lose the bindings as PHI nodes are introduced
717 when DEST gains new predecessors. */
718 void
719 propagate_threaded_block_debug_into (basic_block dest, basic_block src)
721 if (!MAY_HAVE_DEBUG_STMTS)
722 return;
724 if (!single_pred_p (dest))
725 return;
727 gcc_checking_assert (dest != src);
729 gimple_stmt_iterator gsi = gsi_after_labels (dest);
730 int i = 0;
731 const int alloc_count = 16; // ?? Should this be a PARAM?
733 /* Estimate the number of debug vars overridden in the beginning of
734 DEST, to tell how many we're going to need to begin with. */
735 for (gimple_stmt_iterator si = gsi;
736 i * 4 <= alloc_count * 3 && !gsi_end_p (si); gsi_next (&si))
738 gimple stmt = gsi_stmt (si);
739 if (!is_gimple_debug (stmt))
740 break;
741 i++;
744 auto_vec<tree, alloc_count> fewvars;
745 hash_set<tree> *vars = NULL;
747 /* If we're already starting with 3/4 of alloc_count, go for a
748 hash_set, otherwise start with an unordered stack-allocated
749 VEC. */
750 if (i * 4 > alloc_count * 3)
751 vars = new hash_set<tree>;
753 /* Now go through the initial debug stmts in DEST again, this time
754 actually inserting in VARS or FEWVARS. Don't bother checking for
755 duplicates in FEWVARS. */
756 for (gimple_stmt_iterator si = gsi; !gsi_end_p (si); gsi_next (&si))
758 gimple stmt = gsi_stmt (si);
759 if (!is_gimple_debug (stmt))
760 break;
762 tree var;
764 if (gimple_debug_bind_p (stmt))
765 var = gimple_debug_bind_get_var (stmt);
766 else if (gimple_debug_source_bind_p (stmt))
767 var = gimple_debug_source_bind_get_var (stmt);
768 else
769 gcc_unreachable ();
771 if (vars)
772 vars->add (var);
773 else
774 fewvars.quick_push (var);
777 basic_block bb = dest;
781 bb = single_pred (bb);
782 for (gimple_stmt_iterator si = gsi_last_bb (bb);
783 !gsi_end_p (si); gsi_prev (&si))
785 gimple stmt = gsi_stmt (si);
786 if (!is_gimple_debug (stmt))
787 continue;
789 tree var;
791 if (gimple_debug_bind_p (stmt))
792 var = gimple_debug_bind_get_var (stmt);
793 else if (gimple_debug_source_bind_p (stmt))
794 var = gimple_debug_source_bind_get_var (stmt);
795 else
796 gcc_unreachable ();
798 /* Discard debug bind overlaps. ??? Unlike stmts from src,
799 copied into a new block that will precede BB, debug bind
800 stmts in bypassed BBs may actually be discarded if
801 they're overwritten by subsequent debug bind stmts, which
802 might be a problem once we introduce stmt frontier notes
803 or somesuch. Adding `&& bb == src' to the condition
804 below will preserve all potentially relevant debug
805 notes. */
806 if (vars && vars->add (var))
807 continue;
808 else if (!vars)
810 int i = fewvars.length ();
811 while (i--)
812 if (fewvars[i] == var)
813 break;
814 if (i >= 0)
815 continue;
817 if (fewvars.length () < (unsigned) alloc_count)
818 fewvars.quick_push (var);
819 else
821 vars = new hash_set<tree>;
822 for (i = 0; i < alloc_count; i++)
823 vars->add (fewvars[i]);
824 fewvars.release ();
825 vars->add (var);
829 stmt = gimple_copy (stmt);
830 /* ??? Should we drop the location of the copy to denote
831 they're artificial bindings? */
832 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
835 while (bb != src && single_pred_p (bb));
837 if (vars)
838 delete vars;
839 else if (fewvars.exists ())
840 fewvars.release ();
843 /* See if TAKEN_EDGE->dest is a threadable block with no side effecs (ie, it
844 need not be duplicated as part of the CFG/SSA updating process).
846 If it is threadable, add it to PATH and VISITED and recurse, ultimately
847 returning TRUE from the toplevel call. Otherwise do nothing and
848 return false.
850 DUMMY_COND, HANDLE_DOMINATING_ASSERTS and SIMPLIFY are used to
851 try and simplify the condition at the end of TAKEN_EDGE->dest. */
852 static bool
853 thread_around_empty_blocks (edge taken_edge,
854 gcond *dummy_cond,
855 bool handle_dominating_asserts,
856 tree (*simplify) (gimple, gimple),
857 bitmap visited,
858 vec<jump_thread_edge *> *path,
859 bool *backedge_seen_p)
861 basic_block bb = taken_edge->dest;
862 gimple_stmt_iterator gsi;
863 gimple stmt;
864 tree cond;
866 /* The key property of these blocks is that they need not be duplicated
867 when threading. Thus they can not have visible side effects such
868 as PHI nodes. */
869 if (!gsi_end_p (gsi_start_phis (bb)))
870 return false;
872 /* Skip over DEBUG statements at the start of the block. */
873 gsi = gsi_start_nondebug_bb (bb);
875 /* If the block has no statements, but does have a single successor, then
876 it's just a forwarding block and we can thread through it trivially.
878 However, note that just threading through empty blocks with single
879 successors is not inherently profitable. For the jump thread to
880 be profitable, we must avoid a runtime conditional.
882 By taking the return value from the recursive call, we get the
883 desired effect of returning TRUE when we found a profitable jump
884 threading opportunity and FALSE otherwise.
886 This is particularly important when this routine is called after
887 processing a joiner block. Returning TRUE too aggressively in
888 that case results in pointless duplication of the joiner block. */
889 if (gsi_end_p (gsi))
891 if (single_succ_p (bb))
893 taken_edge = single_succ_edge (bb);
894 if (!bitmap_bit_p (visited, taken_edge->dest->index))
896 jump_thread_edge *x
897 = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
898 path->safe_push (x);
899 bitmap_set_bit (visited, taken_edge->dest->index);
900 *backedge_seen_p |= ((taken_edge->flags & EDGE_DFS_BACK) != 0);
901 if (*backedge_seen_p)
902 simplify = dummy_simplify;
903 return thread_around_empty_blocks (taken_edge,
904 dummy_cond,
905 handle_dominating_asserts,
906 simplify,
907 visited,
908 path,
909 backedge_seen_p);
913 /* We have a block with no statements, but multiple successors? */
914 return false;
917 /* The only real statements this block can have are a control
918 flow altering statement. Anything else stops the thread. */
919 stmt = gsi_stmt (gsi);
920 if (gimple_code (stmt) != GIMPLE_COND
921 && gimple_code (stmt) != GIMPLE_GOTO
922 && gimple_code (stmt) != GIMPLE_SWITCH)
923 return false;
925 /* If we have traversed a backedge, then we do not want to look
926 at certain expressions in the table that can not be relied upon.
927 Luckily the only code that looked at those expressions is the
928 SIMPLIFY callback, which we replace if we can no longer use it. */
929 if (*backedge_seen_p)
930 simplify = dummy_simplify;
932 /* Extract and simplify the condition. */
933 cond = simplify_control_stmt_condition (taken_edge, stmt, dummy_cond,
934 simplify, handle_dominating_asserts);
936 /* If the condition can be statically computed and we have not already
937 visited the destination edge, then add the taken edge to our thread
938 path. */
939 if (cond && is_gimple_min_invariant (cond))
941 taken_edge = find_taken_edge (bb, cond);
943 if (bitmap_bit_p (visited, taken_edge->dest->index))
944 return false;
945 bitmap_set_bit (visited, taken_edge->dest->index);
947 jump_thread_edge *x
948 = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
949 path->safe_push (x);
950 *backedge_seen_p |= ((taken_edge->flags & EDGE_DFS_BACK) != 0);
951 if (*backedge_seen_p)
952 simplify = dummy_simplify;
954 thread_around_empty_blocks (taken_edge,
955 dummy_cond,
956 handle_dominating_asserts,
957 simplify,
958 visited,
959 path,
960 backedge_seen_p);
961 return true;
964 return false;
967 /* Return true if the CFG contains at least one path from START_BB to END_BB.
968 When a path is found, record in PATH the blocks from END_BB to START_BB.
969 VISITED_BBS is used to make sure we don't fall into an infinite loop. Bound
970 the recursion to basic blocks belonging to LOOP. */
972 static bool
973 fsm_find_thread_path (basic_block start_bb, basic_block end_bb,
974 vec<basic_block, va_gc> *&path,
975 hash_set<basic_block> *visited_bbs, loop_p loop)
977 if (loop != start_bb->loop_father)
978 return false;
980 if (start_bb == end_bb)
982 vec_safe_push (path, start_bb);
983 return true;
986 if (!visited_bbs->add (start_bb))
988 edge e;
989 edge_iterator ei;
990 FOR_EACH_EDGE (e, ei, start_bb->succs)
991 if (fsm_find_thread_path (e->dest, end_bb, path, visited_bbs, loop))
993 vec_safe_push (path, start_bb);
994 return true;
998 return false;
1001 static int max_threaded_paths;
1003 /* We trace the value of the variable EXPR back through any phi nodes looking
1004 for places where it gets a constant value and save the path. Stop after
1005 having recorded MAX_PATHS jump threading paths. */
1007 static void
1008 fsm_find_control_statement_thread_paths (tree expr,
1009 hash_set<gimple> *visited_phis,
1010 vec<basic_block, va_gc> *&path,
1011 bool seen_loop_phi)
1013 tree var = SSA_NAME_VAR (expr);
1014 gimple def_stmt = SSA_NAME_DEF_STMT (expr);
1015 basic_block var_bb = gimple_bb (def_stmt);
1017 if (var == NULL || var_bb == NULL)
1018 return;
1020 /* For the moment we assume that an SSA chain only contains phi nodes, and
1021 eventually one of the phi arguments will be an integer constant. In the
1022 future, this could be extended to also handle simple assignments of
1023 arithmetic operations. */
1024 if (gimple_code (def_stmt) != GIMPLE_PHI)
1025 return;
1027 /* Avoid infinite recursion. */
1028 if (visited_phis->add (def_stmt))
1029 return;
1031 gphi *phi = as_a <gphi *> (def_stmt);
1032 int next_path_length = 0;
1033 basic_block last_bb_in_path = path->last ();
1035 if (loop_containing_stmt (phi)->header == gimple_bb (phi))
1037 /* Do not walk through more than one loop PHI node. */
1038 if (seen_loop_phi)
1039 return;
1040 seen_loop_phi = true;
1043 /* Following the chain of SSA_NAME definitions, we jumped from a definition in
1044 LAST_BB_IN_PATH to a definition in VAR_BB. When these basic blocks are
1045 different, append to PATH the blocks from LAST_BB_IN_PATH to VAR_BB. */
1046 if (var_bb != last_bb_in_path)
1048 edge e;
1049 int e_count = 0;
1050 edge_iterator ei;
1051 vec<basic_block, va_gc> *next_path;
1052 vec_alloc (next_path, n_basic_blocks_for_fn (cfun));
1054 FOR_EACH_EDGE (e, ei, last_bb_in_path->preds)
1056 hash_set<basic_block> *visited_bbs = new hash_set<basic_block>;
1058 if (fsm_find_thread_path (var_bb, e->src, next_path, visited_bbs,
1059 e->src->loop_father))
1060 ++e_count;
1062 delete visited_bbs;
1064 /* If there is more than one path, stop. */
1065 if (e_count > 1)
1067 vec_free (next_path);
1068 return;
1072 /* Stop if we have not found a path: this could occur when the recursion
1073 is stopped by one of the bounds. */
1074 if (e_count == 0)
1076 vec_free (next_path);
1077 return;
1080 /* Append all the nodes from NEXT_PATH to PATH. */
1081 vec_safe_splice (path, next_path);
1082 next_path_length = next_path->length ();
1083 vec_free (next_path);
1086 gcc_assert (path->last () == var_bb);
1088 /* Iterate over the arguments of PHI. */
1089 unsigned int i;
1090 for (i = 0; i < gimple_phi_num_args (phi); i++)
1092 tree arg = gimple_phi_arg_def (phi, i);
1093 basic_block bbi = gimple_phi_arg_edge (phi, i)->src;
1095 /* Skip edges pointing outside the current loop. */
1096 if (!arg || var_bb->loop_father != bbi->loop_father)
1097 continue;
1099 if (TREE_CODE (arg) == SSA_NAME)
1101 vec_safe_push (path, bbi);
1102 /* Recursively follow SSA_NAMEs looking for a constant definition. */
1103 fsm_find_control_statement_thread_paths (arg, visited_phis, path,
1104 seen_loop_phi);
1106 path->pop ();
1107 continue;
1110 if (TREE_CODE (arg) != INTEGER_CST)
1111 continue;
1113 int path_length = path->length ();
1114 /* A path with less than 2 basic blocks should not be jump-threaded. */
1115 if (path_length < 2)
1116 continue;
1118 if (path_length > PARAM_VALUE (PARAM_MAX_FSM_THREAD_LENGTH))
1120 if (dump_file && (dump_flags & TDF_DETAILS))
1121 fprintf (dump_file, "FSM jump-thread path not considered: "
1122 "the number of basic blocks on the path "
1123 "exceeds PARAM_MAX_FSM_THREAD_LENGTH.\n");
1124 continue;
1127 if (max_threaded_paths <= 0)
1129 if (dump_file && (dump_flags & TDF_DETAILS))
1130 fprintf (dump_file, "FSM jump-thread path not considered: "
1131 "the number of previously recorded FSM paths to thread "
1132 "exceeds PARAM_MAX_FSM_THREAD_PATHS.\n");
1133 continue;
1136 /* Add BBI to the path. */
1137 vec_safe_push (path, bbi);
1138 ++path_length;
1140 int n_insns = 0;
1141 gimple_stmt_iterator gsi;
1142 int j;
1143 loop_p loop = (*path)[0]->loop_father;
1144 bool path_crosses_loops = false;
1146 /* Count the number of instructions on the path: as these instructions
1147 will have to be duplicated, we will not record the path if there are
1148 too many instructions on the path. Also check that all the blocks in
1149 the path belong to a single loop. */
1150 for (j = 1; j < path_length - 1; j++)
1152 basic_block bb = (*path)[j];
1154 if (bb->loop_father != loop)
1156 path_crosses_loops = true;
1157 break;
1160 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1162 gimple stmt = gsi_stmt (gsi);
1163 /* Do not count empty statements and labels. */
1164 if (gimple_code (stmt) != GIMPLE_NOP
1165 && gimple_code (stmt) != GIMPLE_LABEL
1166 && !is_gimple_debug (stmt))
1167 ++n_insns;
1171 if (path_crosses_loops)
1173 if (dump_file && (dump_flags & TDF_DETAILS))
1174 fprintf (dump_file, "FSM jump-thread path not considered: "
1175 "the path crosses loops.\n");
1176 path->pop ();
1177 continue;
1180 if (n_insns >= PARAM_VALUE (PARAM_MAX_FSM_THREAD_PATH_INSNS))
1182 if (dump_file && (dump_flags & TDF_DETAILS))
1183 fprintf (dump_file, "FSM jump-thread path not considered: "
1184 "the number of instructions on the path "
1185 "exceeds PARAM_MAX_FSM_THREAD_PATH_INSNS.\n");
1186 path->pop ();
1187 continue;
1190 vec<jump_thread_edge *> *jump_thread_path
1191 = new vec<jump_thread_edge *> ();
1193 /* Record the edges between the blocks in PATH. */
1194 for (j = 0; j < path_length - 1; j++)
1196 edge e = find_edge ((*path)[path_length - j - 1],
1197 (*path)[path_length - j - 2]);
1198 gcc_assert (e);
1199 jump_thread_edge *x = new jump_thread_edge (e, EDGE_FSM_THREAD);
1200 jump_thread_path->safe_push (x);
1203 /* Add the edge taken when the control variable has value ARG. */
1204 edge taken_edge = find_taken_edge ((*path)[0], arg);
1205 jump_thread_edge *x
1206 = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
1207 jump_thread_path->safe_push (x);
1209 register_jump_thread (jump_thread_path);
1210 --max_threaded_paths;
1212 /* Remove BBI from the path. */
1213 path->pop ();
1216 /* Remove all the nodes that we added from NEXT_PATH. */
1217 if (next_path_length)
1218 vec_safe_truncate (path, (path->length () - next_path_length));
1221 /* We are exiting E->src, see if E->dest ends with a conditional
1222 jump which has a known value when reached via E.
1224 E->dest can have arbitrary side effects which, if threading is
1225 successful, will be maintained.
1227 Special care is necessary if E is a back edge in the CFG as we
1228 may have already recorded equivalences for E->dest into our
1229 various tables, including the result of the conditional at
1230 the end of E->dest. Threading opportunities are severely
1231 limited in that case to avoid short-circuiting the loop
1232 incorrectly.
1234 DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
1235 to avoid allocating memory.
1237 HANDLE_DOMINATING_ASSERTS is true if we should try to replace operands of
1238 the simplified condition with left-hand sides of ASSERT_EXPRs they are
1239 used in.
1241 STACK is used to undo temporary equivalences created during the walk of
1242 E->dest.
1244 SIMPLIFY is a pass-specific function used to simplify statements.
1246 Our caller is responsible for restoring the state of the expression
1247 and const_and_copies stacks.
1249 Positive return value is success. Zero return value is failure, but
1250 the block can still be duplicated as a joiner in a jump thread path,
1251 negative indicates the block should not be duplicated and thus is not
1252 suitable for a joiner in a jump threading path. */
1254 static int
1255 thread_through_normal_block (edge e,
1256 gcond *dummy_cond,
1257 bool handle_dominating_asserts,
1258 vec<tree> *stack,
1259 tree (*simplify) (gimple, gimple),
1260 vec<jump_thread_edge *> *path,
1261 bitmap visited,
1262 bool *backedge_seen_p)
1264 /* If we have traversed a backedge, then we do not want to look
1265 at certain expressions in the table that can not be relied upon.
1266 Luckily the only code that looked at those expressions is the
1267 SIMPLIFY callback, which we replace if we can no longer use it. */
1268 if (*backedge_seen_p)
1269 simplify = dummy_simplify;
1271 /* PHIs create temporary equivalences.
1272 Note that if we found a PHI that made the block non-threadable, then
1273 we need to bubble that up to our caller in the same manner we do
1274 when we prematurely stop processing statements below. */
1275 if (!record_temporary_equivalences_from_phis (e, stack))
1276 return -1;
1278 /* Now walk each statement recording any context sensitive
1279 temporary equivalences we can detect. */
1280 gimple stmt
1281 = record_temporary_equivalences_from_stmts_at_dest (e, stack, simplify,
1282 *backedge_seen_p);
1284 /* If we didn't look at all the statements, the most likely reason is
1285 there were too many and thus duplicating this block is not profitable.
1287 Also note if we do not look at all the statements, then we may not
1288 have invalidated equivalences that are no longer valid if we threaded
1289 around a loop. Thus we must signal to our caller that this block
1290 is not suitable for use as a joiner in a threading path. */
1291 if (!stmt)
1292 return -1;
1294 /* If we stopped at a COND_EXPR or SWITCH_EXPR, see if we know which arm
1295 will be taken. */
1296 if (gimple_code (stmt) == GIMPLE_COND
1297 || gimple_code (stmt) == GIMPLE_GOTO
1298 || gimple_code (stmt) == GIMPLE_SWITCH)
1300 tree cond;
1302 /* Extract and simplify the condition. */
1303 cond = simplify_control_stmt_condition (e, stmt, dummy_cond, simplify,
1304 handle_dominating_asserts);
1306 if (!cond)
1307 return 0;
1309 if (is_gimple_min_invariant (cond))
1311 edge taken_edge = find_taken_edge (e->dest, cond);
1312 basic_block dest = (taken_edge ? taken_edge->dest : NULL);
1314 /* DEST could be NULL for a computed jump to an absolute
1315 address. */
1316 if (dest == NULL
1317 || dest == e->dest
1318 || bitmap_bit_p (visited, dest->index))
1319 return 0;
1321 /* Only push the EDGE_START_JUMP_THREAD marker if this is
1322 first edge on the path. */
1323 if (path->length () == 0)
1325 jump_thread_edge *x
1326 = new jump_thread_edge (e, EDGE_START_JUMP_THREAD);
1327 path->safe_push (x);
1328 *backedge_seen_p |= ((e->flags & EDGE_DFS_BACK) != 0);
1331 jump_thread_edge *x
1332 = new jump_thread_edge (taken_edge, EDGE_COPY_SRC_BLOCK);
1333 path->safe_push (x);
1334 *backedge_seen_p |= ((taken_edge->flags & EDGE_DFS_BACK) != 0);
1335 if (*backedge_seen_p)
1336 simplify = dummy_simplify;
1338 /* See if we can thread through DEST as well, this helps capture
1339 secondary effects of threading without having to re-run DOM or
1340 VRP.
1342 We don't want to thread back to a block we have already
1343 visited. This may be overly conservative. */
1344 bitmap_set_bit (visited, dest->index);
1345 bitmap_set_bit (visited, e->dest->index);
1346 thread_around_empty_blocks (taken_edge,
1347 dummy_cond,
1348 handle_dominating_asserts,
1349 simplify,
1350 visited,
1351 path,
1352 backedge_seen_p);
1353 return 1;
1356 if (!flag_expensive_optimizations
1357 || optimize_function_for_size_p (cfun)
1358 || TREE_CODE (cond) != SSA_NAME
1359 || e->dest->loop_father != e->src->loop_father
1360 || loop_depth (e->dest->loop_father) == 0)
1361 return 0;
1363 /* When COND cannot be simplified, try to find paths from a control
1364 statement back through the PHI nodes which would affect that control
1365 statement. */
1366 vec<basic_block, va_gc> *bb_path;
1367 vec_alloc (bb_path, n_basic_blocks_for_fn (cfun));
1368 vec_safe_push (bb_path, e->dest);
1369 hash_set<gimple> *visited_phis = new hash_set<gimple>;
1371 max_threaded_paths = PARAM_VALUE (PARAM_MAX_FSM_THREAD_PATHS);
1372 fsm_find_control_statement_thread_paths (cond, visited_phis, bb_path,
1373 false);
1375 delete visited_phis;
1376 vec_free (bb_path);
1378 return 0;
1381 /* We are exiting E->src, see if E->dest ends with a conditional
1382 jump which has a known value when reached via E.
1384 Special care is necessary if E is a back edge in the CFG as we
1385 may have already recorded equivalences for E->dest into our
1386 various tables, including the result of the conditional at
1387 the end of E->dest. Threading opportunities are severely
1388 limited in that case to avoid short-circuiting the loop
1389 incorrectly.
1391 Note it is quite common for the first block inside a loop to
1392 end with a conditional which is either always true or always
1393 false when reached via the loop backedge. Thus we do not want
1394 to blindly disable threading across a loop backedge.
1396 DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
1397 to avoid allocating memory.
1399 HANDLE_DOMINATING_ASSERTS is true if we should try to replace operands of
1400 the simplified condition with left-hand sides of ASSERT_EXPRs they are
1401 used in.
1403 STACK is used to undo temporary equivalences created during the walk of
1404 E->dest.
1406 SIMPLIFY is a pass-specific function used to simplify statements. */
1408 void
1409 thread_across_edge (gcond *dummy_cond,
1410 edge e,
1411 bool handle_dominating_asserts,
1412 vec<tree> *stack,
1413 tree (*simplify) (gimple, gimple))
1415 bitmap visited = BITMAP_ALLOC (NULL);
1416 bool backedge_seen;
1418 stmt_count = 0;
1420 vec<jump_thread_edge *> *path = new vec<jump_thread_edge *> ();
1421 bitmap_clear (visited);
1422 bitmap_set_bit (visited, e->src->index);
1423 bitmap_set_bit (visited, e->dest->index);
1424 backedge_seen = ((e->flags & EDGE_DFS_BACK) != 0);
1425 if (backedge_seen)
1426 simplify = dummy_simplify;
1428 int threaded = thread_through_normal_block (e, dummy_cond,
1429 handle_dominating_asserts,
1430 stack, simplify, path,
1431 visited, &backedge_seen);
1432 if (threaded > 0)
1434 propagate_threaded_block_debug_into (path->last ()->e->dest,
1435 e->dest);
1436 remove_temporary_equivalences (stack);
1437 BITMAP_FREE (visited);
1438 register_jump_thread (path);
1439 return;
1441 else
1443 /* Negative and zero return values indicate no threading was possible,
1444 thus there should be no edges on the thread path and no need to walk
1445 through the vector entries. */
1446 gcc_assert (path->length () == 0);
1447 path->release ();
1448 delete path;
1450 /* A negative status indicates the target block was deemed too big to
1451 duplicate. Just quit now rather than trying to use the block as
1452 a joiner in a jump threading path.
1454 This prevents unnecessary code growth, but more importantly if we
1455 do not look at all the statements in the block, then we may have
1456 missed some invalidations if we had traversed a backedge! */
1457 if (threaded < 0)
1459 BITMAP_FREE (visited);
1460 remove_temporary_equivalences (stack);
1461 return;
1465 /* We were unable to determine what out edge from E->dest is taken. However,
1466 we might still be able to thread through successors of E->dest. This
1467 often occurs when E->dest is a joiner block which then fans back out
1468 based on redundant tests.
1470 If so, we'll copy E->dest and redirect the appropriate predecessor to
1471 the copy. Within the copy of E->dest, we'll thread one or more edges
1472 to points deeper in the CFG.
1474 This is a stopgap until we have a more structured approach to path
1475 isolation. */
1477 edge taken_edge;
1478 edge_iterator ei;
1479 bool found;
1481 /* If E->dest has abnormal outgoing edges, then there's no guarantee
1482 we can safely redirect any of the edges. Just punt those cases. */
1483 FOR_EACH_EDGE (taken_edge, ei, e->dest->succs)
1484 if (taken_edge->flags & EDGE_ABNORMAL)
1486 remove_temporary_equivalences (stack);
1487 BITMAP_FREE (visited);
1488 return;
1491 /* Look at each successor of E->dest to see if we can thread through it. */
1492 FOR_EACH_EDGE (taken_edge, ei, e->dest->succs)
1494 /* Push a fresh marker so we can unwind the equivalences created
1495 for each of E->dest's successors. */
1496 stack->safe_push (NULL_TREE);
1498 /* Avoid threading to any block we have already visited. */
1499 bitmap_clear (visited);
1500 bitmap_set_bit (visited, e->src->index);
1501 bitmap_set_bit (visited, e->dest->index);
1502 bitmap_set_bit (visited, taken_edge->dest->index);
1503 vec<jump_thread_edge *> *path = new vec<jump_thread_edge *> ();
1505 /* Record whether or not we were able to thread through a successor
1506 of E->dest. */
1507 jump_thread_edge *x = new jump_thread_edge (e, EDGE_START_JUMP_THREAD);
1508 path->safe_push (x);
1510 x = new jump_thread_edge (taken_edge, EDGE_COPY_SRC_JOINER_BLOCK);
1511 path->safe_push (x);
1512 found = false;
1513 backedge_seen = ((e->flags & EDGE_DFS_BACK) != 0);
1514 backedge_seen |= ((taken_edge->flags & EDGE_DFS_BACK) != 0);
1515 if (backedge_seen)
1516 simplify = dummy_simplify;
1517 found = thread_around_empty_blocks (taken_edge,
1518 dummy_cond,
1519 handle_dominating_asserts,
1520 simplify,
1521 visited,
1522 path,
1523 &backedge_seen);
1525 if (backedge_seen)
1526 simplify = dummy_simplify;
1528 if (!found)
1529 found = thread_through_normal_block (path->last ()->e, dummy_cond,
1530 handle_dominating_asserts,
1531 stack, simplify, path, visited,
1532 &backedge_seen) > 0;
1534 /* If we were able to thread through a successor of E->dest, then
1535 record the jump threading opportunity. */
1536 if (found)
1538 propagate_threaded_block_debug_into (path->last ()->e->dest,
1539 taken_edge->dest);
1540 register_jump_thread (path);
1542 else
1544 delete_jump_thread_path (path);
1547 /* And unwind the equivalence table. */
1548 remove_temporary_equivalences (stack);
1550 BITMAP_FREE (visited);
1553 remove_temporary_equivalences (stack);