2 Copyright (C) 2005-2020 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)
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/>. */
23 #include "coretypes.h"
29 #include "fold-const.h"
31 #include "gimple-iterator.h"
33 #include "tree-ssa-threadupdate.h"
34 #include "tree-ssa-scopedtables.h"
35 #include "tree-ssa-threadedge.h"
36 #include "tree-ssa-dom.h"
37 #include "gimple-fold.h"
39 #include "alloc-pool.h"
40 #include "vr-values.h"
41 #include "gimple-ssa-evrp-analyze.h"
43 /* To avoid code explosion due to jump threading, we limit the
44 number of statements we are going to copy. This variable
45 holds the number of statements currently seen that we'll have
46 to copy as part of the jump threading process. */
47 static int stmt_count
;
49 /* Array to record value-handles per SSA_NAME. */
50 vec
<tree
> ssa_name_values
;
52 typedef tree (pfn_simplify
) (gimple
*, gimple
*,
53 class avail_exprs_stack
*,
56 /* Set the value for the SSA name NAME to VALUE. */
59 set_ssa_name_value (tree name
, tree value
)
61 if (SSA_NAME_VERSION (name
) >= ssa_name_values
.length ())
62 ssa_name_values
.safe_grow_cleared (SSA_NAME_VERSION (name
) + 1, true);
63 if (value
&& TREE_OVERFLOW_P (value
))
64 value
= drop_tree_overflow (value
);
65 ssa_name_values
[SSA_NAME_VERSION (name
)] = value
;
68 /* Initialize the per SSA_NAME value-handles array. Returns it. */
70 threadedge_initialize_values (void)
72 gcc_assert (!ssa_name_values
.exists ());
73 ssa_name_values
.create (num_ssa_names
);
76 /* Free the per SSA_NAME value-handle array. */
78 threadedge_finalize_values (void)
80 ssa_name_values
.release ();
83 /* Return TRUE if we may be able to thread an incoming edge into
84 BB to an outgoing edge from BB. Return FALSE otherwise. */
87 potentially_threadable_block (basic_block bb
)
89 gimple_stmt_iterator gsi
;
91 /* Special case. We can get blocks that are forwarders, but are
92 not optimized away because they forward from outside a loop
93 to the loop header. We want to thread through them as we can
94 sometimes thread to the loop exit, which is obviously profitable.
95 the interesting case here is when the block has PHIs. */
96 if (gsi_end_p (gsi_start_nondebug_bb (bb
))
97 && !gsi_end_p (gsi_start_phis (bb
)))
100 /* If BB has a single successor or a single predecessor, then
101 there is no threading opportunity. */
102 if (single_succ_p (bb
) || single_pred_p (bb
))
105 /* If BB does not end with a conditional, switch or computed goto,
106 then there is no threading opportunity. */
107 gsi
= gsi_last_bb (bb
);
110 || (gimple_code (gsi_stmt (gsi
)) != GIMPLE_COND
111 && gimple_code (gsi_stmt (gsi
)) != GIMPLE_GOTO
112 && gimple_code (gsi_stmt (gsi
)) != GIMPLE_SWITCH
))
118 /* Record temporary equivalences created by PHIs at the target of the
119 edge E. Record unwind information for the equivalences into
120 CONST_AND_COPIES and EVRP_RANGE_DATA.
122 If a PHI which prevents threading is encountered, then return FALSE
123 indicating we should not thread this edge, else return TRUE. */
126 record_temporary_equivalences_from_phis (edge e
,
127 const_and_copies
*const_and_copies
,
128 evrp_range_analyzer
*evrp_range_analyzer
)
132 /* Each PHI creates a temporary equivalence, record them.
133 These are context sensitive equivalences and will be removed
135 for (gsi
= gsi_start_phis (e
->dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
137 gphi
*phi
= gsi
.phi ();
138 tree src
= PHI_ARG_DEF_FROM_EDGE (phi
, e
);
139 tree dst
= gimple_phi_result (phi
);
141 /* If the desired argument is not the same as this PHI's result
142 and it is set by a PHI in E->dest, then we cannot thread
145 && TREE_CODE (src
) == SSA_NAME
146 && gimple_code (SSA_NAME_DEF_STMT (src
)) == GIMPLE_PHI
147 && gimple_bb (SSA_NAME_DEF_STMT (src
)) == e
->dest
)
150 /* We consider any non-virtual PHI as a statement since it
151 count result in a constant assignment or copy operation. */
152 if (!virtual_operand_p (dst
))
155 const_and_copies
->record_const_or_copy (dst
, src
);
157 /* Also update the value range associated with DST, using
160 Note that even if SRC is a constant we need to set a suitable
161 output range so that VR_UNDEFINED ranges do not leak through. */
162 if (evrp_range_analyzer
)
164 /* Get an empty new VR we can pass to update_value_range and save
165 away in the VR stack. */
166 value_range_equiv
*new_vr
167 = evrp_range_analyzer
->allocate_value_range_equiv ();
168 new (new_vr
) value_range_equiv ();
170 /* There are three cases to consider:
172 First if SRC is an SSA_NAME, then we can copy the value
173 range from SRC into NEW_VR.
175 Second if SRC is an INTEGER_CST, then we can just wet
176 NEW_VR to a singleton range.
178 Otherwise set NEW_VR to varying. This may be overly
180 if (TREE_CODE (src
) == SSA_NAME
)
181 new_vr
->deep_copy (evrp_range_analyzer
->get_value_range (src
));
182 else if (TREE_CODE (src
) == INTEGER_CST
)
185 new_vr
->set_varying (TREE_TYPE (src
));
187 /* This is a temporary range for DST, so push it. */
188 evrp_range_analyzer
->push_value_range (dst
, new_vr
);
194 /* Valueize hook for gimple_fold_stmt_to_constant_1. */
197 threadedge_valueize (tree t
)
199 if (TREE_CODE (t
) == SSA_NAME
)
201 tree tem
= SSA_NAME_VALUE (t
);
208 /* Try to simplify each statement in E->dest, ultimately leading to
209 a simplification of the COND_EXPR at the end of E->dest.
211 Record unwind information for temporary equivalences onto STACK.
213 Use SIMPLIFY (a pointer to a callback function) to further simplify
214 statements using pass specific information.
216 We might consider marking just those statements which ultimately
217 feed the COND_EXPR. It's not clear if the overhead of bookkeeping
218 would be recovered by trying to simplify fewer statements.
220 If we are able to simplify a statement into the form
221 SSA_NAME = (SSA_NAME | gimple invariant), then we can record
222 a context sensitive equivalence which may help us simplify
223 later statements in E->dest. */
226 record_temporary_equivalences_from_stmts_at_dest (edge e
,
227 const_and_copies
*const_and_copies
,
228 avail_exprs_stack
*avail_exprs_stack
,
229 evrp_range_analyzer
*evrp_range_analyzer
,
230 pfn_simplify simplify
)
233 gimple_stmt_iterator gsi
;
236 max_stmt_count
= param_max_jump_thread_duplication_stmts
;
238 /* Walk through each statement in the block recording equivalences
239 we discover. Note any equivalences we discover are context
240 sensitive (ie, are dependent on traversing E) and must be unwound
241 when we're finished processing E. */
242 for (gsi
= gsi_start_bb (e
->dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
244 tree cached_lhs
= NULL
;
246 stmt
= gsi_stmt (gsi
);
248 /* Ignore empty statements and labels. */
249 if (gimple_code (stmt
) == GIMPLE_NOP
250 || gimple_code (stmt
) == GIMPLE_LABEL
251 || is_gimple_debug (stmt
))
254 /* If the statement has volatile operands, then we assume we
255 cannot thread through this block. This is overly
256 conservative in some ways. */
257 if (gimple_code (stmt
) == GIMPLE_ASM
258 && gimple_asm_volatile_p (as_a
<gasm
*> (stmt
)))
261 /* If the statement is a unique builtin, we cannot thread
263 if (gimple_code (stmt
) == GIMPLE_CALL
264 && gimple_call_internal_p (stmt
)
265 && gimple_call_internal_unique_p (stmt
))
268 /* If duplicating this block is going to cause too much code
269 expansion, then do not thread through this block. */
271 if (stmt_count
> max_stmt_count
)
273 /* If any of the stmts in the PATH's dests are going to be
274 killed due to threading, grow the max count
277 == param_max_jump_thread_duplication_stmts
)
279 max_stmt_count
+= estimate_threading_killed_stmts (e
->dest
);
281 fprintf (dump_file
, "threading bb %i up to %i stmts\n",
282 e
->dest
->index
, max_stmt_count
);
284 /* If we're still past the limit, we're done. */
285 if (stmt_count
> max_stmt_count
)
289 /* These are temporary ranges, do nto reflect them back into
290 the global range data. */
291 if (evrp_range_analyzer
)
292 evrp_range_analyzer
->record_ranges_from_stmt (stmt
, true);
294 /* If this is not a statement that sets an SSA_NAME to a new
295 value, then do not try to simplify this statement as it will
296 not simplify in any way that is helpful for jump threading. */
297 if ((gimple_code (stmt
) != GIMPLE_ASSIGN
298 || TREE_CODE (gimple_assign_lhs (stmt
)) != SSA_NAME
)
299 && (gimple_code (stmt
) != GIMPLE_CALL
300 || gimple_call_lhs (stmt
) == NULL_TREE
301 || TREE_CODE (gimple_call_lhs (stmt
)) != SSA_NAME
))
304 /* The result of __builtin_object_size depends on all the arguments
305 of a phi node. Temporarily using only one edge produces invalid
314 r = PHI <&w[2].a[1](2), &a.a[6](3)>
315 __builtin_object_size (r, 0)
317 The result of __builtin_object_size is defined to be the maximum of
318 remaining bytes. If we use only one edge on the phi, the result will
319 change to be the remaining bytes for the corresponding phi argument.
321 Similarly for __builtin_constant_p:
324 __builtin_constant_p (r)
326 Both PHI arguments are constant, but x ? 1 : 2 is still not
329 if (is_gimple_call (stmt
))
331 tree fndecl
= gimple_call_fndecl (stmt
);
333 && fndecl_built_in_p (fndecl
, BUILT_IN_NORMAL
)
334 && (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_OBJECT_SIZE
335 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CONSTANT_P
))
339 /* At this point we have a statement which assigns an RHS to an
340 SSA_VAR on the LHS. We want to try and simplify this statement
341 to expose more context sensitive equivalences which in turn may
342 allow us to simplify the condition at the end of the loop.
344 Handle simple copy operations as well as implied copies from
346 if (gimple_assign_single_p (stmt
)
347 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
)
348 cached_lhs
= gimple_assign_rhs1 (stmt
);
349 else if (gimple_assign_single_p (stmt
)
350 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ASSERT_EXPR
)
351 cached_lhs
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
354 /* A statement that is not a trivial copy or ASSERT_EXPR.
355 Try to fold the new expression. Inserting the
356 expression into the hash table is unlikely to help. */
357 /* ??? The DOM callback below can be changed to setting
358 the mprts_hook around the call to thread_across_edge,
359 avoiding the use substitution. The VRP hook should be
360 changed to properly valueize operands itself using
361 SSA_NAME_VALUE in addition to its own lattice. */
362 cached_lhs
= gimple_fold_stmt_to_constant_1 (stmt
,
363 threadedge_valueize
);
364 if (NUM_SSA_OPERANDS (stmt
, SSA_OP_ALL_USES
) != 0
366 || (TREE_CODE (cached_lhs
) != SSA_NAME
367 && !is_gimple_min_invariant (cached_lhs
))))
369 /* We're going to temporarily copy propagate the operands
370 and see if that allows us to simplify this statement. */
374 unsigned int num
, i
= 0;
376 num
= NUM_SSA_OPERANDS (stmt
, SSA_OP_ALL_USES
);
377 copy
= XALLOCAVEC (tree
, num
);
379 /* Make a copy of the uses & vuses into USES_COPY, then cprop into
381 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_ALL_USES
)
384 tree use
= USE_FROM_PTR (use_p
);
387 if (TREE_CODE (use
) == SSA_NAME
)
388 tmp
= SSA_NAME_VALUE (use
);
390 SET_USE (use_p
, tmp
);
393 cached_lhs
= (*simplify
) (stmt
, stmt
, avail_exprs_stack
, e
->src
);
395 /* Restore the statement's original uses/defs. */
397 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_ALL_USES
)
398 SET_USE (use_p
, copy
[i
++]);
402 /* Record the context sensitive equivalence if we were able
403 to simplify this statement. */
405 && (TREE_CODE (cached_lhs
) == SSA_NAME
406 || is_gimple_min_invariant (cached_lhs
)))
407 const_and_copies
->record_const_or_copy (gimple_get_lhs (stmt
),
413 static tree
simplify_control_stmt_condition_1 (edge
, gimple
*,
414 class avail_exprs_stack
*,
415 tree
, enum tree_code
, tree
,
416 gcond
*, pfn_simplify
,
419 /* Simplify the control statement at the end of the block E->dest.
421 To avoid allocating memory unnecessarily, a scratch GIMPLE_COND
422 is available to use/clobber in DUMMY_COND.
424 Use SIMPLIFY (a pointer to a callback function) to further simplify
425 a condition using pass specific information.
427 Return the simplified condition or NULL if simplification could
428 not be performed. When simplifying a GIMPLE_SWITCH, we may return
429 the CASE_LABEL_EXPR that will be taken.
431 The available expression table is referenced via AVAIL_EXPRS_STACK. */
434 simplify_control_stmt_condition (edge e
,
436 class avail_exprs_stack
*avail_exprs_stack
,
438 pfn_simplify simplify
)
440 tree cond
, cached_lhs
;
441 enum gimple_code code
= gimple_code (stmt
);
443 /* For comparisons, we have to update both operands, then try
444 to simplify the comparison. */
445 if (code
== GIMPLE_COND
)
448 enum tree_code cond_code
;
450 op0
= gimple_cond_lhs (stmt
);
451 op1
= gimple_cond_rhs (stmt
);
452 cond_code
= gimple_cond_code (stmt
);
454 /* Get the current value of both operands. */
455 if (TREE_CODE (op0
) == SSA_NAME
)
457 for (int i
= 0; i
< 2; i
++)
459 if (TREE_CODE (op0
) == SSA_NAME
460 && SSA_NAME_VALUE (op0
))
461 op0
= SSA_NAME_VALUE (op0
);
467 if (TREE_CODE (op1
) == SSA_NAME
)
469 for (int i
= 0; i
< 2; i
++)
471 if (TREE_CODE (op1
) == SSA_NAME
472 && SSA_NAME_VALUE (op1
))
473 op1
= SSA_NAME_VALUE (op1
);
479 const unsigned recursion_limit
= 4;
482 = simplify_control_stmt_condition_1 (e
, stmt
, avail_exprs_stack
,
484 dummy_cond
, simplify
,
487 /* If we were testing an integer/pointer against a constant, then
488 we can use the FSM code to trace the value of the SSA_NAME. If
489 a value is found, then the condition will collapse to a constant.
491 Return the SSA_NAME we want to trace back rather than the full
492 expression and give the FSM threader a chance to find its value. */
493 if (cached_lhs
== NULL
)
495 /* Recover the original operands. They may have been simplified
496 using context sensitive equivalences. Those context sensitive
497 equivalences may not be valid on paths found by the FSM optimizer. */
498 tree op0
= gimple_cond_lhs (stmt
);
499 tree op1
= gimple_cond_rhs (stmt
);
501 if ((INTEGRAL_TYPE_P (TREE_TYPE (op0
))
502 || POINTER_TYPE_P (TREE_TYPE (op0
)))
503 && TREE_CODE (op0
) == SSA_NAME
504 && TREE_CODE (op1
) == INTEGER_CST
)
511 if (code
== GIMPLE_SWITCH
)
512 cond
= gimple_switch_index (as_a
<gswitch
*> (stmt
));
513 else if (code
== GIMPLE_GOTO
)
514 cond
= gimple_goto_dest (stmt
);
518 /* We can have conditionals which just test the state of a variable
519 rather than use a relational operator. These are simpler to handle. */
520 if (TREE_CODE (cond
) == SSA_NAME
)
522 tree original_lhs
= cond
;
525 /* Get the variable's current value from the equivalence chains.
527 It is possible to get loops in the SSA_NAME_VALUE chains
528 (consider threading the backedge of a loop where we have
529 a loop invariant SSA_NAME used in the condition). */
532 for (int i
= 0; i
< 2; i
++)
534 if (TREE_CODE (cached_lhs
) == SSA_NAME
535 && SSA_NAME_VALUE (cached_lhs
))
536 cached_lhs
= SSA_NAME_VALUE (cached_lhs
);
542 /* If we haven't simplified to an invariant yet, then use the
543 pass specific callback to try and simplify it further. */
544 if (cached_lhs
&& ! is_gimple_min_invariant (cached_lhs
))
546 if (code
== GIMPLE_SWITCH
)
548 /* Replace the index operand of the GIMPLE_SWITCH with any LHS
549 we found before handing off to VRP. If simplification is
550 possible, the simplified value will be a CASE_LABEL_EXPR of
551 the label that is proven to be taken. */
552 gswitch
*dummy_switch
= as_a
<gswitch
*> (gimple_copy (stmt
));
553 gimple_switch_set_index (dummy_switch
, cached_lhs
);
554 cached_lhs
= (*simplify
) (dummy_switch
, stmt
,
555 avail_exprs_stack
, e
->src
);
556 ggc_free (dummy_switch
);
559 cached_lhs
= (*simplify
) (stmt
, stmt
, avail_exprs_stack
, e
->src
);
562 /* We couldn't find an invariant. But, callers of this
563 function may be able to do something useful with the
564 unmodified destination. */
566 cached_lhs
= original_lhs
;
574 /* Recursive helper for simplify_control_stmt_condition. */
577 simplify_control_stmt_condition_1 (edge e
,
579 class avail_exprs_stack
*avail_exprs_stack
,
581 enum tree_code cond_code
,
584 pfn_simplify simplify
,
590 /* We may need to canonicalize the comparison. For
591 example, op0 might be a constant while op1 is an
592 SSA_NAME. Failure to canonicalize will cause us to
593 miss threading opportunities. */
594 if (tree_swap_operands_p (op0
, op1
))
596 cond_code
= swap_tree_comparison (cond_code
);
597 std::swap (op0
, op1
);
600 /* If the condition has the form (A & B) CMP 0 or (A | B) CMP 0 then
601 recurse into the LHS to see if there is a dominating ASSERT_EXPR
602 of A or of B that makes this condition always true or always false
604 if ((cond_code
== EQ_EXPR
|| cond_code
== NE_EXPR
)
605 && TREE_CODE (op0
) == SSA_NAME
606 && integer_zerop (op1
))
608 gimple
*def_stmt
= SSA_NAME_DEF_STMT (op0
);
609 if (gimple_code (def_stmt
) != GIMPLE_ASSIGN
)
611 else if (gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
612 || gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
)
614 enum tree_code rhs_code
= gimple_assign_rhs_code (def_stmt
);
615 const tree rhs1
= gimple_assign_rhs1 (def_stmt
);
616 const tree rhs2
= gimple_assign_rhs2 (def_stmt
);
620 = simplify_control_stmt_condition_1 (e
, def_stmt
, avail_exprs_stack
,
622 dummy_cond
, simplify
,
624 if (res1
== NULL_TREE
)
626 else if (rhs_code
== BIT_AND_EXPR
&& integer_zerop (res1
))
628 /* If A == 0 then (A & B) != 0 is always false. */
629 if (cond_code
== NE_EXPR
)
630 return boolean_false_node
;
631 /* If A == 0 then (A & B) == 0 is always true. */
632 if (cond_code
== EQ_EXPR
)
633 return boolean_true_node
;
635 else if (rhs_code
== BIT_IOR_EXPR
&& integer_nonzerop (res1
))
637 /* If A != 0 then (A | B) != 0 is always true. */
638 if (cond_code
== NE_EXPR
)
639 return boolean_true_node
;
640 /* If A != 0 then (A | B) == 0 is always false. */
641 if (cond_code
== EQ_EXPR
)
642 return boolean_false_node
;
647 = simplify_control_stmt_condition_1 (e
, def_stmt
, avail_exprs_stack
,
649 dummy_cond
, simplify
,
651 if (res2
== NULL_TREE
)
653 else if (rhs_code
== BIT_AND_EXPR
&& integer_zerop (res2
))
655 /* If B == 0 then (A & B) != 0 is always false. */
656 if (cond_code
== NE_EXPR
)
657 return boolean_false_node
;
658 /* If B == 0 then (A & B) == 0 is always true. */
659 if (cond_code
== EQ_EXPR
)
660 return boolean_true_node
;
662 else if (rhs_code
== BIT_IOR_EXPR
&& integer_nonzerop (res2
))
664 /* If B != 0 then (A | B) != 0 is always true. */
665 if (cond_code
== NE_EXPR
)
666 return boolean_true_node
;
667 /* If B != 0 then (A | B) == 0 is always false. */
668 if (cond_code
== EQ_EXPR
)
669 return boolean_false_node
;
672 if (res1
!= NULL_TREE
&& res2
!= NULL_TREE
)
674 if (rhs_code
== BIT_AND_EXPR
675 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
676 && integer_nonzerop (res1
)
677 && integer_nonzerop (res2
))
679 /* If A != 0 and B != 0 then (bool)(A & B) != 0 is true. */
680 if (cond_code
== NE_EXPR
)
681 return boolean_true_node
;
682 /* If A != 0 and B != 0 then (bool)(A & B) == 0 is false. */
683 if (cond_code
== EQ_EXPR
)
684 return boolean_false_node
;
687 if (rhs_code
== BIT_IOR_EXPR
688 && integer_zerop (res1
)
689 && integer_zerop (res2
))
691 /* If A == 0 and B == 0 then (A | B) != 0 is false. */
692 if (cond_code
== NE_EXPR
)
693 return boolean_false_node
;
694 /* If A == 0 and B == 0 then (A | B) == 0 is true. */
695 if (cond_code
== EQ_EXPR
)
696 return boolean_true_node
;
700 /* Handle (A CMP B) CMP 0. */
701 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt
))
704 tree rhs1
= gimple_assign_rhs1 (def_stmt
);
705 tree rhs2
= gimple_assign_rhs2 (def_stmt
);
707 tree_code new_cond
= gimple_assign_rhs_code (def_stmt
);
708 if (cond_code
== EQ_EXPR
)
709 new_cond
= invert_tree_comparison (new_cond
, false);
712 = simplify_control_stmt_condition_1 (e
, def_stmt
, avail_exprs_stack
,
713 rhs1
, new_cond
, rhs2
,
714 dummy_cond
, simplify
,
716 if (res
!= NULL_TREE
&& is_gimple_min_invariant (res
))
721 gimple_cond_set_code (dummy_cond
, cond_code
);
722 gimple_cond_set_lhs (dummy_cond
, op0
);
723 gimple_cond_set_rhs (dummy_cond
, op1
);
725 /* We absolutely do not care about any type conversions
726 we only care about a zero/nonzero value. */
727 fold_defer_overflow_warnings ();
729 tree res
= fold_binary (cond_code
, boolean_type_node
, op0
, op1
);
731 while (CONVERT_EXPR_P (res
))
732 res
= TREE_OPERAND (res
, 0);
734 fold_undefer_overflow_warnings ((res
&& is_gimple_min_invariant (res
)),
735 stmt
, WARN_STRICT_OVERFLOW_CONDITIONAL
);
737 /* If we have not simplified the condition down to an invariant,
738 then use the pass specific callback to simplify the condition. */
740 || !is_gimple_min_invariant (res
))
741 res
= (*simplify
) (dummy_cond
, stmt
, avail_exprs_stack
, e
->src
);
746 /* Copy debug stmts from DEST's chain of single predecessors up to
747 SRC, so that we don't lose the bindings as PHI nodes are introduced
748 when DEST gains new predecessors. */
750 propagate_threaded_block_debug_into (basic_block dest
, basic_block src
)
752 if (!MAY_HAVE_DEBUG_BIND_STMTS
)
755 if (!single_pred_p (dest
))
758 gcc_checking_assert (dest
!= src
);
760 gimple_stmt_iterator gsi
= gsi_after_labels (dest
);
762 const int alloc_count
= 16; // ?? Should this be a PARAM?
764 /* Estimate the number of debug vars overridden in the beginning of
765 DEST, to tell how many we're going to need to begin with. */
766 for (gimple_stmt_iterator si
= gsi
;
767 i
* 4 <= alloc_count
* 3 && !gsi_end_p (si
); gsi_next (&si
))
769 gimple
*stmt
= gsi_stmt (si
);
770 if (!is_gimple_debug (stmt
))
772 if (gimple_debug_nonbind_marker_p (stmt
))
777 auto_vec
<tree
, alloc_count
> fewvars
;
778 hash_set
<tree
> *vars
= NULL
;
780 /* If we're already starting with 3/4 of alloc_count, go for a
781 hash_set, otherwise start with an unordered stack-allocated
783 if (i
* 4 > alloc_count
* 3)
784 vars
= new hash_set
<tree
>;
786 /* Now go through the initial debug stmts in DEST again, this time
787 actually inserting in VARS or FEWVARS. Don't bother checking for
788 duplicates in FEWVARS. */
789 for (gimple_stmt_iterator si
= gsi
; !gsi_end_p (si
); gsi_next (&si
))
791 gimple
*stmt
= gsi_stmt (si
);
792 if (!is_gimple_debug (stmt
))
797 if (gimple_debug_bind_p (stmt
))
798 var
= gimple_debug_bind_get_var (stmt
);
799 else if (gimple_debug_source_bind_p (stmt
))
800 var
= gimple_debug_source_bind_get_var (stmt
);
801 else if (gimple_debug_nonbind_marker_p (stmt
))
809 fewvars
.quick_push (var
);
812 basic_block bb
= dest
;
816 bb
= single_pred (bb
);
817 for (gimple_stmt_iterator si
= gsi_last_bb (bb
);
818 !gsi_end_p (si
); gsi_prev (&si
))
820 gimple
*stmt
= gsi_stmt (si
);
821 if (!is_gimple_debug (stmt
))
826 if (gimple_debug_bind_p (stmt
))
827 var
= gimple_debug_bind_get_var (stmt
);
828 else if (gimple_debug_source_bind_p (stmt
))
829 var
= gimple_debug_source_bind_get_var (stmt
);
830 else if (gimple_debug_nonbind_marker_p (stmt
))
835 /* Discard debug bind overlaps. Unlike stmts from src,
836 copied into a new block that will precede BB, debug bind
837 stmts in bypassed BBs may actually be discarded if
838 they're overwritten by subsequent debug bind stmts. We
839 want to copy binds for all modified variables, so that we
840 retain a bind to the shared def if there is one, or to a
841 newly introduced PHI node if there is one. Our bind will
842 end up reset if the value is dead, but that implies the
843 variable couldn't have survived, so it's fine. We are
844 not actually running the code that performed the binds at
845 this point, we're just adding binds so that they survive
846 the new confluence, so markers should not be copied. */
847 if (vars
&& vars
->add (var
))
851 int i
= fewvars
.length ();
853 if (fewvars
[i
] == var
)
857 else if (fewvars
.length () < (unsigned) alloc_count
)
858 fewvars
.quick_push (var
);
861 vars
= new hash_set
<tree
>;
862 for (i
= 0; i
< alloc_count
; i
++)
863 vars
->add (fewvars
[i
]);
869 stmt
= gimple_copy (stmt
);
870 /* ??? Should we drop the location of the copy to denote
871 they're artificial bindings? */
872 gsi_insert_before (&gsi
, stmt
, GSI_NEW_STMT
);
875 while (bb
!= src
&& single_pred_p (bb
));
879 else if (fewvars
.exists ())
883 /* See if TAKEN_EDGE->dest is a threadable block with no side effecs (ie, it
884 need not be duplicated as part of the CFG/SSA updating process).
886 If it is threadable, add it to PATH and VISITED and recurse, ultimately
887 returning TRUE from the toplevel call. Otherwise do nothing and
890 DUMMY_COND, SIMPLIFY are used to try and simplify the condition at the
891 end of TAKEN_EDGE->dest.
893 The available expression table is referenced via AVAIL_EXPRS_STACK. */
896 thread_around_empty_blocks (edge taken_edge
,
898 class avail_exprs_stack
*avail_exprs_stack
,
899 pfn_simplify simplify
,
901 vec
<jump_thread_edge
*> *path
)
903 basic_block bb
= taken_edge
->dest
;
904 gimple_stmt_iterator gsi
;
908 /* The key property of these blocks is that they need not be duplicated
909 when threading. Thus they cannot have visible side effects such
911 if (!gsi_end_p (gsi_start_phis (bb
)))
914 /* Skip over DEBUG statements at the start of the block. */
915 gsi
= gsi_start_nondebug_bb (bb
);
917 /* If the block has no statements, but does have a single successor, then
918 it's just a forwarding block and we can thread through it trivially.
920 However, note that just threading through empty blocks with single
921 successors is not inherently profitable. For the jump thread to
922 be profitable, we must avoid a runtime conditional.
924 By taking the return value from the recursive call, we get the
925 desired effect of returning TRUE when we found a profitable jump
926 threading opportunity and FALSE otherwise.
928 This is particularly important when this routine is called after
929 processing a joiner block. Returning TRUE too aggressively in
930 that case results in pointless duplication of the joiner block. */
933 if (single_succ_p (bb
))
935 taken_edge
= single_succ_edge (bb
);
937 if ((taken_edge
->flags
& EDGE_DFS_BACK
) != 0)
940 if (!bitmap_bit_p (visited
, taken_edge
->dest
->index
))
943 = new jump_thread_edge (taken_edge
, EDGE_NO_COPY_SRC_BLOCK
);
945 bitmap_set_bit (visited
, taken_edge
->dest
->index
);
946 return thread_around_empty_blocks (taken_edge
,
955 /* We have a block with no statements, but multiple successors? */
959 /* The only real statements this block can have are a control
960 flow altering statement. Anything else stops the thread. */
961 stmt
= gsi_stmt (gsi
);
962 if (gimple_code (stmt
) != GIMPLE_COND
963 && gimple_code (stmt
) != GIMPLE_GOTO
964 && gimple_code (stmt
) != GIMPLE_SWITCH
)
967 /* Extract and simplify the condition. */
968 cond
= simplify_control_stmt_condition (taken_edge
, stmt
,
969 avail_exprs_stack
, dummy_cond
,
972 /* If the condition can be statically computed and we have not already
973 visited the destination edge, then add the taken edge to our thread
975 if (cond
!= NULL_TREE
976 && (is_gimple_min_invariant (cond
)
977 || TREE_CODE (cond
) == CASE_LABEL_EXPR
))
979 if (TREE_CODE (cond
) == CASE_LABEL_EXPR
)
980 taken_edge
= find_edge (bb
, label_to_block (cfun
, CASE_LABEL (cond
)));
982 taken_edge
= find_taken_edge (bb
, cond
);
985 || (taken_edge
->flags
& EDGE_DFS_BACK
) != 0)
988 if (bitmap_bit_p (visited
, taken_edge
->dest
->index
))
990 bitmap_set_bit (visited
, taken_edge
->dest
->index
);
993 = new jump_thread_edge (taken_edge
, EDGE_NO_COPY_SRC_BLOCK
);
996 thread_around_empty_blocks (taken_edge
,
1008 /* We are exiting E->src, see if E->dest ends with a conditional
1009 jump which has a known value when reached via E.
1011 E->dest can have arbitrary side effects which, if threading is
1012 successful, will be maintained.
1014 Special care is necessary if E is a back edge in the CFG as we
1015 may have already recorded equivalences for E->dest into our
1016 various tables, including the result of the conditional at
1017 the end of E->dest. Threading opportunities are severely
1018 limited in that case to avoid short-circuiting the loop
1021 DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
1022 to avoid allocating memory.
1024 STACK is used to undo temporary equivalences created during the walk of
1027 SIMPLIFY is a pass-specific function used to simplify statements.
1029 Our caller is responsible for restoring the state of the expression
1030 and const_and_copies stacks.
1032 Positive return value is success. Zero return value is failure, but
1033 the block can still be duplicated as a joiner in a jump thread path,
1034 negative indicates the block should not be duplicated and thus is not
1035 suitable for a joiner in a jump threading path. */
1038 thread_through_normal_block (edge e
,
1040 const_and_copies
*const_and_copies
,
1041 avail_exprs_stack
*avail_exprs_stack
,
1042 evrp_range_analyzer
*evrp_range_analyzer
,
1043 pfn_simplify simplify
,
1044 vec
<jump_thread_edge
*> *path
,
1047 /* We want to record any equivalences created by traversing E. */
1048 record_temporary_equivalences (e
, const_and_copies
, avail_exprs_stack
);
1050 /* PHIs create temporary equivalences.
1051 Note that if we found a PHI that made the block non-threadable, then
1052 we need to bubble that up to our caller in the same manner we do
1053 when we prematurely stop processing statements below. */
1054 if (!record_temporary_equivalences_from_phis (e
, const_and_copies
,
1055 evrp_range_analyzer
))
1058 /* Now walk each statement recording any context sensitive
1059 temporary equivalences we can detect. */
1061 = record_temporary_equivalences_from_stmts_at_dest (e
, const_and_copies
,
1063 evrp_range_analyzer
,
1066 /* There's two reasons STMT might be null, and distinguishing
1067 between them is important.
1069 First the block may not have had any statements. For example, it
1070 might have some PHIs and unconditionally transfer control elsewhere.
1071 Such blocks are suitable for jump threading, particularly as a
1074 The second reason would be if we did not process all the statements
1075 in the block (because there were too many to make duplicating the
1076 block profitable. If we did not look at all the statements, then
1077 we may not have invalidated everything needing invalidation. Thus
1078 we must signal to our caller that this block is not suitable for
1079 use as a joiner in a threading path. */
1082 /* First case. The statement simply doesn't have any instructions, but
1084 if (gsi_end_p (gsi_start_nondebug_bb (e
->dest
))
1085 && !gsi_end_p (gsi_start_phis (e
->dest
)))
1092 /* If we stopped at a COND_EXPR or SWITCH_EXPR, see if we know which arm
1094 if (gimple_code (stmt
) == GIMPLE_COND
1095 || gimple_code (stmt
) == GIMPLE_GOTO
1096 || gimple_code (stmt
) == GIMPLE_SWITCH
)
1100 /* Extract and simplify the condition. */
1101 cond
= simplify_control_stmt_condition (e
, stmt
, avail_exprs_stack
,
1102 dummy_cond
, simplify
);
1107 if (is_gimple_min_invariant (cond
)
1108 || TREE_CODE (cond
) == CASE_LABEL_EXPR
)
1111 if (TREE_CODE (cond
) == CASE_LABEL_EXPR
)
1112 taken_edge
= find_edge (e
->dest
,
1113 label_to_block (cfun
, CASE_LABEL (cond
)));
1115 taken_edge
= find_taken_edge (e
->dest
, cond
);
1117 basic_block dest
= (taken_edge
? taken_edge
->dest
: NULL
);
1119 /* DEST could be NULL for a computed jump to an absolute
1123 || (taken_edge
->flags
& EDGE_DFS_BACK
) != 0
1124 || bitmap_bit_p (visited
, dest
->index
))
1127 /* Only push the EDGE_START_JUMP_THREAD marker if this is
1128 first edge on the path. */
1129 if (path
->length () == 0)
1132 = new jump_thread_edge (e
, EDGE_START_JUMP_THREAD
);
1133 path
->safe_push (x
);
1137 = new jump_thread_edge (taken_edge
, EDGE_COPY_SRC_BLOCK
);
1138 path
->safe_push (x
);
1140 /* See if we can thread through DEST as well, this helps capture
1141 secondary effects of threading without having to re-run DOM or
1144 We don't want to thread back to a block we have already
1145 visited. This may be overly conservative. */
1146 bitmap_set_bit (visited
, dest
->index
);
1147 bitmap_set_bit (visited
, e
->dest
->index
);
1148 thread_around_empty_blocks (taken_edge
,
1160 /* There are basic blocks look like:
1162 p0 = a CMP b ; or p0 = (INT) (a CMP b)
1170 # phi = PHI <p0 (P0), p1 (P1)>
1171 if (phi != 0) goto <Y>; else goto <Z>;
1173 Then, edge (P0,X) or (P1,X) could be marked as EDGE_START_JUMP_THREAD
1174 And edge (X,Y), (X,Z) is EDGE_COPY_SRC_JOINER_BLOCK
1176 Return true if E is (P0,X) or (P1,X) */
1179 edge_forwards_cmp_to_conditional_jump_through_empty_bb_p (edge e
)
1181 /* See if there is only one stmt which is gcond. */
1183 if (!(gs
= safe_dyn_cast
<gcond
*> (last_and_only_stmt (e
->dest
))))
1186 /* See if gcond's cond is "(phi !=/== 0/1)" in the basic block. */
1187 tree cond
= gimple_cond_lhs (gs
);
1188 enum tree_code code
= gimple_cond_code (gs
);
1189 tree rhs
= gimple_cond_rhs (gs
);
1190 if (TREE_CODE (cond
) != SSA_NAME
1191 || (code
!= NE_EXPR
&& code
!= EQ_EXPR
)
1192 || (!integer_onep (rhs
) && !integer_zerop (rhs
)))
1194 gphi
*phi
= dyn_cast
<gphi
*> (SSA_NAME_DEF_STMT (cond
));
1195 if (phi
== NULL
|| gimple_bb (phi
) != e
->dest
)
1198 /* Check if phi's incoming value is CMP. */
1200 tree value
= PHI_ARG_DEF_FROM_EDGE (phi
, e
);
1201 if (TREE_CODE (value
) != SSA_NAME
1202 || !has_single_use (value
)
1203 || !(def
= dyn_cast
<gassign
*> (SSA_NAME_DEF_STMT (value
))))
1206 /* Or if it is (INT) (a CMP b). */
1207 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def
)))
1209 value
= gimple_assign_rhs1 (def
);
1210 if (TREE_CODE (value
) != SSA_NAME
1211 || !has_single_use (value
)
1212 || !(def
= dyn_cast
<gassign
*> (SSA_NAME_DEF_STMT (value
))))
1216 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def
)) != tcc_comparison
)
1222 /* We are exiting E->src, see if E->dest ends with a conditional
1223 jump which has a known value when reached via E.
1225 DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
1226 to avoid allocating memory.
1228 CONST_AND_COPIES is used to undo temporary equivalences created during the
1231 The available expression table is referenced vai AVAIL_EXPRS_STACK.
1233 SIMPLIFY is a pass-specific function used to simplify statements. */
1236 thread_across_edge (gcond
*dummy_cond
,
1238 class const_and_copies
*const_and_copies
,
1239 class avail_exprs_stack
*avail_exprs_stack
,
1240 class evrp_range_analyzer
*evrp_range_analyzer
,
1241 pfn_simplify simplify
)
1243 bitmap visited
= BITMAP_ALLOC (NULL
);
1245 const_and_copies
->push_marker ();
1246 avail_exprs_stack
->push_marker ();
1247 if (evrp_range_analyzer
)
1248 evrp_range_analyzer
->push_marker ();
1252 vec
<jump_thread_edge
*> *path
= new vec
<jump_thread_edge
*> ();
1253 bitmap_clear (visited
);
1254 bitmap_set_bit (visited
, e
->src
->index
);
1255 bitmap_set_bit (visited
, e
->dest
->index
);
1258 if ((e
->flags
& EDGE_DFS_BACK
) == 0)
1259 threaded
= thread_through_normal_block (e
, dummy_cond
,
1262 evrp_range_analyzer
,
1270 propagate_threaded_block_debug_into (path
->last ()->e
->dest
,
1272 const_and_copies
->pop_to_marker ();
1273 avail_exprs_stack
->pop_to_marker ();
1274 if (evrp_range_analyzer
)
1275 evrp_range_analyzer
->pop_to_marker ();
1276 BITMAP_FREE (visited
);
1277 register_jump_thread (path
);
1282 /* Negative and zero return values indicate no threading was possible,
1283 thus there should be no edges on the thread path and no need to walk
1284 through the vector entries. */
1285 gcc_assert (path
->length () == 0);
1289 /* A negative status indicates the target block was deemed too big to
1290 duplicate. Just quit now rather than trying to use the block as
1291 a joiner in a jump threading path.
1293 This prevents unnecessary code growth, but more importantly if we
1294 do not look at all the statements in the block, then we may have
1295 missed some invalidations if we had traversed a backedge! */
1298 BITMAP_FREE (visited
);
1299 const_and_copies
->pop_to_marker ();
1300 avail_exprs_stack
->pop_to_marker ();
1301 if (evrp_range_analyzer
)
1302 evrp_range_analyzer
->pop_to_marker ();
1307 /* We were unable to determine what out edge from E->dest is taken. However,
1308 we might still be able to thread through successors of E->dest. This
1309 often occurs when E->dest is a joiner block which then fans back out
1310 based on redundant tests.
1312 If so, we'll copy E->dest and redirect the appropriate predecessor to
1313 the copy. Within the copy of E->dest, we'll thread one or more edges
1314 to points deeper in the CFG.
1316 This is a stopgap until we have a more structured approach to path
1323 /* If E->dest has abnormal outgoing edges, then there's no guarantee
1324 we can safely redirect any of the edges. Just punt those cases. */
1325 FOR_EACH_EDGE (taken_edge
, ei
, e
->dest
->succs
)
1326 if (taken_edge
->flags
& EDGE_COMPLEX
)
1328 const_and_copies
->pop_to_marker ();
1329 avail_exprs_stack
->pop_to_marker ();
1330 if (evrp_range_analyzer
)
1331 evrp_range_analyzer
->pop_to_marker ();
1332 BITMAP_FREE (visited
);
1336 /* Look at each successor of E->dest to see if we can thread through it. */
1337 FOR_EACH_EDGE (taken_edge
, ei
, e
->dest
->succs
)
1339 if ((e
->flags
& EDGE_DFS_BACK
) != 0
1340 || (taken_edge
->flags
& EDGE_DFS_BACK
) != 0)
1343 /* Push a fresh marker so we can unwind the equivalences created
1344 for each of E->dest's successors. */
1345 const_and_copies
->push_marker ();
1346 avail_exprs_stack
->push_marker ();
1347 if (evrp_range_analyzer
)
1348 evrp_range_analyzer
->push_marker ();
1350 /* Avoid threading to any block we have already visited. */
1351 bitmap_clear (visited
);
1352 bitmap_set_bit (visited
, e
->src
->index
);
1353 bitmap_set_bit (visited
, e
->dest
->index
);
1354 bitmap_set_bit (visited
, taken_edge
->dest
->index
);
1355 vec
<jump_thread_edge
*> *path
= new vec
<jump_thread_edge
*> ();
1357 /* Record whether or not we were able to thread through a successor
1359 jump_thread_edge
*x
= new jump_thread_edge (e
, EDGE_START_JUMP_THREAD
);
1360 path
->safe_push (x
);
1362 x
= new jump_thread_edge (taken_edge
, EDGE_COPY_SRC_JOINER_BLOCK
);
1363 path
->safe_push (x
);
1364 found
= thread_around_empty_blocks (taken_edge
,
1372 found
= thread_through_normal_block (path
->last ()->e
, dummy_cond
,
1375 evrp_range_analyzer
,
1379 /* If we were able to thread through a successor of E->dest, then
1380 record the jump threading opportunity. */
1382 || edge_forwards_cmp_to_conditional_jump_through_empty_bb_p (e
))
1384 if (taken_edge
->dest
!= path
->last ()->e
->dest
)
1385 propagate_threaded_block_debug_into (path
->last ()->e
->dest
,
1387 register_jump_thread (path
);
1390 delete_jump_thread_path (path
);
1392 /* And unwind the equivalence table. */
1393 if (evrp_range_analyzer
)
1394 evrp_range_analyzer
->pop_to_marker ();
1395 avail_exprs_stack
->pop_to_marker ();
1396 const_and_copies
->pop_to_marker ();
1398 BITMAP_FREE (visited
);
1401 if (evrp_range_analyzer
)
1402 evrp_range_analyzer
->pop_to_marker ();
1403 const_and_copies
->pop_to_marker ();
1404 avail_exprs_stack
->pop_to_marker ();
1407 /* Examine the outgoing edges from BB and conditionally
1410 DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
1411 to avoid allocating memory.
1413 CONST_AND_COPIES is used to undo temporary equivalences created during the
1416 The available expression table is referenced vai AVAIL_EXPRS_STACK.
1418 SIMPLIFY is a pass-specific function used to simplify statements. */
1421 thread_outgoing_edges (basic_block bb
, gcond
*dummy_cond
,
1422 class const_and_copies
*const_and_copies
,
1423 class avail_exprs_stack
*avail_exprs_stack
,
1424 class evrp_range_analyzer
*evrp_range_analyzer
,
1425 tree (*simplify
) (gimple
*, gimple
*,
1426 class avail_exprs_stack
*,
1429 int flags
= (EDGE_IGNORE
| EDGE_COMPLEX
| EDGE_ABNORMAL
);
1432 /* If we have an outgoing edge to a block with multiple incoming and
1433 outgoing edges, then we may be able to thread the edge, i.e., we
1434 may be able to statically determine which of the outgoing edges
1435 will be traversed when the incoming edge from BB is traversed. */
1436 if (single_succ_p (bb
)
1437 && (single_succ_edge (bb
)->flags
& flags
) == 0
1438 && potentially_threadable_block (single_succ (bb
)))
1440 thread_across_edge (dummy_cond
, single_succ_edge (bb
),
1441 const_and_copies
, avail_exprs_stack
,
1442 evrp_range_analyzer
, simplify
);
1444 else if ((last
= last_stmt (bb
))
1445 && gimple_code (last
) == GIMPLE_COND
1446 && EDGE_COUNT (bb
->succs
) == 2
1447 && (EDGE_SUCC (bb
, 0)->flags
& flags
) == 0
1448 && (EDGE_SUCC (bb
, 1)->flags
& flags
) == 0)
1450 edge true_edge
, false_edge
;
1452 extract_true_false_edges_from_block (bb
, &true_edge
, &false_edge
);
1454 /* Only try to thread the edge if it reaches a target block with
1455 more than one predecessor and more than one successor. */
1456 if (potentially_threadable_block (true_edge
->dest
))
1457 thread_across_edge (dummy_cond
, true_edge
,
1458 const_and_copies
, avail_exprs_stack
,
1459 evrp_range_analyzer
, simplify
);
1461 /* Similarly for the ELSE arm. */
1462 if (potentially_threadable_block (false_edge
->dest
))
1463 thread_across_edge (dummy_cond
, false_edge
,
1464 const_and_copies
, avail_exprs_stack
,
1465 evrp_range_analyzer
, simplify
);