]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree-ssa-threadedge.c
2015-06-08 Andrew MacLeod <amacleod@redhat.com>
[thirdparty/gcc.git] / gcc / tree-ssa-threadedge.c
1 /* SSA Jump Threading
2 Copyright (C) 2005-2015 Free Software Foundation, Inc.
3 Contributed by Jeff Law <law@redhat.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "input.h"
26 #include "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "flags.h"
31 #include "tm_p.h"
32 #include "predict.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-scopedtables.h"
58 #include "tree-ssa-threadedge.h"
59 #include "tree-ssa-loop.h"
60 #include "builtins.h"
61 #include "cfg.h"
62 #include "cfganal.h"
63
64 /* To avoid code explosion due to jump threading, we limit the
65 number of statements we are going to copy. This variable
66 holds the number of statements currently seen that we'll have
67 to copy as part of the jump threading process. */
68 static int stmt_count;
69
70 /* Array to record value-handles per SSA_NAME. */
71 vec<tree> ssa_name_values;
72
73 /* Set the value for the SSA name NAME to VALUE. */
74
75 void
76 set_ssa_name_value (tree name, tree value)
77 {
78 if (SSA_NAME_VERSION (name) >= ssa_name_values.length ())
79 ssa_name_values.safe_grow_cleared (SSA_NAME_VERSION (name) + 1);
80 if (value && TREE_OVERFLOW_P (value))
81 value = drop_tree_overflow (value);
82 ssa_name_values[SSA_NAME_VERSION (name)] = value;
83 }
84
85 /* Initialize the per SSA_NAME value-handles array. Returns it. */
86 void
87 threadedge_initialize_values (void)
88 {
89 gcc_assert (!ssa_name_values.exists ());
90 ssa_name_values.create (num_ssa_names);
91 }
92
93 /* Free the per SSA_NAME value-handle array. */
94 void
95 threadedge_finalize_values (void)
96 {
97 ssa_name_values.release ();
98 }
99
100 /* Return TRUE if we may be able to thread an incoming edge into
101 BB to an outgoing edge from BB. Return FALSE otherwise. */
102
103 bool
104 potentially_threadable_block (basic_block bb)
105 {
106 gimple_stmt_iterator gsi;
107
108 /* Special case. We can get blocks that are forwarders, but are
109 not optimized away because they forward from outside a loop
110 to the loop header. We want to thread through them as we can
111 sometimes thread to the loop exit, which is obviously profitable.
112 the interesting case here is when the block has PHIs. */
113 if (gsi_end_p (gsi_start_nondebug_bb (bb))
114 && !gsi_end_p (gsi_start_phis (bb)))
115 return true;
116
117 /* If BB has a single successor or a single predecessor, then
118 there is no threading opportunity. */
119 if (single_succ_p (bb) || single_pred_p (bb))
120 return false;
121
122 /* If BB does not end with a conditional, switch or computed goto,
123 then there is no threading opportunity. */
124 gsi = gsi_last_bb (bb);
125 if (gsi_end_p (gsi)
126 || ! gsi_stmt (gsi)
127 || (gimple_code (gsi_stmt (gsi)) != GIMPLE_COND
128 && gimple_code (gsi_stmt (gsi)) != GIMPLE_GOTO
129 && gimple_code (gsi_stmt (gsi)) != GIMPLE_SWITCH))
130 return false;
131
132 return true;
133 }
134
135 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
136 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
137 BB. If no such ASSERT_EXPR is found, return OP. */
138
139 static tree
140 lhs_of_dominating_assert (tree op, basic_block bb, gimple stmt)
141 {
142 imm_use_iterator imm_iter;
143 gimple use_stmt;
144 use_operand_p use_p;
145
146 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
147 {
148 use_stmt = USE_STMT (use_p);
149 if (use_stmt != stmt
150 && gimple_assign_single_p (use_stmt)
151 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ASSERT_EXPR
152 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) == op
153 && dominated_by_p (CDI_DOMINATORS, bb, gimple_bb (use_stmt)))
154 {
155 return gimple_assign_lhs (use_stmt);
156 }
157 }
158 return op;
159 }
160
161 /* Record temporary equivalences created by PHIs at the target of the
162 edge E. Record unwind information for the equivalences onto STACK.
163
164 If a PHI which prevents threading is encountered, then return FALSE
165 indicating we should not thread this edge, else return TRUE.
166
167 If SRC_MAP/DST_MAP exist, then mark the source and destination SSA_NAMEs
168 of any equivalences recorded. We use this to make invalidation after
169 traversing back edges less painful. */
170
171 static bool
172 record_temporary_equivalences_from_phis (edge e, const_and_copies *const_and_copies)
173 {
174 gphi_iterator gsi;
175
176 /* Each PHI creates a temporary equivalence, record them.
177 These are context sensitive equivalences and will be removed
178 later. */
179 for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
180 {
181 gphi *phi = gsi.phi ();
182 tree src = PHI_ARG_DEF_FROM_EDGE (phi, e);
183 tree dst = gimple_phi_result (phi);
184
185 /* If the desired argument is not the same as this PHI's result
186 and it is set by a PHI in E->dest, then we can not thread
187 through E->dest. */
188 if (src != dst
189 && TREE_CODE (src) == SSA_NAME
190 && gimple_code (SSA_NAME_DEF_STMT (src)) == GIMPLE_PHI
191 && gimple_bb (SSA_NAME_DEF_STMT (src)) == e->dest)
192 return false;
193
194 /* We consider any non-virtual PHI as a statement since it
195 count result in a constant assignment or copy operation. */
196 if (!virtual_operand_p (dst))
197 stmt_count++;
198
199 const_and_copies->record_const_or_copy (dst, src);
200 }
201 return true;
202 }
203
204 /* Fold the RHS of an assignment statement and return it as a tree.
205 May return NULL_TREE if no simplification is possible. */
206
207 static tree
208 fold_assignment_stmt (gimple stmt)
209 {
210 enum tree_code subcode = gimple_assign_rhs_code (stmt);
211
212 switch (get_gimple_rhs_class (subcode))
213 {
214 case GIMPLE_SINGLE_RHS:
215 return fold (gimple_assign_rhs1 (stmt));
216
217 case GIMPLE_UNARY_RHS:
218 {
219 tree lhs = gimple_assign_lhs (stmt);
220 tree op0 = gimple_assign_rhs1 (stmt);
221 return fold_unary (subcode, TREE_TYPE (lhs), op0);
222 }
223
224 case GIMPLE_BINARY_RHS:
225 {
226 tree lhs = gimple_assign_lhs (stmt);
227 tree op0 = gimple_assign_rhs1 (stmt);
228 tree op1 = gimple_assign_rhs2 (stmt);
229 return fold_binary (subcode, TREE_TYPE (lhs), op0, op1);
230 }
231
232 case GIMPLE_TERNARY_RHS:
233 {
234 tree lhs = gimple_assign_lhs (stmt);
235 tree op0 = gimple_assign_rhs1 (stmt);
236 tree op1 = gimple_assign_rhs2 (stmt);
237 tree op2 = gimple_assign_rhs3 (stmt);
238
239 /* Sadly, we have to handle conditional assignments specially
240 here, because fold expects all the operands of an expression
241 to be folded before the expression itself is folded, but we
242 can't just substitute the folded condition here. */
243 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
244 op0 = fold (op0);
245
246 return fold_ternary (subcode, TREE_TYPE (lhs), op0, op1, op2);
247 }
248
249 default:
250 gcc_unreachable ();
251 }
252 }
253
254 /* Try to simplify each statement in E->dest, ultimately leading to
255 a simplification of the COND_EXPR at the end of E->dest.
256
257 Record unwind information for temporary equivalences onto STACK.
258
259 Use SIMPLIFY (a pointer to a callback function) to further simplify
260 statements using pass specific information.
261
262 We might consider marking just those statements which ultimately
263 feed the COND_EXPR. It's not clear if the overhead of bookkeeping
264 would be recovered by trying to simplify fewer statements.
265
266 If we are able to simplify a statement into the form
267 SSA_NAME = (SSA_NAME | gimple invariant), then we can record
268 a context sensitive equivalence which may help us simplify
269 later statements in E->dest. */
270
271 static gimple
272 record_temporary_equivalences_from_stmts_at_dest (edge e,
273 const_and_copies *const_and_copies,
274 tree (*simplify) (gimple,
275 gimple),
276 bool backedge_seen)
277 {
278 gimple stmt = NULL;
279 gimple_stmt_iterator gsi;
280 int max_stmt_count;
281
282 max_stmt_count = PARAM_VALUE (PARAM_MAX_JUMP_THREAD_DUPLICATION_STMTS);
283
284 /* Walk through each statement in the block recording equivalences
285 we discover. Note any equivalences we discover are context
286 sensitive (ie, are dependent on traversing E) and must be unwound
287 when we're finished processing E. */
288 for (gsi = gsi_start_bb (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
289 {
290 tree cached_lhs = NULL;
291
292 stmt = gsi_stmt (gsi);
293
294 /* Ignore empty statements and labels. */
295 if (gimple_code (stmt) == GIMPLE_NOP
296 || gimple_code (stmt) == GIMPLE_LABEL
297 || is_gimple_debug (stmt))
298 continue;
299
300 /* If the statement has volatile operands, then we assume we
301 can not thread through this block. This is overly
302 conservative in some ways. */
303 if (gimple_code (stmt) == GIMPLE_ASM
304 && gimple_asm_volatile_p (as_a <gasm *> (stmt)))
305 return NULL;
306
307 /* If duplicating this block is going to cause too much code
308 expansion, then do not thread through this block. */
309 stmt_count++;
310 if (stmt_count > max_stmt_count)
311 return NULL;
312
313 /* If this is not a statement that sets an SSA_NAME to a new
314 value, then do not try to simplify this statement as it will
315 not simplify in any way that is helpful for jump threading. */
316 if ((gimple_code (stmt) != GIMPLE_ASSIGN
317 || TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
318 && (gimple_code (stmt) != GIMPLE_CALL
319 || gimple_call_lhs (stmt) == NULL_TREE
320 || TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME))
321 {
322 /* STMT might still have DEFS and we need to invalidate any known
323 equivalences for them.
324
325 Consider if STMT is a GIMPLE_ASM with one or more outputs that
326 feeds a conditional inside a loop. We might derive an equivalence
327 due to the conditional. */
328 tree op;
329 ssa_op_iter iter;
330
331 if (backedge_seen)
332 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
333 const_and_copies->invalidate (op);
334
335 continue;
336 }
337
338 /* The result of __builtin_object_size depends on all the arguments
339 of a phi node. Temporarily using only one edge produces invalid
340 results. For example
341
342 if (x < 6)
343 goto l;
344 else
345 goto l;
346
347 l:
348 r = PHI <&w[2].a[1](2), &a.a[6](3)>
349 __builtin_object_size (r, 0)
350
351 The result of __builtin_object_size is defined to be the maximum of
352 remaining bytes. If we use only one edge on the phi, the result will
353 change to be the remaining bytes for the corresponding phi argument.
354
355 Similarly for __builtin_constant_p:
356
357 r = PHI <1(2), 2(3)>
358 __builtin_constant_p (r)
359
360 Both PHI arguments are constant, but x ? 1 : 2 is still not
361 constant. */
362
363 if (is_gimple_call (stmt))
364 {
365 tree fndecl = gimple_call_fndecl (stmt);
366 if (fndecl
367 && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_OBJECT_SIZE
368 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P))
369 {
370 if (backedge_seen)
371 {
372 tree lhs = gimple_get_lhs (stmt);
373 const_and_copies->invalidate (lhs);
374 }
375 continue;
376 }
377 }
378
379 /* At this point we have a statement which assigns an RHS to an
380 SSA_VAR on the LHS. We want to try and simplify this statement
381 to expose more context sensitive equivalences which in turn may
382 allow us to simplify the condition at the end of the loop.
383
384 Handle simple copy operations as well as implied copies from
385 ASSERT_EXPRs. */
386 if (gimple_assign_single_p (stmt)
387 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
388 cached_lhs = gimple_assign_rhs1 (stmt);
389 else if (gimple_assign_single_p (stmt)
390 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
391 cached_lhs = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
392 else
393 {
394 /* A statement that is not a trivial copy or ASSERT_EXPR.
395 We're going to temporarily copy propagate the operands
396 and see if that allows us to simplify this statement. */
397 tree *copy;
398 ssa_op_iter iter;
399 use_operand_p use_p;
400 unsigned int num, i = 0;
401
402 num = NUM_SSA_OPERANDS (stmt, (SSA_OP_USE | SSA_OP_VUSE));
403 copy = XCNEWVEC (tree, num);
404
405 /* Make a copy of the uses & vuses into USES_COPY, then cprop into
406 the operands. */
407 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE | SSA_OP_VUSE)
408 {
409 tree tmp = NULL;
410 tree use = USE_FROM_PTR (use_p);
411
412 copy[i++] = use;
413 if (TREE_CODE (use) == SSA_NAME)
414 tmp = SSA_NAME_VALUE (use);
415 if (tmp)
416 SET_USE (use_p, tmp);
417 }
418
419 /* Try to fold/lookup the new expression. Inserting the
420 expression into the hash table is unlikely to help. */
421 if (is_gimple_call (stmt))
422 cached_lhs = fold_call_stmt (as_a <gcall *> (stmt), false);
423 else
424 cached_lhs = fold_assignment_stmt (stmt);
425
426 if (!cached_lhs
427 || (TREE_CODE (cached_lhs) != SSA_NAME
428 && !is_gimple_min_invariant (cached_lhs)))
429 cached_lhs = (*simplify) (stmt, stmt);
430
431 /* Restore the statement's original uses/defs. */
432 i = 0;
433 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE | SSA_OP_VUSE)
434 SET_USE (use_p, copy[i++]);
435
436 free (copy);
437 }
438
439 /* Record the context sensitive equivalence if we were able
440 to simplify this statement.
441
442 If we have traversed a backedge at some point during threading,
443 then always enter something here. Either a real equivalence,
444 or a NULL_TREE equivalence which is effectively invalidation of
445 prior equivalences. */
446 if (cached_lhs
447 && (TREE_CODE (cached_lhs) == SSA_NAME
448 || is_gimple_min_invariant (cached_lhs)))
449 const_and_copies->record_const_or_copy (gimple_get_lhs (stmt), cached_lhs);
450 else if (backedge_seen)
451 const_and_copies->invalidate (gimple_get_lhs (stmt));
452 }
453 return stmt;
454 }
455
456 /* Once we have passed a backedge in the CFG when threading, we do not want to
457 utilize edge equivalences for simplification purpose. They are no longer
458 necessarily valid. We use this callback rather than the ones provided by
459 DOM/VRP to achieve that effect. */
460 static tree
461 dummy_simplify (gimple stmt1 ATTRIBUTE_UNUSED, gimple stmt2 ATTRIBUTE_UNUSED)
462 {
463 return NULL_TREE;
464 }
465
466 /* Simplify the control statement at the end of the block E->dest.
467
468 To avoid allocating memory unnecessarily, a scratch GIMPLE_COND
469 is available to use/clobber in DUMMY_COND.
470
471 Use SIMPLIFY (a pointer to a callback function) to further simplify
472 a condition using pass specific information.
473
474 Return the simplified condition or NULL if simplification could
475 not be performed. */
476
477 static tree
478 simplify_control_stmt_condition (edge e,
479 gimple stmt,
480 gcond *dummy_cond,
481 tree (*simplify) (gimple, gimple),
482 bool handle_dominating_asserts)
483 {
484 tree cond, cached_lhs;
485 enum gimple_code code = gimple_code (stmt);
486
487 /* For comparisons, we have to update both operands, then try
488 to simplify the comparison. */
489 if (code == GIMPLE_COND)
490 {
491 tree op0, op1;
492 enum tree_code cond_code;
493
494 op0 = gimple_cond_lhs (stmt);
495 op1 = gimple_cond_rhs (stmt);
496 cond_code = gimple_cond_code (stmt);
497
498 /* Get the current value of both operands. */
499 if (TREE_CODE (op0) == SSA_NAME)
500 {
501 for (int i = 0; i < 2; i++)
502 {
503 if (TREE_CODE (op0) == SSA_NAME
504 && SSA_NAME_VALUE (op0))
505 op0 = SSA_NAME_VALUE (op0);
506 else
507 break;
508 }
509 }
510
511 if (TREE_CODE (op1) == SSA_NAME)
512 {
513 for (int i = 0; i < 2; i++)
514 {
515 if (TREE_CODE (op1) == SSA_NAME
516 && SSA_NAME_VALUE (op1))
517 op1 = SSA_NAME_VALUE (op1);
518 else
519 break;
520 }
521 }
522
523 if (handle_dominating_asserts)
524 {
525 /* Now see if the operand was consumed by an ASSERT_EXPR
526 which dominates E->src. If so, we want to replace the
527 operand with the LHS of the ASSERT_EXPR. */
528 if (TREE_CODE (op0) == SSA_NAME)
529 op0 = lhs_of_dominating_assert (op0, e->src, stmt);
530
531 if (TREE_CODE (op1) == SSA_NAME)
532 op1 = lhs_of_dominating_assert (op1, e->src, stmt);
533 }
534
535 /* We may need to canonicalize the comparison. For
536 example, op0 might be a constant while op1 is an
537 SSA_NAME. Failure to canonicalize will cause us to
538 miss threading opportunities. */
539 if (tree_swap_operands_p (op0, op1, false))
540 {
541 cond_code = swap_tree_comparison (cond_code);
542 std::swap (op0, op1);
543 }
544
545 /* Stuff the operator and operands into our dummy conditional
546 expression. */
547 gimple_cond_set_code (dummy_cond, cond_code);
548 gimple_cond_set_lhs (dummy_cond, op0);
549 gimple_cond_set_rhs (dummy_cond, op1);
550
551 /* We absolutely do not care about any type conversions
552 we only care about a zero/nonzero value. */
553 fold_defer_overflow_warnings ();
554
555 cached_lhs = fold_binary (cond_code, boolean_type_node, op0, op1);
556 if (cached_lhs)
557 while (CONVERT_EXPR_P (cached_lhs))
558 cached_lhs = TREE_OPERAND (cached_lhs, 0);
559
560 fold_undefer_overflow_warnings ((cached_lhs
561 && is_gimple_min_invariant (cached_lhs)),
562 stmt, WARN_STRICT_OVERFLOW_CONDITIONAL);
563
564 /* If we have not simplified the condition down to an invariant,
565 then use the pass specific callback to simplify the condition. */
566 if (!cached_lhs
567 || !is_gimple_min_invariant (cached_lhs))
568 cached_lhs = (*simplify) (dummy_cond, stmt);
569
570 return cached_lhs;
571 }
572
573 if (code == GIMPLE_SWITCH)
574 cond = gimple_switch_index (as_a <gswitch *> (stmt));
575 else if (code == GIMPLE_GOTO)
576 cond = gimple_goto_dest (stmt);
577 else
578 gcc_unreachable ();
579
580 /* We can have conditionals which just test the state of a variable
581 rather than use a relational operator. These are simpler to handle. */
582 if (TREE_CODE (cond) == SSA_NAME)
583 {
584 tree original_lhs = cond;
585 cached_lhs = cond;
586
587 /* Get the variable's current value from the equivalence chains.
588
589 It is possible to get loops in the SSA_NAME_VALUE chains
590 (consider threading the backedge of a loop where we have
591 a loop invariant SSA_NAME used in the condition. */
592 if (cached_lhs)
593 {
594 for (int i = 0; i < 2; i++)
595 {
596 if (TREE_CODE (cached_lhs) == SSA_NAME
597 && SSA_NAME_VALUE (cached_lhs))
598 cached_lhs = SSA_NAME_VALUE (cached_lhs);
599 else
600 break;
601 }
602 }
603
604 /* If we're dominated by a suitable ASSERT_EXPR, then
605 update CACHED_LHS appropriately. */
606 if (handle_dominating_asserts && TREE_CODE (cached_lhs) == SSA_NAME)
607 cached_lhs = lhs_of_dominating_assert (cached_lhs, e->src, stmt);
608
609 /* If we haven't simplified to an invariant yet, then use the
610 pass specific callback to try and simplify it further. */
611 if (cached_lhs && ! is_gimple_min_invariant (cached_lhs))
612 cached_lhs = (*simplify) (stmt, stmt);
613
614 /* We couldn't find an invariant. But, callers of this
615 function may be able to do something useful with the
616 unmodified destination. */
617 if (!cached_lhs)
618 cached_lhs = original_lhs;
619 }
620 else
621 cached_lhs = NULL;
622
623 return cached_lhs;
624 }
625
626 /* Copy debug stmts from DEST's chain of single predecessors up to
627 SRC, so that we don't lose the bindings as PHI nodes are introduced
628 when DEST gains new predecessors. */
629 void
630 propagate_threaded_block_debug_into (basic_block dest, basic_block src)
631 {
632 if (!MAY_HAVE_DEBUG_STMTS)
633 return;
634
635 if (!single_pred_p (dest))
636 return;
637
638 gcc_checking_assert (dest != src);
639
640 gimple_stmt_iterator gsi = gsi_after_labels (dest);
641 int i = 0;
642 const int alloc_count = 16; // ?? Should this be a PARAM?
643
644 /* Estimate the number of debug vars overridden in the beginning of
645 DEST, to tell how many we're going to need to begin with. */
646 for (gimple_stmt_iterator si = gsi;
647 i * 4 <= alloc_count * 3 && !gsi_end_p (si); gsi_next (&si))
648 {
649 gimple stmt = gsi_stmt (si);
650 if (!is_gimple_debug (stmt))
651 break;
652 i++;
653 }
654
655 auto_vec<tree, alloc_count> fewvars;
656 hash_set<tree> *vars = NULL;
657
658 /* If we're already starting with 3/4 of alloc_count, go for a
659 hash_set, otherwise start with an unordered stack-allocated
660 VEC. */
661 if (i * 4 > alloc_count * 3)
662 vars = new hash_set<tree>;
663
664 /* Now go through the initial debug stmts in DEST again, this time
665 actually inserting in VARS or FEWVARS. Don't bother checking for
666 duplicates in FEWVARS. */
667 for (gimple_stmt_iterator si = gsi; !gsi_end_p (si); gsi_next (&si))
668 {
669 gimple stmt = gsi_stmt (si);
670 if (!is_gimple_debug (stmt))
671 break;
672
673 tree var;
674
675 if (gimple_debug_bind_p (stmt))
676 var = gimple_debug_bind_get_var (stmt);
677 else if (gimple_debug_source_bind_p (stmt))
678 var = gimple_debug_source_bind_get_var (stmt);
679 else
680 gcc_unreachable ();
681
682 if (vars)
683 vars->add (var);
684 else
685 fewvars.quick_push (var);
686 }
687
688 basic_block bb = dest;
689
690 do
691 {
692 bb = single_pred (bb);
693 for (gimple_stmt_iterator si = gsi_last_bb (bb);
694 !gsi_end_p (si); gsi_prev (&si))
695 {
696 gimple stmt = gsi_stmt (si);
697 if (!is_gimple_debug (stmt))
698 continue;
699
700 tree var;
701
702 if (gimple_debug_bind_p (stmt))
703 var = gimple_debug_bind_get_var (stmt);
704 else if (gimple_debug_source_bind_p (stmt))
705 var = gimple_debug_source_bind_get_var (stmt);
706 else
707 gcc_unreachable ();
708
709 /* Discard debug bind overlaps. ??? Unlike stmts from src,
710 copied into a new block that will precede BB, debug bind
711 stmts in bypassed BBs may actually be discarded if
712 they're overwritten by subsequent debug bind stmts, which
713 might be a problem once we introduce stmt frontier notes
714 or somesuch. Adding `&& bb == src' to the condition
715 below will preserve all potentially relevant debug
716 notes. */
717 if (vars && vars->add (var))
718 continue;
719 else if (!vars)
720 {
721 int i = fewvars.length ();
722 while (i--)
723 if (fewvars[i] == var)
724 break;
725 if (i >= 0)
726 continue;
727
728 if (fewvars.length () < (unsigned) alloc_count)
729 fewvars.quick_push (var);
730 else
731 {
732 vars = new hash_set<tree>;
733 for (i = 0; i < alloc_count; i++)
734 vars->add (fewvars[i]);
735 fewvars.release ();
736 vars->add (var);
737 }
738 }
739
740 stmt = gimple_copy (stmt);
741 /* ??? Should we drop the location of the copy to denote
742 they're artificial bindings? */
743 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
744 }
745 }
746 while (bb != src && single_pred_p (bb));
747
748 if (vars)
749 delete vars;
750 else if (fewvars.exists ())
751 fewvars.release ();
752 }
753
754 /* See if TAKEN_EDGE->dest is a threadable block with no side effecs (ie, it
755 need not be duplicated as part of the CFG/SSA updating process).
756
757 If it is threadable, add it to PATH and VISITED and recurse, ultimately
758 returning TRUE from the toplevel call. Otherwise do nothing and
759 return false.
760
761 DUMMY_COND, HANDLE_DOMINATING_ASSERTS and SIMPLIFY are used to
762 try and simplify the condition at the end of TAKEN_EDGE->dest. */
763 static bool
764 thread_around_empty_blocks (edge taken_edge,
765 gcond *dummy_cond,
766 bool handle_dominating_asserts,
767 tree (*simplify) (gimple, gimple),
768 bitmap visited,
769 vec<jump_thread_edge *> *path,
770 bool *backedge_seen_p)
771 {
772 basic_block bb = taken_edge->dest;
773 gimple_stmt_iterator gsi;
774 gimple stmt;
775 tree cond;
776
777 /* The key property of these blocks is that they need not be duplicated
778 when threading. Thus they can not have visible side effects such
779 as PHI nodes. */
780 if (!gsi_end_p (gsi_start_phis (bb)))
781 return false;
782
783 /* Skip over DEBUG statements at the start of the block. */
784 gsi = gsi_start_nondebug_bb (bb);
785
786 /* If the block has no statements, but does have a single successor, then
787 it's just a forwarding block and we can thread through it trivially.
788
789 However, note that just threading through empty blocks with single
790 successors is not inherently profitable. For the jump thread to
791 be profitable, we must avoid a runtime conditional.
792
793 By taking the return value from the recursive call, we get the
794 desired effect of returning TRUE when we found a profitable jump
795 threading opportunity and FALSE otherwise.
796
797 This is particularly important when this routine is called after
798 processing a joiner block. Returning TRUE too aggressively in
799 that case results in pointless duplication of the joiner block. */
800 if (gsi_end_p (gsi))
801 {
802 if (single_succ_p (bb))
803 {
804 taken_edge = single_succ_edge (bb);
805 if (!bitmap_bit_p (visited, taken_edge->dest->index))
806 {
807 jump_thread_edge *x
808 = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
809 path->safe_push (x);
810 bitmap_set_bit (visited, taken_edge->dest->index);
811 *backedge_seen_p |= ((taken_edge->flags & EDGE_DFS_BACK) != 0);
812 if (*backedge_seen_p)
813 simplify = dummy_simplify;
814 return thread_around_empty_blocks (taken_edge,
815 dummy_cond,
816 handle_dominating_asserts,
817 simplify,
818 visited,
819 path,
820 backedge_seen_p);
821 }
822 }
823
824 /* We have a block with no statements, but multiple successors? */
825 return false;
826 }
827
828 /* The only real statements this block can have are a control
829 flow altering statement. Anything else stops the thread. */
830 stmt = gsi_stmt (gsi);
831 if (gimple_code (stmt) != GIMPLE_COND
832 && gimple_code (stmt) != GIMPLE_GOTO
833 && gimple_code (stmt) != GIMPLE_SWITCH)
834 return false;
835
836 /* If we have traversed a backedge, then we do not want to look
837 at certain expressions in the table that can not be relied upon.
838 Luckily the only code that looked at those expressions is the
839 SIMPLIFY callback, which we replace if we can no longer use it. */
840 if (*backedge_seen_p)
841 simplify = dummy_simplify;
842
843 /* Extract and simplify the condition. */
844 cond = simplify_control_stmt_condition (taken_edge, stmt, dummy_cond,
845 simplify, handle_dominating_asserts);
846
847 /* If the condition can be statically computed and we have not already
848 visited the destination edge, then add the taken edge to our thread
849 path. */
850 if (cond && is_gimple_min_invariant (cond))
851 {
852 taken_edge = find_taken_edge (bb, cond);
853
854 if (bitmap_bit_p (visited, taken_edge->dest->index))
855 return false;
856 bitmap_set_bit (visited, taken_edge->dest->index);
857
858 jump_thread_edge *x
859 = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
860 path->safe_push (x);
861 *backedge_seen_p |= ((taken_edge->flags & EDGE_DFS_BACK) != 0);
862 if (*backedge_seen_p)
863 simplify = dummy_simplify;
864
865 thread_around_empty_blocks (taken_edge,
866 dummy_cond,
867 handle_dominating_asserts,
868 simplify,
869 visited,
870 path,
871 backedge_seen_p);
872 return true;
873 }
874
875 return false;
876 }
877
878 /* Return true if the CFG contains at least one path from START_BB to END_BB.
879 When a path is found, record in PATH the blocks from END_BB to START_BB.
880 VISITED_BBS is used to make sure we don't fall into an infinite loop. Bound
881 the recursion to basic blocks belonging to LOOP. */
882
883 static bool
884 fsm_find_thread_path (basic_block start_bb, basic_block end_bb,
885 vec<basic_block, va_gc> *&path,
886 hash_set<basic_block> *visited_bbs, loop_p loop)
887 {
888 if (loop != start_bb->loop_father)
889 return false;
890
891 if (start_bb == end_bb)
892 {
893 vec_safe_push (path, start_bb);
894 return true;
895 }
896
897 if (!visited_bbs->add (start_bb))
898 {
899 edge e;
900 edge_iterator ei;
901 FOR_EACH_EDGE (e, ei, start_bb->succs)
902 if (fsm_find_thread_path (e->dest, end_bb, path, visited_bbs, loop))
903 {
904 vec_safe_push (path, start_bb);
905 return true;
906 }
907 }
908
909 return false;
910 }
911
912 static int max_threaded_paths;
913
914 /* We trace the value of the variable EXPR back through any phi nodes looking
915 for places where it gets a constant value and save the path. Stop after
916 having recorded MAX_PATHS jump threading paths. */
917
918 static void
919 fsm_find_control_statement_thread_paths (tree expr,
920 hash_set<basic_block> *visited_bbs,
921 vec<basic_block, va_gc> *&path,
922 bool seen_loop_phi)
923 {
924 tree var = SSA_NAME_VAR (expr);
925 gimple def_stmt = SSA_NAME_DEF_STMT (expr);
926 basic_block var_bb = gimple_bb (def_stmt);
927
928 if (var == NULL || var_bb == NULL)
929 return;
930
931 /* For the moment we assume that an SSA chain only contains phi nodes, and
932 eventually one of the phi arguments will be an integer constant. In the
933 future, this could be extended to also handle simple assignments of
934 arithmetic operations. */
935 if (gimple_code (def_stmt) != GIMPLE_PHI)
936 return;
937
938 /* Avoid infinite recursion. */
939 if (visited_bbs->add (var_bb))
940 return;
941
942 gphi *phi = as_a <gphi *> (def_stmt);
943 int next_path_length = 0;
944 basic_block last_bb_in_path = path->last ();
945
946 if (loop_containing_stmt (phi)->header == gimple_bb (phi))
947 {
948 /* Do not walk through more than one loop PHI node. */
949 if (seen_loop_phi)
950 return;
951 seen_loop_phi = true;
952 }
953
954 /* Following the chain of SSA_NAME definitions, we jumped from a definition in
955 LAST_BB_IN_PATH to a definition in VAR_BB. When these basic blocks are
956 different, append to PATH the blocks from LAST_BB_IN_PATH to VAR_BB. */
957 if (var_bb != last_bb_in_path)
958 {
959 edge e;
960 int e_count = 0;
961 edge_iterator ei;
962 vec<basic_block, va_gc> *next_path;
963 vec_alloc (next_path, n_basic_blocks_for_fn (cfun));
964
965 FOR_EACH_EDGE (e, ei, last_bb_in_path->preds)
966 {
967 hash_set<basic_block> *visited_bbs = new hash_set<basic_block>;
968
969 if (fsm_find_thread_path (var_bb, e->src, next_path, visited_bbs,
970 e->src->loop_father))
971 ++e_count;
972
973 delete visited_bbs;
974
975 /* If there is more than one path, stop. */
976 if (e_count > 1)
977 {
978 vec_free (next_path);
979 return;
980 }
981 }
982
983 /* Stop if we have not found a path: this could occur when the recursion
984 is stopped by one of the bounds. */
985 if (e_count == 0)
986 {
987 vec_free (next_path);
988 return;
989 }
990
991 /* Append all the nodes from NEXT_PATH to PATH. */
992 vec_safe_splice (path, next_path);
993 next_path_length = next_path->length ();
994 vec_free (next_path);
995 }
996
997 gcc_assert (path->last () == var_bb);
998
999 /* Iterate over the arguments of PHI. */
1000 unsigned int i;
1001 for (i = 0; i < gimple_phi_num_args (phi); i++)
1002 {
1003 tree arg = gimple_phi_arg_def (phi, i);
1004 basic_block bbi = gimple_phi_arg_edge (phi, i)->src;
1005
1006 /* Skip edges pointing outside the current loop. */
1007 if (!arg || var_bb->loop_father != bbi->loop_father)
1008 continue;
1009
1010 if (TREE_CODE (arg) == SSA_NAME)
1011 {
1012 vec_safe_push (path, bbi);
1013 /* Recursively follow SSA_NAMEs looking for a constant definition. */
1014 fsm_find_control_statement_thread_paths (arg, visited_bbs, path,
1015 seen_loop_phi);
1016
1017 path->pop ();
1018 continue;
1019 }
1020
1021 if (TREE_CODE (arg) != INTEGER_CST)
1022 continue;
1023
1024 int path_length = path->length ();
1025 /* A path with less than 2 basic blocks should not be jump-threaded. */
1026 if (path_length < 2)
1027 continue;
1028
1029 if (path_length > PARAM_VALUE (PARAM_MAX_FSM_THREAD_LENGTH))
1030 {
1031 if (dump_file && (dump_flags & TDF_DETAILS))
1032 fprintf (dump_file, "FSM jump-thread path not considered: "
1033 "the number of basic blocks on the path "
1034 "exceeds PARAM_MAX_FSM_THREAD_LENGTH.\n");
1035 continue;
1036 }
1037
1038 if (max_threaded_paths <= 0)
1039 {
1040 if (dump_file && (dump_flags & TDF_DETAILS))
1041 fprintf (dump_file, "FSM jump-thread path not considered: "
1042 "the number of previously recorded FSM paths to thread "
1043 "exceeds PARAM_MAX_FSM_THREAD_PATHS.\n");
1044 continue;
1045 }
1046
1047 /* Add BBI to the path. */
1048 vec_safe_push (path, bbi);
1049 ++path_length;
1050
1051 int n_insns = 0;
1052 gimple_stmt_iterator gsi;
1053 int j;
1054 loop_p loop = (*path)[0]->loop_father;
1055 bool path_crosses_loops = false;
1056
1057 /* Count the number of instructions on the path: as these instructions
1058 will have to be duplicated, we will not record the path if there are
1059 too many instructions on the path. Also check that all the blocks in
1060 the path belong to a single loop. */
1061 for (j = 1; j < path_length - 1; j++)
1062 {
1063 basic_block bb = (*path)[j];
1064
1065 if (bb->loop_father != loop)
1066 {
1067 path_crosses_loops = true;
1068 break;
1069 }
1070
1071 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1072 {
1073 gimple stmt = gsi_stmt (gsi);
1074 /* Do not count empty statements and labels. */
1075 if (gimple_code (stmt) != GIMPLE_NOP
1076 && gimple_code (stmt) != GIMPLE_LABEL
1077 && !is_gimple_debug (stmt))
1078 ++n_insns;
1079 }
1080 }
1081
1082 if (path_crosses_loops)
1083 {
1084 if (dump_file && (dump_flags & TDF_DETAILS))
1085 fprintf (dump_file, "FSM jump-thread path not considered: "
1086 "the path crosses loops.\n");
1087 path->pop ();
1088 continue;
1089 }
1090
1091 if (n_insns >= PARAM_VALUE (PARAM_MAX_FSM_THREAD_PATH_INSNS))
1092 {
1093 if (dump_file && (dump_flags & TDF_DETAILS))
1094 fprintf (dump_file, "FSM jump-thread path not considered: "
1095 "the number of instructions on the path "
1096 "exceeds PARAM_MAX_FSM_THREAD_PATH_INSNS.\n");
1097 path->pop ();
1098 continue;
1099 }
1100
1101 vec<jump_thread_edge *> *jump_thread_path
1102 = new vec<jump_thread_edge *> ();
1103
1104 /* Record the edges between the blocks in PATH. */
1105 for (j = 0; j < path_length - 1; j++)
1106 {
1107 edge e = find_edge ((*path)[path_length - j - 1],
1108 (*path)[path_length - j - 2]);
1109 gcc_assert (e);
1110 jump_thread_edge *x = new jump_thread_edge (e, EDGE_FSM_THREAD);
1111 jump_thread_path->safe_push (x);
1112 }
1113
1114 /* Add the edge taken when the control variable has value ARG. */
1115 edge taken_edge = find_taken_edge ((*path)[0], arg);
1116 jump_thread_edge *x
1117 = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
1118 jump_thread_path->safe_push (x);
1119
1120 register_jump_thread (jump_thread_path);
1121 --max_threaded_paths;
1122
1123 /* Remove BBI from the path. */
1124 path->pop ();
1125 }
1126
1127 /* Remove all the nodes that we added from NEXT_PATH. */
1128 if (next_path_length)
1129 vec_safe_truncate (path, (path->length () - next_path_length));
1130 }
1131
1132 /* We are exiting E->src, see if E->dest ends with a conditional
1133 jump which has a known value when reached via E.
1134
1135 E->dest can have arbitrary side effects which, if threading is
1136 successful, will be maintained.
1137
1138 Special care is necessary if E is a back edge in the CFG as we
1139 may have already recorded equivalences for E->dest into our
1140 various tables, including the result of the conditional at
1141 the end of E->dest. Threading opportunities are severely
1142 limited in that case to avoid short-circuiting the loop
1143 incorrectly.
1144
1145 DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
1146 to avoid allocating memory.
1147
1148 HANDLE_DOMINATING_ASSERTS is true if we should try to replace operands of
1149 the simplified condition with left-hand sides of ASSERT_EXPRs they are
1150 used in.
1151
1152 STACK is used to undo temporary equivalences created during the walk of
1153 E->dest.
1154
1155 SIMPLIFY is a pass-specific function used to simplify statements.
1156
1157 Our caller is responsible for restoring the state of the expression
1158 and const_and_copies stacks.
1159
1160 Positive return value is success. Zero return value is failure, but
1161 the block can still be duplicated as a joiner in a jump thread path,
1162 negative indicates the block should not be duplicated and thus is not
1163 suitable for a joiner in a jump threading path. */
1164
1165 static int
1166 thread_through_normal_block (edge e,
1167 gcond *dummy_cond,
1168 bool handle_dominating_asserts,
1169 const_and_copies *const_and_copies,
1170 tree (*simplify) (gimple, gimple),
1171 vec<jump_thread_edge *> *path,
1172 bitmap visited,
1173 bool *backedge_seen_p)
1174 {
1175 /* If we have traversed a backedge, then we do not want to look
1176 at certain expressions in the table that can not be relied upon.
1177 Luckily the only code that looked at those expressions is the
1178 SIMPLIFY callback, which we replace if we can no longer use it. */
1179 if (*backedge_seen_p)
1180 simplify = dummy_simplify;
1181
1182 /* PHIs create temporary equivalences.
1183 Note that if we found a PHI that made the block non-threadable, then
1184 we need to bubble that up to our caller in the same manner we do
1185 when we prematurely stop processing statements below. */
1186 if (!record_temporary_equivalences_from_phis (e, const_and_copies))
1187 return -1;
1188
1189 /* Now walk each statement recording any context sensitive
1190 temporary equivalences we can detect. */
1191 gimple stmt
1192 = record_temporary_equivalences_from_stmts_at_dest (e, const_and_copies, simplify,
1193 *backedge_seen_p);
1194
1195 /* There's two reasons STMT might be null, and distinguishing
1196 between them is important.
1197
1198 First the block may not have had any statements. For example, it
1199 might have some PHIs and unconditionally transfer control elsewhere.
1200 Such blocks are suitable for jump threading, particularly as a
1201 joiner block.
1202
1203 The second reason would be if we did not process all the statements
1204 in the block (because there were too many to make duplicating the
1205 block profitable. If we did not look at all the statements, then
1206 we may not have invalidated everything needing invalidation. Thus
1207 we must signal to our caller that this block is not suitable for
1208 use as a joiner in a threading path. */
1209 if (!stmt)
1210 {
1211 /* First case. The statement simply doesn't have any instructions, but
1212 does have PHIs. */
1213 if (gsi_end_p (gsi_start_nondebug_bb (e->dest))
1214 && !gsi_end_p (gsi_start_phis (e->dest)))
1215 return 0;
1216
1217 /* Second case. */
1218 return -1;
1219 }
1220
1221 /* If we stopped at a COND_EXPR or SWITCH_EXPR, see if we know which arm
1222 will be taken. */
1223 if (gimple_code (stmt) == GIMPLE_COND
1224 || gimple_code (stmt) == GIMPLE_GOTO
1225 || gimple_code (stmt) == GIMPLE_SWITCH)
1226 {
1227 tree cond;
1228
1229 /* Extract and simplify the condition. */
1230 cond = simplify_control_stmt_condition (e, stmt, dummy_cond, simplify,
1231 handle_dominating_asserts);
1232
1233 if (!cond)
1234 return 0;
1235
1236 if (is_gimple_min_invariant (cond))
1237 {
1238 edge taken_edge = find_taken_edge (e->dest, cond);
1239 basic_block dest = (taken_edge ? taken_edge->dest : NULL);
1240
1241 /* DEST could be NULL for a computed jump to an absolute
1242 address. */
1243 if (dest == NULL
1244 || dest == e->dest
1245 || bitmap_bit_p (visited, dest->index))
1246 return 0;
1247
1248 /* Only push the EDGE_START_JUMP_THREAD marker if this is
1249 first edge on the path. */
1250 if (path->length () == 0)
1251 {
1252 jump_thread_edge *x
1253 = new jump_thread_edge (e, EDGE_START_JUMP_THREAD);
1254 path->safe_push (x);
1255 *backedge_seen_p |= ((e->flags & EDGE_DFS_BACK) != 0);
1256 }
1257
1258 jump_thread_edge *x
1259 = new jump_thread_edge (taken_edge, EDGE_COPY_SRC_BLOCK);
1260 path->safe_push (x);
1261 *backedge_seen_p |= ((taken_edge->flags & EDGE_DFS_BACK) != 0);
1262 if (*backedge_seen_p)
1263 simplify = dummy_simplify;
1264
1265 /* See if we can thread through DEST as well, this helps capture
1266 secondary effects of threading without having to re-run DOM or
1267 VRP.
1268
1269 We don't want to thread back to a block we have already
1270 visited. This may be overly conservative. */
1271 bitmap_set_bit (visited, dest->index);
1272 bitmap_set_bit (visited, e->dest->index);
1273 thread_around_empty_blocks (taken_edge,
1274 dummy_cond,
1275 handle_dominating_asserts,
1276 simplify,
1277 visited,
1278 path,
1279 backedge_seen_p);
1280 return 1;
1281 }
1282
1283 if (!flag_expensive_optimizations
1284 || optimize_function_for_size_p (cfun)
1285 || TREE_CODE (cond) != SSA_NAME
1286 || e->dest->loop_father != e->src->loop_father
1287 || loop_depth (e->dest->loop_father) == 0)
1288 return 0;
1289
1290 /* When COND cannot be simplified, try to find paths from a control
1291 statement back through the PHI nodes which would affect that control
1292 statement. */
1293 vec<basic_block, va_gc> *bb_path;
1294 vec_alloc (bb_path, n_basic_blocks_for_fn (cfun));
1295 vec_safe_push (bb_path, e->dest);
1296 hash_set<basic_block> *visited_bbs = new hash_set<basic_block>;
1297
1298 max_threaded_paths = PARAM_VALUE (PARAM_MAX_FSM_THREAD_PATHS);
1299 fsm_find_control_statement_thread_paths (cond, visited_bbs, bb_path,
1300 false);
1301
1302 delete visited_bbs;
1303 vec_free (bb_path);
1304 }
1305 return 0;
1306 }
1307
1308 /* We are exiting E->src, see if E->dest ends with a conditional
1309 jump which has a known value when reached via E.
1310
1311 Special care is necessary if E is a back edge in the CFG as we
1312 may have already recorded equivalences for E->dest into our
1313 various tables, including the result of the conditional at
1314 the end of E->dest. Threading opportunities are severely
1315 limited in that case to avoid short-circuiting the loop
1316 incorrectly.
1317
1318 Note it is quite common for the first block inside a loop to
1319 end with a conditional which is either always true or always
1320 false when reached via the loop backedge. Thus we do not want
1321 to blindly disable threading across a loop backedge.
1322
1323 DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
1324 to avoid allocating memory.
1325
1326 HANDLE_DOMINATING_ASSERTS is true if we should try to replace operands of
1327 the simplified condition with left-hand sides of ASSERT_EXPRs they are
1328 used in.
1329
1330 STACK is used to undo temporary equivalences created during the walk of
1331 E->dest.
1332
1333 SIMPLIFY is a pass-specific function used to simplify statements. */
1334
1335 void
1336 thread_across_edge (gcond *dummy_cond,
1337 edge e,
1338 bool handle_dominating_asserts,
1339 const_and_copies *const_and_copies,
1340 tree (*simplify) (gimple, gimple))
1341 {
1342 bitmap visited = BITMAP_ALLOC (NULL);
1343 bool backedge_seen;
1344
1345 stmt_count = 0;
1346
1347 vec<jump_thread_edge *> *path = new vec<jump_thread_edge *> ();
1348 bitmap_clear (visited);
1349 bitmap_set_bit (visited, e->src->index);
1350 bitmap_set_bit (visited, e->dest->index);
1351 backedge_seen = ((e->flags & EDGE_DFS_BACK) != 0);
1352 if (backedge_seen)
1353 simplify = dummy_simplify;
1354
1355 int threaded = thread_through_normal_block (e, dummy_cond,
1356 handle_dominating_asserts,
1357 const_and_copies, simplify, path,
1358 visited, &backedge_seen);
1359 if (threaded > 0)
1360 {
1361 propagate_threaded_block_debug_into (path->last ()->e->dest,
1362 e->dest);
1363 const_and_copies->pop_to_marker ();
1364 BITMAP_FREE (visited);
1365 register_jump_thread (path);
1366 return;
1367 }
1368 else
1369 {
1370 /* Negative and zero return values indicate no threading was possible,
1371 thus there should be no edges on the thread path and no need to walk
1372 through the vector entries. */
1373 gcc_assert (path->length () == 0);
1374 path->release ();
1375 delete path;
1376
1377 /* A negative status indicates the target block was deemed too big to
1378 duplicate. Just quit now rather than trying to use the block as
1379 a joiner in a jump threading path.
1380
1381 This prevents unnecessary code growth, but more importantly if we
1382 do not look at all the statements in the block, then we may have
1383 missed some invalidations if we had traversed a backedge! */
1384 if (threaded < 0)
1385 {
1386 BITMAP_FREE (visited);
1387 const_and_copies->pop_to_marker ();
1388 return;
1389 }
1390 }
1391
1392 /* We were unable to determine what out edge from E->dest is taken. However,
1393 we might still be able to thread through successors of E->dest. This
1394 often occurs when E->dest is a joiner block which then fans back out
1395 based on redundant tests.
1396
1397 If so, we'll copy E->dest and redirect the appropriate predecessor to
1398 the copy. Within the copy of E->dest, we'll thread one or more edges
1399 to points deeper in the CFG.
1400
1401 This is a stopgap until we have a more structured approach to path
1402 isolation. */
1403 {
1404 edge taken_edge;
1405 edge_iterator ei;
1406 bool found;
1407
1408 /* If E->dest has abnormal outgoing edges, then there's no guarantee
1409 we can safely redirect any of the edges. Just punt those cases. */
1410 FOR_EACH_EDGE (taken_edge, ei, e->dest->succs)
1411 if (taken_edge->flags & EDGE_ABNORMAL)
1412 {
1413 const_and_copies->pop_to_marker ();
1414 BITMAP_FREE (visited);
1415 return;
1416 }
1417
1418 /* Look at each successor of E->dest to see if we can thread through it. */
1419 FOR_EACH_EDGE (taken_edge, ei, e->dest->succs)
1420 {
1421 /* Push a fresh marker so we can unwind the equivalences created
1422 for each of E->dest's successors. */
1423 const_and_copies->push_marker ();
1424
1425 /* Avoid threading to any block we have already visited. */
1426 bitmap_clear (visited);
1427 bitmap_set_bit (visited, e->src->index);
1428 bitmap_set_bit (visited, e->dest->index);
1429 bitmap_set_bit (visited, taken_edge->dest->index);
1430 vec<jump_thread_edge *> *path = new vec<jump_thread_edge *> ();
1431
1432 /* Record whether or not we were able to thread through a successor
1433 of E->dest. */
1434 jump_thread_edge *x = new jump_thread_edge (e, EDGE_START_JUMP_THREAD);
1435 path->safe_push (x);
1436
1437 x = new jump_thread_edge (taken_edge, EDGE_COPY_SRC_JOINER_BLOCK);
1438 path->safe_push (x);
1439 found = false;
1440 backedge_seen = ((e->flags & EDGE_DFS_BACK) != 0);
1441 backedge_seen |= ((taken_edge->flags & EDGE_DFS_BACK) != 0);
1442 if (backedge_seen)
1443 simplify = dummy_simplify;
1444 found = thread_around_empty_blocks (taken_edge,
1445 dummy_cond,
1446 handle_dominating_asserts,
1447 simplify,
1448 visited,
1449 path,
1450 &backedge_seen);
1451
1452 if (backedge_seen)
1453 simplify = dummy_simplify;
1454
1455 if (!found)
1456 found = thread_through_normal_block (path->last ()->e, dummy_cond,
1457 handle_dominating_asserts,
1458 const_and_copies, simplify, path, visited,
1459 &backedge_seen) > 0;
1460
1461 /* If we were able to thread through a successor of E->dest, then
1462 record the jump threading opportunity. */
1463 if (found)
1464 {
1465 propagate_threaded_block_debug_into (path->last ()->e->dest,
1466 taken_edge->dest);
1467 register_jump_thread (path);
1468 }
1469 else
1470 {
1471 delete_jump_thread_path (path);
1472 }
1473
1474 /* And unwind the equivalence table. */
1475 const_and_copies->pop_to_marker ();
1476 }
1477 BITMAP_FREE (visited);
1478 }
1479
1480 const_and_copies->pop_to_marker ();
1481 }