svn merge -r 217500:218679 svn+ssh://gcc.gnu.org/svn/gcc/trunk
[official-gcc.git] / gcc / tree-ssa-threadedge.c
blobce7031112a4c04f54f9153c925b7ed9986942416
1 /* SSA Jump Threading
2 Copyright (C) 2005-2014 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 "tree.h"
26 #include "flags.h"
27 #include "tm_p.h"
28 #include "predict.h"
29 #include "vec.h"
30 #include "hashtab.h"
31 #include "hash-set.h"
32 #include "machmode.h"
33 #include "hard-reg-set.h"
34 #include "input.h"
35 #include "function.h"
36 #include "dominance.h"
37 #include "basic-block.h"
38 #include "cfgloop.h"
39 #include "timevar.h"
40 #include "dumpfile.h"
41 #include "tree-ssa-alias.h"
42 #include "internal-fn.h"
43 #include "gimple-expr.h"
44 #include "is-a.h"
45 #include "gimple.h"
46 #include "gimple-iterator.h"
47 #include "gimple-ssa.h"
48 #include "tree-cfg.h"
49 #include "tree-phinodes.h"
50 #include "ssa-iterators.h"
51 #include "stringpool.h"
52 #include "tree-ssanames.h"
53 #include "tree-ssa-propagate.h"
54 #include "tree-ssa-threadupdate.h"
55 #include "langhooks.h"
56 #include "params.h"
57 #include "tree-ssa-threadedge.h"
58 #include "builtins.h"
59 #include "cfg.h"
60 #include "cfganal.h"
62 /* To avoid code explosion due to jump threading, we limit the
63 number of statements we are going to copy. This variable
64 holds the number of statements currently seen that we'll have
65 to copy as part of the jump threading process. */
66 static int stmt_count;
68 /* Array to record value-handles per SSA_NAME. */
69 vec<tree> ssa_name_values;
71 /* Set the value for the SSA name NAME to VALUE. */
73 void
74 set_ssa_name_value (tree name, tree value)
76 if (SSA_NAME_VERSION (name) >= ssa_name_values.length ())
77 ssa_name_values.safe_grow_cleared (SSA_NAME_VERSION (name) + 1);
78 if (value && TREE_OVERFLOW_P (value))
79 value = drop_tree_overflow (value);
80 ssa_name_values[SSA_NAME_VERSION (name)] = value;
83 /* Initialize the per SSA_NAME value-handles array. Returns it. */
84 void
85 threadedge_initialize_values (void)
87 gcc_assert (!ssa_name_values.exists ());
88 ssa_name_values.create (num_ssa_names);
91 /* Free the per SSA_NAME value-handle array. */
92 void
93 threadedge_finalize_values (void)
95 ssa_name_values.release ();
98 /* Return TRUE if we may be able to thread an incoming edge into
99 BB to an outgoing edge from BB. Return FALSE otherwise. */
101 bool
102 potentially_threadable_block (basic_block bb)
104 gimple_stmt_iterator gsi;
106 /* If BB has a single successor or a single predecessor, then
107 there is no threading opportunity. */
108 if (single_succ_p (bb) || single_pred_p (bb))
109 return false;
111 /* If BB does not end with a conditional, switch or computed goto,
112 then there is no threading opportunity. */
113 gsi = gsi_last_bb (bb);
114 if (gsi_end_p (gsi)
115 || ! gsi_stmt (gsi)
116 || (gimple_code (gsi_stmt (gsi)) != GIMPLE_COND
117 && gimple_code (gsi_stmt (gsi)) != GIMPLE_GOTO
118 && gimple_code (gsi_stmt (gsi)) != GIMPLE_SWITCH))
119 return false;
121 return true;
124 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
125 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
126 BB. If no such ASSERT_EXPR is found, return OP. */
128 static tree
129 lhs_of_dominating_assert (tree op, basic_block bb, gimple stmt)
131 imm_use_iterator imm_iter;
132 gimple use_stmt;
133 use_operand_p use_p;
135 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
137 use_stmt = USE_STMT (use_p);
138 if (use_stmt != stmt
139 && gimple_assign_single_p (use_stmt)
140 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ASSERT_EXPR
141 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) == op
142 && dominated_by_p (CDI_DOMINATORS, bb, gimple_bb (use_stmt)))
144 return gimple_assign_lhs (use_stmt);
147 return op;
150 /* We record temporary equivalences created by PHI nodes or
151 statements within the target block. Doing so allows us to
152 identify more jump threading opportunities, even in blocks
153 with side effects.
155 We keep track of those temporary equivalences in a stack
156 structure so that we can unwind them when we're done processing
157 a particular edge. This routine handles unwinding the data
158 structures. */
160 static void
161 remove_temporary_equivalences (vec<tree> *stack)
163 while (stack->length () > 0)
165 tree prev_value, dest;
167 dest = stack->pop ();
169 /* A NULL value indicates we should stop unwinding, otherwise
170 pop off the next entry as they're recorded in pairs. */
171 if (dest == NULL)
172 break;
174 prev_value = stack->pop ();
175 set_ssa_name_value (dest, prev_value);
179 /* Record a temporary equivalence, saving enough information so that
180 we can restore the state of recorded equivalences when we're
181 done processing the current edge. */
183 static void
184 record_temporary_equivalence (tree x, tree y, vec<tree> *stack)
186 tree prev_x = SSA_NAME_VALUE (x);
188 /* Y may be NULL if we are invalidating entries in the table. */
189 if (y && TREE_CODE (y) == SSA_NAME)
191 tree tmp = SSA_NAME_VALUE (y);
192 y = tmp ? tmp : y;
195 set_ssa_name_value (x, y);
196 stack->reserve (2);
197 stack->quick_push (prev_x);
198 stack->quick_push (x);
201 /* Record temporary equivalences created by PHIs at the target of the
202 edge E. Record unwind information for the equivalences onto STACK.
204 If a PHI which prevents threading is encountered, then return FALSE
205 indicating we should not thread this edge, else return TRUE.
207 If SRC_MAP/DST_MAP exist, then mark the source and destination SSA_NAMEs
208 of any equivalences recorded. We use this to make invalidation after
209 traversing back edges less painful. */
211 static bool
212 record_temporary_equivalences_from_phis (edge e, vec<tree> *stack)
214 gphi_iterator gsi;
216 /* Each PHI creates a temporary equivalence, record them.
217 These are context sensitive equivalences and will be removed
218 later. */
219 for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
221 gphi *phi = gsi.phi ();
222 tree src = PHI_ARG_DEF_FROM_EDGE (phi, e);
223 tree dst = gimple_phi_result (phi);
225 /* If the desired argument is not the same as this PHI's result
226 and it is set by a PHI in E->dest, then we can not thread
227 through E->dest. */
228 if (src != dst
229 && TREE_CODE (src) == SSA_NAME
230 && gimple_code (SSA_NAME_DEF_STMT (src)) == GIMPLE_PHI
231 && gimple_bb (SSA_NAME_DEF_STMT (src)) == e->dest)
232 return false;
234 /* We consider any non-virtual PHI as a statement since it
235 count result in a constant assignment or copy operation. */
236 if (!virtual_operand_p (dst))
237 stmt_count++;
239 record_temporary_equivalence (dst, src, stack);
241 return true;
244 /* Fold the RHS of an assignment statement and return it as a tree.
245 May return NULL_TREE if no simplification is possible. */
247 static tree
248 fold_assignment_stmt (gimple stmt)
250 enum tree_code subcode = gimple_assign_rhs_code (stmt);
252 switch (get_gimple_rhs_class (subcode))
254 case GIMPLE_SINGLE_RHS:
255 return fold (gimple_assign_rhs1 (stmt));
257 case GIMPLE_UNARY_RHS:
259 tree lhs = gimple_assign_lhs (stmt);
260 tree op0 = gimple_assign_rhs1 (stmt);
261 return fold_unary (subcode, TREE_TYPE (lhs), op0);
264 case GIMPLE_BINARY_RHS:
266 tree lhs = gimple_assign_lhs (stmt);
267 tree op0 = gimple_assign_rhs1 (stmt);
268 tree op1 = gimple_assign_rhs2 (stmt);
269 return fold_binary (subcode, TREE_TYPE (lhs), op0, op1);
272 case GIMPLE_TERNARY_RHS:
274 tree lhs = gimple_assign_lhs (stmt);
275 tree op0 = gimple_assign_rhs1 (stmt);
276 tree op1 = gimple_assign_rhs2 (stmt);
277 tree op2 = gimple_assign_rhs3 (stmt);
279 /* Sadly, we have to handle conditional assignments specially
280 here, because fold expects all the operands of an expression
281 to be folded before the expression itself is folded, but we
282 can't just substitute the folded condition here. */
283 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
284 op0 = fold (op0);
286 return fold_ternary (subcode, TREE_TYPE (lhs), op0, op1, op2);
289 default:
290 gcc_unreachable ();
294 /* A new value has been assigned to LHS. If necessary, invalidate any
295 equivalences that are no longer valid. This includes invaliding
296 LHS and any objects that are currently equivalent to LHS.
298 Finding the objects that are currently marked as equivalent to LHS
299 is a bit tricky. We could walk the ssa names and see if any have
300 SSA_NAME_VALUE that is the same as LHS. That's expensive.
302 However, it's far more efficient to look at the unwinding stack as
303 that will have all context sensitive equivalences which are the only
304 ones that we really have to worry about here. */
305 static void
306 invalidate_equivalences (tree lhs, vec<tree> *stack)
309 /* The stack is an unwinding stack. If the current element is NULL
310 then it's a "stop unwinding" marker. Else the current marker is
311 the SSA_NAME with an equivalence and the prior entry in the stack
312 is what the current element is equivalent to. */
313 for (int i = stack->length() - 1; i >= 0; i--)
315 /* Ignore the stop unwinding markers. */
316 if ((*stack)[i] == NULL)
317 continue;
319 /* We want to check the current value of stack[i] to see if
320 it matches LHS. If so, then invalidate. */
321 if (SSA_NAME_VALUE ((*stack)[i]) == lhs)
322 record_temporary_equivalence ((*stack)[i], NULL_TREE, stack);
324 /* Remember, we're dealing with two elements in this case. */
325 i--;
328 /* And invalidate any known value for LHS itself. */
329 if (SSA_NAME_VALUE (lhs))
330 record_temporary_equivalence (lhs, NULL_TREE, stack);
333 /* Try to simplify each statement in E->dest, ultimately leading to
334 a simplification of the COND_EXPR at the end of E->dest.
336 Record unwind information for temporary equivalences onto STACK.
338 Use SIMPLIFY (a pointer to a callback function) to further simplify
339 statements using pass specific information.
341 We might consider marking just those statements which ultimately
342 feed the COND_EXPR. It's not clear if the overhead of bookkeeping
343 would be recovered by trying to simplify fewer statements.
345 If we are able to simplify a statement into the form
346 SSA_NAME = (SSA_NAME | gimple invariant), then we can record
347 a context sensitive equivalence which may help us simplify
348 later statements in E->dest. */
350 static gimple
351 record_temporary_equivalences_from_stmts_at_dest (edge e,
352 vec<tree> *stack,
353 tree (*simplify) (gimple,
354 gimple),
355 bool backedge_seen)
357 gimple stmt = NULL;
358 gimple_stmt_iterator gsi;
359 int max_stmt_count;
361 max_stmt_count = PARAM_VALUE (PARAM_MAX_JUMP_THREAD_DUPLICATION_STMTS);
363 /* Walk through each statement in the block recording equivalences
364 we discover. Note any equivalences we discover are context
365 sensitive (ie, are dependent on traversing E) and must be unwound
366 when we're finished processing E. */
367 for (gsi = gsi_start_bb (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
369 tree cached_lhs = NULL;
371 stmt = gsi_stmt (gsi);
373 /* Ignore empty statements and labels. */
374 if (gimple_code (stmt) == GIMPLE_NOP
375 || gimple_code (stmt) == GIMPLE_LABEL
376 || is_gimple_debug (stmt))
377 continue;
379 /* If the statement has volatile operands, then we assume we
380 can not thread through this block. This is overly
381 conservative in some ways. */
382 if (gimple_code (stmt) == GIMPLE_ASM
383 && gimple_asm_volatile_p (as_a <gasm *> (stmt)))
384 return NULL;
386 /* If duplicating this block is going to cause too much code
387 expansion, then do not thread through this block. */
388 stmt_count++;
389 if (stmt_count > max_stmt_count)
390 return NULL;
392 /* If this is not a statement that sets an SSA_NAME to a new
393 value, then do not try to simplify this statement as it will
394 not simplify in any way that is helpful for jump threading. */
395 if ((gimple_code (stmt) != GIMPLE_ASSIGN
396 || TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
397 && (gimple_code (stmt) != GIMPLE_CALL
398 || gimple_call_lhs (stmt) == NULL_TREE
399 || TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME))
401 /* STMT might still have DEFS and we need to invalidate any known
402 equivalences for them.
404 Consider if STMT is a GIMPLE_ASM with one or more outputs that
405 feeds a conditional inside a loop. We might derive an equivalence
406 due to the conditional. */
407 tree op;
408 ssa_op_iter iter;
410 if (backedge_seen)
411 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
412 invalidate_equivalences (op, stack);
414 continue;
417 /* The result of __builtin_object_size depends on all the arguments
418 of a phi node. Temporarily using only one edge produces invalid
419 results. For example
421 if (x < 6)
422 goto l;
423 else
424 goto l;
427 r = PHI <&w[2].a[1](2), &a.a[6](3)>
428 __builtin_object_size (r, 0)
430 The result of __builtin_object_size is defined to be the maximum of
431 remaining bytes. If we use only one edge on the phi, the result will
432 change to be the remaining bytes for the corresponding phi argument.
434 Similarly for __builtin_constant_p:
436 r = PHI <1(2), 2(3)>
437 __builtin_constant_p (r)
439 Both PHI arguments are constant, but x ? 1 : 2 is still not
440 constant. */
442 if (is_gimple_call (stmt))
444 tree fndecl = gimple_call_fndecl (stmt);
445 if (fndecl
446 && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_OBJECT_SIZE
447 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P))
449 if (backedge_seen)
451 tree lhs = gimple_get_lhs (stmt);
452 invalidate_equivalences (lhs, stack);
454 continue;
458 /* At this point we have a statement which assigns an RHS to an
459 SSA_VAR on the LHS. We want to try and simplify this statement
460 to expose more context sensitive equivalences which in turn may
461 allow us to simplify the condition at the end of the loop.
463 Handle simple copy operations as well as implied copies from
464 ASSERT_EXPRs. */
465 if (gimple_assign_single_p (stmt)
466 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
467 cached_lhs = gimple_assign_rhs1 (stmt);
468 else if (gimple_assign_single_p (stmt)
469 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
470 cached_lhs = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
471 else
473 /* A statement that is not a trivial copy or ASSERT_EXPR.
474 We're going to temporarily copy propagate the operands
475 and see if that allows us to simplify this statement. */
476 tree *copy;
477 ssa_op_iter iter;
478 use_operand_p use_p;
479 unsigned int num, i = 0;
481 num = NUM_SSA_OPERANDS (stmt, (SSA_OP_USE | SSA_OP_VUSE));
482 copy = XCNEWVEC (tree, num);
484 /* Make a copy of the uses & vuses into USES_COPY, then cprop into
485 the operands. */
486 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE | SSA_OP_VUSE)
488 tree tmp = NULL;
489 tree use = USE_FROM_PTR (use_p);
491 copy[i++] = use;
492 if (TREE_CODE (use) == SSA_NAME)
493 tmp = SSA_NAME_VALUE (use);
494 if (tmp)
495 SET_USE (use_p, tmp);
498 /* Try to fold/lookup the new expression. Inserting the
499 expression into the hash table is unlikely to help. */
500 if (is_gimple_call (stmt))
501 cached_lhs = fold_call_stmt (as_a <gcall *> (stmt), false);
502 else
503 cached_lhs = fold_assignment_stmt (stmt);
505 if (!cached_lhs
506 || (TREE_CODE (cached_lhs) != SSA_NAME
507 && !is_gimple_min_invariant (cached_lhs)))
508 cached_lhs = (*simplify) (stmt, stmt);
510 /* Restore the statement's original uses/defs. */
511 i = 0;
512 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE | SSA_OP_VUSE)
513 SET_USE (use_p, copy[i++]);
515 free (copy);
518 /* Record the context sensitive equivalence if we were able
519 to simplify this statement.
521 If we have traversed a backedge at some point during threading,
522 then always enter something here. Either a real equivalence,
523 or a NULL_TREE equivalence which is effectively invalidation of
524 prior equivalences. */
525 if (cached_lhs
526 && (TREE_CODE (cached_lhs) == SSA_NAME
527 || is_gimple_min_invariant (cached_lhs)))
528 record_temporary_equivalence (gimple_get_lhs (stmt), cached_lhs, stack);
529 else if (backedge_seen)
530 invalidate_equivalences (gimple_get_lhs (stmt), stack);
532 return stmt;
535 /* Once we have passed a backedge in the CFG when threading, we do not want to
536 utilize edge equivalences for simplification purpose. They are no longer
537 necessarily valid. We use this callback rather than the ones provided by
538 DOM/VRP to achieve that effect. */
539 static tree
540 dummy_simplify (gimple stmt1 ATTRIBUTE_UNUSED, gimple stmt2 ATTRIBUTE_UNUSED)
542 return NULL_TREE;
545 /* Simplify the control statement at the end of the block E->dest.
547 To avoid allocating memory unnecessarily, a scratch GIMPLE_COND
548 is available to use/clobber in DUMMY_COND.
550 Use SIMPLIFY (a pointer to a callback function) to further simplify
551 a condition using pass specific information.
553 Return the simplified condition or NULL if simplification could
554 not be performed. */
556 static tree
557 simplify_control_stmt_condition (edge e,
558 gimple stmt,
559 gcond *dummy_cond,
560 tree (*simplify) (gimple, gimple),
561 bool handle_dominating_asserts)
563 tree cond, cached_lhs;
564 enum gimple_code code = gimple_code (stmt);
566 /* For comparisons, we have to update both operands, then try
567 to simplify the comparison. */
568 if (code == GIMPLE_COND)
570 tree op0, op1;
571 enum tree_code cond_code;
573 op0 = gimple_cond_lhs (stmt);
574 op1 = gimple_cond_rhs (stmt);
575 cond_code = gimple_cond_code (stmt);
577 /* Get the current value of both operands. */
578 if (TREE_CODE (op0) == SSA_NAME)
580 for (int i = 0; i < 2; i++)
582 if (TREE_CODE (op0) == SSA_NAME
583 && SSA_NAME_VALUE (op0))
584 op0 = SSA_NAME_VALUE (op0);
585 else
586 break;
590 if (TREE_CODE (op1) == SSA_NAME)
592 for (int i = 0; i < 2; i++)
594 if (TREE_CODE (op1) == SSA_NAME
595 && SSA_NAME_VALUE (op1))
596 op1 = SSA_NAME_VALUE (op1);
597 else
598 break;
602 if (handle_dominating_asserts)
604 /* Now see if the operand was consumed by an ASSERT_EXPR
605 which dominates E->src. If so, we want to replace the
606 operand with the LHS of the ASSERT_EXPR. */
607 if (TREE_CODE (op0) == SSA_NAME)
608 op0 = lhs_of_dominating_assert (op0, e->src, stmt);
610 if (TREE_CODE (op1) == SSA_NAME)
611 op1 = lhs_of_dominating_assert (op1, e->src, stmt);
614 /* We may need to canonicalize the comparison. For
615 example, op0 might be a constant while op1 is an
616 SSA_NAME. Failure to canonicalize will cause us to
617 miss threading opportunities. */
618 if (tree_swap_operands_p (op0, op1, false))
620 tree tmp;
621 cond_code = swap_tree_comparison (cond_code);
622 tmp = op0;
623 op0 = op1;
624 op1 = tmp;
627 /* Stuff the operator and operands into our dummy conditional
628 expression. */
629 gimple_cond_set_code (dummy_cond, cond_code);
630 gimple_cond_set_lhs (dummy_cond, op0);
631 gimple_cond_set_rhs (dummy_cond, op1);
633 /* We absolutely do not care about any type conversions
634 we only care about a zero/nonzero value. */
635 fold_defer_overflow_warnings ();
637 cached_lhs = fold_binary (cond_code, boolean_type_node, op0, op1);
638 if (cached_lhs)
639 while (CONVERT_EXPR_P (cached_lhs))
640 cached_lhs = TREE_OPERAND (cached_lhs, 0);
642 fold_undefer_overflow_warnings ((cached_lhs
643 && is_gimple_min_invariant (cached_lhs)),
644 stmt, WARN_STRICT_OVERFLOW_CONDITIONAL);
646 /* If we have not simplified the condition down to an invariant,
647 then use the pass specific callback to simplify the condition. */
648 if (!cached_lhs
649 || !is_gimple_min_invariant (cached_lhs))
650 cached_lhs = (*simplify) (dummy_cond, stmt);
652 return cached_lhs;
655 if (code == GIMPLE_SWITCH)
656 cond = gimple_switch_index (as_a <gswitch *> (stmt));
657 else if (code == GIMPLE_GOTO)
658 cond = gimple_goto_dest (stmt);
659 else
660 gcc_unreachable ();
662 /* We can have conditionals which just test the state of a variable
663 rather than use a relational operator. These are simpler to handle. */
664 if (TREE_CODE (cond) == SSA_NAME)
666 tree original_lhs = cond;
667 cached_lhs = cond;
669 /* Get the variable's current value from the equivalence chains.
671 It is possible to get loops in the SSA_NAME_VALUE chains
672 (consider threading the backedge of a loop where we have
673 a loop invariant SSA_NAME used in the condition. */
674 if (cached_lhs)
676 for (int i = 0; i < 2; i++)
678 if (TREE_CODE (cached_lhs) == SSA_NAME
679 && SSA_NAME_VALUE (cached_lhs))
680 cached_lhs = SSA_NAME_VALUE (cached_lhs);
681 else
682 break;
686 /* If we're dominated by a suitable ASSERT_EXPR, then
687 update CACHED_LHS appropriately. */
688 if (handle_dominating_asserts && TREE_CODE (cached_lhs) == SSA_NAME)
689 cached_lhs = lhs_of_dominating_assert (cached_lhs, e->src, stmt);
691 /* If we haven't simplified to an invariant yet, then use the
692 pass specific callback to try and simplify it further. */
693 if (cached_lhs && ! is_gimple_min_invariant (cached_lhs))
694 cached_lhs = (*simplify) (stmt, stmt);
696 /* We couldn't find an invariant. But, callers of this
697 function may be able to do something useful with the
698 unmodified destination. */
699 if (!cached_lhs)
700 cached_lhs = original_lhs;
702 else
703 cached_lhs = NULL;
705 return cached_lhs;
708 /* Copy debug stmts from DEST's chain of single predecessors up to
709 SRC, so that we don't lose the bindings as PHI nodes are introduced
710 when DEST gains new predecessors. */
711 void
712 propagate_threaded_block_debug_into (basic_block dest, basic_block src)
714 if (!MAY_HAVE_DEBUG_STMTS)
715 return;
717 if (!single_pred_p (dest))
718 return;
720 gcc_checking_assert (dest != src);
722 gimple_stmt_iterator gsi = gsi_after_labels (dest);
723 int i = 0;
724 const int alloc_count = 16; // ?? Should this be a PARAM?
726 /* Estimate the number of debug vars overridden in the beginning of
727 DEST, to tell how many we're going to need to begin with. */
728 for (gimple_stmt_iterator si = gsi;
729 i * 4 <= alloc_count * 3 && !gsi_end_p (si); gsi_next (&si))
731 gimple stmt = gsi_stmt (si);
732 if (!is_gimple_debug (stmt))
733 break;
734 i++;
737 auto_vec<tree, alloc_count> fewvars;
738 hash_set<tree> *vars = NULL;
740 /* If we're already starting with 3/4 of alloc_count, go for a
741 hash_set, otherwise start with an unordered stack-allocated
742 VEC. */
743 if (i * 4 > alloc_count * 3)
744 vars = new hash_set<tree>;
746 /* Now go through the initial debug stmts in DEST again, this time
747 actually inserting in VARS or FEWVARS. Don't bother checking for
748 duplicates in FEWVARS. */
749 for (gimple_stmt_iterator si = gsi; !gsi_end_p (si); gsi_next (&si))
751 gimple stmt = gsi_stmt (si);
752 if (!is_gimple_debug (stmt))
753 break;
755 tree var;
757 if (gimple_debug_bind_p (stmt))
758 var = gimple_debug_bind_get_var (stmt);
759 else if (gimple_debug_source_bind_p (stmt))
760 var = gimple_debug_source_bind_get_var (stmt);
761 else
762 gcc_unreachable ();
764 if (vars)
765 vars->add (var);
766 else
767 fewvars.quick_push (var);
770 basic_block bb = dest;
774 bb = single_pred (bb);
775 for (gimple_stmt_iterator si = gsi_last_bb (bb);
776 !gsi_end_p (si); gsi_prev (&si))
778 gimple stmt = gsi_stmt (si);
779 if (!is_gimple_debug (stmt))
780 continue;
782 tree var;
784 if (gimple_debug_bind_p (stmt))
785 var = gimple_debug_bind_get_var (stmt);
786 else if (gimple_debug_source_bind_p (stmt))
787 var = gimple_debug_source_bind_get_var (stmt);
788 else
789 gcc_unreachable ();
791 /* Discard debug bind overlaps. ??? Unlike stmts from src,
792 copied into a new block that will precede BB, debug bind
793 stmts in bypassed BBs may actually be discarded if
794 they're overwritten by subsequent debug bind stmts, which
795 might be a problem once we introduce stmt frontier notes
796 or somesuch. Adding `&& bb == src' to the condition
797 below will preserve all potentially relevant debug
798 notes. */
799 if (vars && vars->add (var))
800 continue;
801 else if (!vars)
803 int i = fewvars.length ();
804 while (i--)
805 if (fewvars[i] == var)
806 break;
807 if (i >= 0)
808 continue;
810 if (fewvars.length () < (unsigned) alloc_count)
811 fewvars.quick_push (var);
812 else
814 vars = new hash_set<tree>;
815 for (i = 0; i < alloc_count; i++)
816 vars->add (fewvars[i]);
817 fewvars.release ();
818 vars->add (var);
822 stmt = gimple_copy (stmt);
823 /* ??? Should we drop the location of the copy to denote
824 they're artificial bindings? */
825 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
828 while (bb != src && single_pred_p (bb));
830 if (vars)
831 delete vars;
832 else if (fewvars.exists ())
833 fewvars.release ();
836 /* See if TAKEN_EDGE->dest is a threadable block with no side effecs (ie, it
837 need not be duplicated as part of the CFG/SSA updating process).
839 If it is threadable, add it to PATH and VISITED and recurse, ultimately
840 returning TRUE from the toplevel call. Otherwise do nothing and
841 return false.
843 DUMMY_COND, HANDLE_DOMINATING_ASSERTS and SIMPLIFY are used to
844 try and simplify the condition at the end of TAKEN_EDGE->dest. */
845 static bool
846 thread_around_empty_blocks (edge taken_edge,
847 gcond *dummy_cond,
848 bool handle_dominating_asserts,
849 tree (*simplify) (gimple, gimple),
850 bitmap visited,
851 vec<jump_thread_edge *> *path,
852 bool *backedge_seen_p)
854 basic_block bb = taken_edge->dest;
855 gimple_stmt_iterator gsi;
856 gimple stmt;
857 tree cond;
859 /* The key property of these blocks is that they need not be duplicated
860 when threading. Thus they can not have visible side effects such
861 as PHI nodes. */
862 if (!gsi_end_p (gsi_start_phis (bb)))
863 return false;
865 /* Skip over DEBUG statements at the start of the block. */
866 gsi = gsi_start_nondebug_bb (bb);
868 /* If the block has no statements, but does have a single successor, then
869 it's just a forwarding block and we can thread through it trivially.
871 However, note that just threading through empty blocks with single
872 successors is not inherently profitable. For the jump thread to
873 be profitable, we must avoid a runtime conditional.
875 By taking the return value from the recursive call, we get the
876 desired effect of returning TRUE when we found a profitable jump
877 threading opportunity and FALSE otherwise.
879 This is particularly important when this routine is called after
880 processing a joiner block. Returning TRUE too aggressively in
881 that case results in pointless duplication of the joiner block. */
882 if (gsi_end_p (gsi))
884 if (single_succ_p (bb))
886 taken_edge = single_succ_edge (bb);
887 if (!bitmap_bit_p (visited, taken_edge->dest->index))
889 jump_thread_edge *x
890 = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
891 path->safe_push (x);
892 bitmap_set_bit (visited, taken_edge->dest->index);
893 *backedge_seen_p |= ((taken_edge->flags & EDGE_DFS_BACK) != 0);
894 if (*backedge_seen_p)
895 simplify = dummy_simplify;
896 return thread_around_empty_blocks (taken_edge,
897 dummy_cond,
898 handle_dominating_asserts,
899 simplify,
900 visited,
901 path,
902 backedge_seen_p);
906 /* We have a block with no statements, but multiple successors? */
907 return false;
910 /* The only real statements this block can have are a control
911 flow altering statement. Anything else stops the thread. */
912 stmt = gsi_stmt (gsi);
913 if (gimple_code (stmt) != GIMPLE_COND
914 && gimple_code (stmt) != GIMPLE_GOTO
915 && gimple_code (stmt) != GIMPLE_SWITCH)
916 return false;
918 /* If we have traversed a backedge, then we do not want to look
919 at certain expressions in the table that can not be relied upon.
920 Luckily the only code that looked at those expressions is the
921 SIMPLIFY callback, which we replace if we can no longer use it. */
922 if (*backedge_seen_p)
923 simplify = dummy_simplify;
925 /* Extract and simplify the condition. */
926 cond = simplify_control_stmt_condition (taken_edge, stmt, dummy_cond,
927 simplify, handle_dominating_asserts);
929 /* If the condition can be statically computed and we have not already
930 visited the destination edge, then add the taken edge to our thread
931 path. */
932 if (cond && is_gimple_min_invariant (cond))
934 taken_edge = find_taken_edge (bb, cond);
936 if (bitmap_bit_p (visited, taken_edge->dest->index))
937 return false;
938 bitmap_set_bit (visited, taken_edge->dest->index);
940 jump_thread_edge *x
941 = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
942 path->safe_push (x);
943 *backedge_seen_p |= ((taken_edge->flags & EDGE_DFS_BACK) != 0);
944 if (*backedge_seen_p)
945 simplify = dummy_simplify;
947 thread_around_empty_blocks (taken_edge,
948 dummy_cond,
949 handle_dominating_asserts,
950 simplify,
951 visited,
952 path,
953 backedge_seen_p);
954 return true;
957 return false;
960 /* Return true if the CFG contains at least one path from START_BB to END_BB.
961 When a path is found, record in PATH the blocks from END_BB to START_BB.
962 VISITED_BBS is used to make sure we don't fall into an infinite loop. Bound
963 the recursion to basic blocks belonging to LOOP. */
965 static bool
966 fsm_find_thread_path (basic_block start_bb, basic_block end_bb,
967 vec<basic_block, va_gc> *&path,
968 hash_set<basic_block> *visited_bbs, loop_p loop)
970 if (loop != start_bb->loop_father)
971 return false;
973 if (start_bb == end_bb)
975 vec_safe_push (path, start_bb);
976 return true;
979 if (!visited_bbs->add (start_bb))
981 edge e;
982 edge_iterator ei;
983 FOR_EACH_EDGE (e, ei, start_bb->succs)
984 if (fsm_find_thread_path (e->dest, end_bb, path, visited_bbs, loop))
986 vec_safe_push (path, start_bb);
987 return true;
991 return false;
994 static int max_threaded_paths;
996 /* We trace the value of the variable EXPR back through any phi nodes looking
997 for places where it gets a constant value and save the path. Stop after
998 having recorded MAX_PATHS jump threading paths. */
1000 static void
1001 fsm_find_control_statement_thread_paths (tree expr,
1002 hash_set<gimple> *visited_phis,
1003 vec<basic_block, va_gc> *&path)
1005 tree var = SSA_NAME_VAR (expr);
1006 gimple def_stmt = SSA_NAME_DEF_STMT (expr);
1007 basic_block var_bb = gimple_bb (def_stmt);
1009 if (var == NULL || var_bb == NULL)
1010 return;
1012 /* For the moment we assume that an SSA chain only contains phi nodes, and
1013 eventually one of the phi arguments will be an integer constant. In the
1014 future, this could be extended to also handle simple assignments of
1015 arithmetic operations. */
1016 if (gimple_code (def_stmt) != GIMPLE_PHI)
1017 return;
1019 /* Avoid infinite recursion. */
1020 if (visited_phis->add (def_stmt))
1021 return;
1023 gphi *phi = as_a <gphi *> (def_stmt);
1024 int next_path_length = 0;
1025 basic_block last_bb_in_path = path->last ();
1027 /* Following the chain of SSA_NAME definitions, we jumped from a definition in
1028 LAST_BB_IN_PATH to a definition in VAR_BB. When these basic blocks are
1029 different, append to PATH the blocks from LAST_BB_IN_PATH to VAR_BB. */
1030 if (var_bb != last_bb_in_path)
1032 edge e;
1033 int e_count = 0;
1034 edge_iterator ei;
1035 vec<basic_block, va_gc> *next_path;
1036 vec_alloc (next_path, n_basic_blocks_for_fn (cfun));
1038 FOR_EACH_EDGE (e, ei, last_bb_in_path->preds)
1040 hash_set<basic_block> *visited_bbs = new hash_set<basic_block>;
1042 if (fsm_find_thread_path (var_bb, e->src, next_path, visited_bbs,
1043 e->src->loop_father))
1044 ++e_count;
1046 delete visited_bbs;
1048 /* If there is more than one path, stop. */
1049 if (e_count > 1)
1051 vec_free (next_path);
1052 return;
1056 /* Stop if we have not found a path: this could occur when the recursion
1057 is stopped by one of the bounds. */
1058 if (e_count == 0)
1060 vec_free (next_path);
1061 return;
1064 /* Append all the nodes from NEXT_PATH to PATH. */
1065 vec_safe_splice (path, next_path);
1066 next_path_length = next_path->length ();
1067 vec_free (next_path);
1070 gcc_assert (path->last () == var_bb);
1072 /* Iterate over the arguments of PHI. */
1073 unsigned int i;
1074 for (i = 0; i < gimple_phi_num_args (phi); i++)
1076 tree arg = gimple_phi_arg_def (phi, i);
1077 basic_block bbi = gimple_phi_arg_edge (phi, i)->src;
1079 /* Skip edges pointing outside the current loop. */
1080 if (!arg || var_bb->loop_father != bbi->loop_father)
1081 continue;
1083 if (TREE_CODE (arg) == SSA_NAME)
1085 vec_safe_push (path, bbi);
1086 /* Recursively follow SSA_NAMEs looking for a constant definition. */
1087 fsm_find_control_statement_thread_paths (arg, visited_phis, path);
1088 path->pop ();
1089 continue;
1092 if (TREE_CODE (arg) != INTEGER_CST)
1093 continue;
1095 int path_length = path->length ();
1096 /* A path with less than 2 basic blocks should not be jump-threaded. */
1097 if (path_length < 2)
1098 continue;
1100 if (path_length > PARAM_VALUE (PARAM_MAX_FSM_THREAD_LENGTH))
1102 if (dump_file && (dump_flags & TDF_DETAILS))
1103 fprintf (dump_file, "FSM jump-thread path not considered: "
1104 "the number of basic blocks on the path "
1105 "exceeds PARAM_MAX_FSM_THREAD_LENGTH.\n");
1106 continue;
1109 if (max_threaded_paths <= 0)
1111 if (dump_file && (dump_flags & TDF_DETAILS))
1112 fprintf (dump_file, "FSM jump-thread path not considered: "
1113 "the number of previously recorded FSM paths to thread "
1114 "exceeds PARAM_MAX_FSM_THREAD_PATHS.\n");
1115 continue;
1118 /* Add BBI to the path. */
1119 vec_safe_push (path, bbi);
1120 ++path_length;
1122 int n_insns = 0;
1123 gimple_stmt_iterator gsi;
1124 int j;
1125 loop_p loop = (*path)[0]->loop_father;
1126 bool path_crosses_loops = false;
1128 /* Count the number of instructions on the path: as these instructions
1129 will have to be duplicated, we will not record the path if there are
1130 too many instructions on the path. Also check that all the blocks in
1131 the path belong to a single loop. */
1132 for (j = 1; j < path_length - 1; j++)
1134 basic_block bb = (*path)[j];
1136 if (bb->loop_father != loop)
1138 path_crosses_loops = true;
1139 break;
1142 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1144 gimple stmt = gsi_stmt (gsi);
1145 /* Do not count empty statements and labels. */
1146 if (gimple_code (stmt) != GIMPLE_NOP
1147 && gimple_code (stmt) != GIMPLE_LABEL
1148 && !is_gimple_debug (stmt))
1149 ++n_insns;
1153 if (path_crosses_loops)
1155 if (dump_file && (dump_flags & TDF_DETAILS))
1156 fprintf (dump_file, "FSM jump-thread path not considered: "
1157 "the path crosses loops.\n");
1158 path->pop ();
1159 continue;
1162 if (n_insns >= PARAM_VALUE (PARAM_MAX_FSM_THREAD_PATH_INSNS))
1164 if (dump_file && (dump_flags & TDF_DETAILS))
1165 fprintf (dump_file, "FSM jump-thread path not considered: "
1166 "the number of instructions on the path "
1167 "exceeds PARAM_MAX_FSM_THREAD_PATH_INSNS.\n");
1168 path->pop ();
1169 continue;
1172 vec<jump_thread_edge *> *jump_thread_path
1173 = new vec<jump_thread_edge *> ();
1175 /* Record the edges between the blocks in PATH. */
1176 for (j = 0; j < path_length - 1; j++)
1178 edge e = find_edge ((*path)[path_length - j - 1],
1179 (*path)[path_length - j - 2]);
1180 gcc_assert (e);
1181 jump_thread_edge *x = new jump_thread_edge (e, EDGE_FSM_THREAD);
1182 jump_thread_path->safe_push (x);
1185 /* Add the edge taken when the control variable has value ARG. */
1186 edge taken_edge = find_taken_edge ((*path)[0], arg);
1187 jump_thread_edge *x
1188 = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
1189 jump_thread_path->safe_push (x);
1191 register_jump_thread (jump_thread_path);
1192 --max_threaded_paths;
1194 /* Remove BBI from the path. */
1195 path->pop ();
1198 /* Remove all the nodes that we added from NEXT_PATH. */
1199 if (next_path_length)
1200 vec_safe_truncate (path, (path->length () - next_path_length));
1203 /* We are exiting E->src, see if E->dest ends with a conditional
1204 jump which has a known value when reached via E.
1206 E->dest can have arbitrary side effects which, if threading is
1207 successful, will be maintained.
1209 Special care is necessary if E is a back edge in the CFG as we
1210 may have already recorded equivalences for E->dest into our
1211 various tables, including the result of the conditional at
1212 the end of E->dest. Threading opportunities are severely
1213 limited in that case to avoid short-circuiting the loop
1214 incorrectly.
1216 DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
1217 to avoid allocating memory.
1219 HANDLE_DOMINATING_ASSERTS is true if we should try to replace operands of
1220 the simplified condition with left-hand sides of ASSERT_EXPRs they are
1221 used in.
1223 STACK is used to undo temporary equivalences created during the walk of
1224 E->dest.
1226 SIMPLIFY is a pass-specific function used to simplify statements.
1228 Our caller is responsible for restoring the state of the expression
1229 and const_and_copies stacks.
1231 Positive return value is success. Zero return value is failure, but
1232 the block can still be duplicated as a joiner in a jump thread path,
1233 negative indicates the block should not be duplicated and thus is not
1234 suitable for a joiner in a jump threading path. */
1236 static int
1237 thread_through_normal_block (edge e,
1238 gcond *dummy_cond,
1239 bool handle_dominating_asserts,
1240 vec<tree> *stack,
1241 tree (*simplify) (gimple, gimple),
1242 vec<jump_thread_edge *> *path,
1243 bitmap visited,
1244 bool *backedge_seen_p)
1246 /* If we have traversed a backedge, then we do not want to look
1247 at certain expressions in the table that can not be relied upon.
1248 Luckily the only code that looked at those expressions is the
1249 SIMPLIFY callback, which we replace if we can no longer use it. */
1250 if (*backedge_seen_p)
1251 simplify = dummy_simplify;
1253 /* PHIs create temporary equivalences.
1254 Note that if we found a PHI that made the block non-threadable, then
1255 we need to bubble that up to our caller in the same manner we do
1256 when we prematurely stop processing statements below. */
1257 if (!record_temporary_equivalences_from_phis (e, stack))
1258 return -1;
1260 /* Now walk each statement recording any context sensitive
1261 temporary equivalences we can detect. */
1262 gimple stmt
1263 = record_temporary_equivalences_from_stmts_at_dest (e, stack, simplify,
1264 *backedge_seen_p);
1266 /* If we didn't look at all the statements, the most likely reason is
1267 there were too many and thus duplicating this block is not profitable.
1269 Also note if we do not look at all the statements, then we may not
1270 have invalidated equivalences that are no longer valid if we threaded
1271 around a loop. Thus we must signal to our caller that this block
1272 is not suitable for use as a joiner in a threading path. */
1273 if (!stmt)
1274 return -1;
1276 /* If we stopped at a COND_EXPR or SWITCH_EXPR, see if we know which arm
1277 will be taken. */
1278 if (gimple_code (stmt) == GIMPLE_COND
1279 || gimple_code (stmt) == GIMPLE_GOTO
1280 || gimple_code (stmt) == GIMPLE_SWITCH)
1282 tree cond;
1284 /* Extract and simplify the condition. */
1285 cond = simplify_control_stmt_condition (e, stmt, dummy_cond, simplify,
1286 handle_dominating_asserts);
1288 if (!cond)
1289 return 0;
1291 if (is_gimple_min_invariant (cond))
1293 edge taken_edge = find_taken_edge (e->dest, cond);
1294 basic_block dest = (taken_edge ? taken_edge->dest : NULL);
1296 /* DEST could be NULL for a computed jump to an absolute
1297 address. */
1298 if (dest == NULL
1299 || dest == e->dest
1300 || bitmap_bit_p (visited, dest->index))
1301 return 0;
1303 /* Only push the EDGE_START_JUMP_THREAD marker if this is
1304 first edge on the path. */
1305 if (path->length () == 0)
1307 jump_thread_edge *x
1308 = new jump_thread_edge (e, EDGE_START_JUMP_THREAD);
1309 path->safe_push (x);
1310 *backedge_seen_p |= ((e->flags & EDGE_DFS_BACK) != 0);
1313 jump_thread_edge *x
1314 = new jump_thread_edge (taken_edge, EDGE_COPY_SRC_BLOCK);
1315 path->safe_push (x);
1316 *backedge_seen_p |= ((taken_edge->flags & EDGE_DFS_BACK) != 0);
1317 if (*backedge_seen_p)
1318 simplify = dummy_simplify;
1320 /* See if we can thread through DEST as well, this helps capture
1321 secondary effects of threading without having to re-run DOM or
1322 VRP.
1324 We don't want to thread back to a block we have already
1325 visited. This may be overly conservative. */
1326 bitmap_set_bit (visited, dest->index);
1327 bitmap_set_bit (visited, e->dest->index);
1328 thread_around_empty_blocks (taken_edge,
1329 dummy_cond,
1330 handle_dominating_asserts,
1331 simplify,
1332 visited,
1333 path,
1334 backedge_seen_p);
1335 return 1;
1338 if (!flag_expensive_optimizations
1339 || optimize_function_for_size_p (cfun)
1340 || TREE_CODE (cond) != SSA_NAME
1341 || e->dest->loop_father != e->src->loop_father
1342 || loop_depth (e->dest->loop_father) == 0)
1343 return 0;
1345 /* When COND cannot be simplified, try to find paths from a control
1346 statement back through the PHI nodes which would affect that control
1347 statement. */
1348 vec<basic_block, va_gc> *bb_path;
1349 vec_alloc (bb_path, n_basic_blocks_for_fn (cfun));
1350 vec_safe_push (bb_path, e->dest);
1351 hash_set<gimple> *visited_phis = new hash_set<gimple>;
1353 max_threaded_paths = PARAM_VALUE (PARAM_MAX_FSM_THREAD_PATHS);
1354 fsm_find_control_statement_thread_paths (cond, visited_phis, bb_path);
1356 delete visited_phis;
1357 vec_free (bb_path);
1359 return 0;
1362 /* We are exiting E->src, see if E->dest ends with a conditional
1363 jump which has a known value when reached via E.
1365 Special care is necessary if E is a back edge in the CFG as we
1366 may have already recorded equivalences for E->dest into our
1367 various tables, including the result of the conditional at
1368 the end of E->dest. Threading opportunities are severely
1369 limited in that case to avoid short-circuiting the loop
1370 incorrectly.
1372 Note it is quite common for the first block inside a loop to
1373 end with a conditional which is either always true or always
1374 false when reached via the loop backedge. Thus we do not want
1375 to blindly disable threading across a loop backedge.
1377 DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
1378 to avoid allocating memory.
1380 HANDLE_DOMINATING_ASSERTS is true if we should try to replace operands of
1381 the simplified condition with left-hand sides of ASSERT_EXPRs they are
1382 used in.
1384 STACK is used to undo temporary equivalences created during the walk of
1385 E->dest.
1387 SIMPLIFY is a pass-specific function used to simplify statements. */
1389 void
1390 thread_across_edge (gcond *dummy_cond,
1391 edge e,
1392 bool handle_dominating_asserts,
1393 vec<tree> *stack,
1394 tree (*simplify) (gimple, gimple))
1396 bitmap visited = BITMAP_ALLOC (NULL);
1397 bool backedge_seen;
1399 stmt_count = 0;
1401 vec<jump_thread_edge *> *path = new vec<jump_thread_edge *> ();
1402 bitmap_clear (visited);
1403 bitmap_set_bit (visited, e->src->index);
1404 bitmap_set_bit (visited, e->dest->index);
1405 backedge_seen = ((e->flags & EDGE_DFS_BACK) != 0);
1406 if (backedge_seen)
1407 simplify = dummy_simplify;
1409 int threaded = thread_through_normal_block (e, dummy_cond,
1410 handle_dominating_asserts,
1411 stack, simplify, path,
1412 visited, &backedge_seen);
1413 if (threaded > 0)
1415 propagate_threaded_block_debug_into (path->last ()->e->dest,
1416 e->dest);
1417 remove_temporary_equivalences (stack);
1418 BITMAP_FREE (visited);
1419 register_jump_thread (path);
1420 return;
1422 else
1424 /* Negative and zero return values indicate no threading was possible,
1425 thus there should be no edges on the thread path and no need to walk
1426 through the vector entries. */
1427 gcc_assert (path->length () == 0);
1428 path->release ();
1429 delete path;
1431 /* A negative status indicates the target block was deemed too big to
1432 duplicate. Just quit now rather than trying to use the block as
1433 a joiner in a jump threading path.
1435 This prevents unnecessary code growth, but more importantly if we
1436 do not look at all the statements in the block, then we may have
1437 missed some invalidations if we had traversed a backedge! */
1438 if (threaded < 0)
1440 BITMAP_FREE (visited);
1441 remove_temporary_equivalences (stack);
1442 return;
1446 /* We were unable to determine what out edge from E->dest is taken. However,
1447 we might still be able to thread through successors of E->dest. This
1448 often occurs when E->dest is a joiner block which then fans back out
1449 based on redundant tests.
1451 If so, we'll copy E->dest and redirect the appropriate predecessor to
1452 the copy. Within the copy of E->dest, we'll thread one or more edges
1453 to points deeper in the CFG.
1455 This is a stopgap until we have a more structured approach to path
1456 isolation. */
1458 edge taken_edge;
1459 edge_iterator ei;
1460 bool found;
1462 /* If E->dest has abnormal outgoing edges, then there's no guarantee
1463 we can safely redirect any of the edges. Just punt those cases. */
1464 FOR_EACH_EDGE (taken_edge, ei, e->dest->succs)
1465 if (taken_edge->flags & EDGE_ABNORMAL)
1467 remove_temporary_equivalences (stack);
1468 BITMAP_FREE (visited);
1469 return;
1472 /* Look at each successor of E->dest to see if we can thread through it. */
1473 FOR_EACH_EDGE (taken_edge, ei, e->dest->succs)
1475 /* Push a fresh marker so we can unwind the equivalences created
1476 for each of E->dest's successors. */
1477 stack->safe_push (NULL_TREE);
1479 /* Avoid threading to any block we have already visited. */
1480 bitmap_clear (visited);
1481 bitmap_set_bit (visited, e->src->index);
1482 bitmap_set_bit (visited, e->dest->index);
1483 bitmap_set_bit (visited, taken_edge->dest->index);
1484 vec<jump_thread_edge *> *path = new vec<jump_thread_edge *> ();
1486 /* Record whether or not we were able to thread through a successor
1487 of E->dest. */
1488 jump_thread_edge *x = new jump_thread_edge (e, EDGE_START_JUMP_THREAD);
1489 path->safe_push (x);
1491 x = new jump_thread_edge (taken_edge, EDGE_COPY_SRC_JOINER_BLOCK);
1492 path->safe_push (x);
1493 found = false;
1494 backedge_seen = ((e->flags & EDGE_DFS_BACK) != 0);
1495 backedge_seen |= ((taken_edge->flags & EDGE_DFS_BACK) != 0);
1496 if (backedge_seen)
1497 simplify = dummy_simplify;
1498 found = thread_around_empty_blocks (taken_edge,
1499 dummy_cond,
1500 handle_dominating_asserts,
1501 simplify,
1502 visited,
1503 path,
1504 &backedge_seen);
1506 if (backedge_seen)
1507 simplify = dummy_simplify;
1509 if (!found)
1510 found = thread_through_normal_block (path->last ()->e, dummy_cond,
1511 handle_dominating_asserts,
1512 stack, simplify, path, visited,
1513 &backedge_seen) > 0;
1515 /* If we were able to thread through a successor of E->dest, then
1516 record the jump threading opportunity. */
1517 if (found)
1519 propagate_threaded_block_debug_into (path->last ()->e->dest,
1520 taken_edge->dest);
1521 register_jump_thread (path);
1523 else
1525 delete_jump_thread_path (path);
1528 /* And unwind the equivalence table. */
1529 remove_temporary_equivalences (stack);
1531 BITMAP_FREE (visited);
1534 remove_temporary_equivalences (stack);