Fix libstdc++ testsuite failures in C++98 and C++11 mode
[official-gcc.git] / gcc / tree-ssa-threadedge.c
blob4949bfa03ab9f293a327e18a8f068c37746696f8
1 /* SSA Jump Threading
2 Copyright (C) 2005-2017 Free Software Foundation, Inc.
3 Contributed by Jeff Law <law@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "predict.h"
28 #include "ssa.h"
29 #include "fold-const.h"
30 #include "cfgloop.h"
31 #include "gimple-iterator.h"
32 #include "tree-cfg.h"
33 #include "tree-ssa-threadupdate.h"
34 #include "params.h"
35 #include "tree-ssa-scopedtables.h"
36 #include "tree-ssa-threadedge.h"
37 #include "tree-ssa-dom.h"
38 #include "gimple-fold.h"
39 #include "cfganal.h"
41 /* To avoid code explosion due to jump threading, we limit the
42 number of statements we are going to copy. This variable
43 holds the number of statements currently seen that we'll have
44 to copy as part of the jump threading process. */
45 static int stmt_count;
47 /* Array to record value-handles per SSA_NAME. */
48 vec<tree> ssa_name_values;
50 typedef tree (pfn_simplify) (gimple *, gimple *, class avail_exprs_stack *);
52 /* Set the value for the SSA name NAME to VALUE. */
54 void
55 set_ssa_name_value (tree name, tree value)
57 if (SSA_NAME_VERSION (name) >= ssa_name_values.length ())
58 ssa_name_values.safe_grow_cleared (SSA_NAME_VERSION (name) + 1);
59 if (value && TREE_OVERFLOW_P (value))
60 value = drop_tree_overflow (value);
61 ssa_name_values[SSA_NAME_VERSION (name)] = value;
64 /* Initialize the per SSA_NAME value-handles array. Returns it. */
65 void
66 threadedge_initialize_values (void)
68 gcc_assert (!ssa_name_values.exists ());
69 ssa_name_values.create (num_ssa_names);
72 /* Free the per SSA_NAME value-handle array. */
73 void
74 threadedge_finalize_values (void)
76 ssa_name_values.release ();
79 /* Return TRUE if we may be able to thread an incoming edge into
80 BB to an outgoing edge from BB. Return FALSE otherwise. */
82 bool
83 potentially_threadable_block (basic_block bb)
85 gimple_stmt_iterator gsi;
87 /* Special case. We can get blocks that are forwarders, but are
88 not optimized away because they forward from outside a loop
89 to the loop header. We want to thread through them as we can
90 sometimes thread to the loop exit, which is obviously profitable.
91 the interesting case here is when the block has PHIs. */
92 if (gsi_end_p (gsi_start_nondebug_bb (bb))
93 && !gsi_end_p (gsi_start_phis (bb)))
94 return true;
96 /* If BB has a single successor or a single predecessor, then
97 there is no threading opportunity. */
98 if (single_succ_p (bb) || single_pred_p (bb))
99 return false;
101 /* If BB does not end with a conditional, switch or computed goto,
102 then there is no threading opportunity. */
103 gsi = gsi_last_bb (bb);
104 if (gsi_end_p (gsi)
105 || ! gsi_stmt (gsi)
106 || (gimple_code (gsi_stmt (gsi)) != GIMPLE_COND
107 && gimple_code (gsi_stmt (gsi)) != GIMPLE_GOTO
108 && gimple_code (gsi_stmt (gsi)) != GIMPLE_SWITCH))
109 return false;
111 return true;
114 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
115 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
116 BB. If no such ASSERT_EXPR is found, return OP. */
118 static tree
119 lhs_of_dominating_assert (tree op, basic_block bb, gimple *stmt)
121 imm_use_iterator imm_iter;
122 gimple *use_stmt;
123 use_operand_p use_p;
125 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
127 use_stmt = USE_STMT (use_p);
128 if (use_stmt != stmt
129 && gimple_assign_single_p (use_stmt)
130 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ASSERT_EXPR
131 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) == op
132 && dominated_by_p (CDI_DOMINATORS, bb, gimple_bb (use_stmt)))
134 return gimple_assign_lhs (use_stmt);
137 return op;
140 /* Record temporary equivalences created by PHIs at the target of the
141 edge E. Record unwind information for the equivalences onto STACK.
143 If a PHI which prevents threading is encountered, then return FALSE
144 indicating we should not thread this edge, else return TRUE.
146 If SRC_MAP/DST_MAP exist, then mark the source and destination SSA_NAMEs
147 of any equivalences recorded. We use this to make invalidation after
148 traversing back edges less painful. */
150 static bool
151 record_temporary_equivalences_from_phis (edge e, const_and_copies *const_and_copies)
153 gphi_iterator gsi;
155 /* Each PHI creates a temporary equivalence, record them.
156 These are context sensitive equivalences and will be removed
157 later. */
158 for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
160 gphi *phi = gsi.phi ();
161 tree src = PHI_ARG_DEF_FROM_EDGE (phi, e);
162 tree dst = gimple_phi_result (phi);
164 /* If the desired argument is not the same as this PHI's result
165 and it is set by a PHI in E->dest, then we can not thread
166 through E->dest. */
167 if (src != dst
168 && TREE_CODE (src) == SSA_NAME
169 && gimple_code (SSA_NAME_DEF_STMT (src)) == GIMPLE_PHI
170 && gimple_bb (SSA_NAME_DEF_STMT (src)) == e->dest)
171 return false;
173 /* We consider any non-virtual PHI as a statement since it
174 count result in a constant assignment or copy operation. */
175 if (!virtual_operand_p (dst))
176 stmt_count++;
178 const_and_copies->record_const_or_copy (dst, src);
180 return true;
183 /* Valueize hook for gimple_fold_stmt_to_constant_1. */
185 static tree
186 threadedge_valueize (tree t)
188 if (TREE_CODE (t) == SSA_NAME)
190 tree tem = SSA_NAME_VALUE (t);
191 if (tem)
192 return tem;
194 return t;
197 /* Try to simplify each statement in E->dest, ultimately leading to
198 a simplification of the COND_EXPR at the end of E->dest.
200 Record unwind information for temporary equivalences onto STACK.
202 Use SIMPLIFY (a pointer to a callback function) to further simplify
203 statements using pass specific information.
205 We might consider marking just those statements which ultimately
206 feed the COND_EXPR. It's not clear if the overhead of bookkeeping
207 would be recovered by trying to simplify fewer statements.
209 If we are able to simplify a statement into the form
210 SSA_NAME = (SSA_NAME | gimple invariant), then we can record
211 a context sensitive equivalence which may help us simplify
212 later statements in E->dest. */
214 static gimple *
215 record_temporary_equivalences_from_stmts_at_dest (edge e,
216 const_and_copies *const_and_copies,
217 avail_exprs_stack *avail_exprs_stack,
218 pfn_simplify simplify)
220 gimple *stmt = NULL;
221 gimple_stmt_iterator gsi;
222 int max_stmt_count;
224 max_stmt_count = PARAM_VALUE (PARAM_MAX_JUMP_THREAD_DUPLICATION_STMTS);
226 /* Walk through each statement in the block recording equivalences
227 we discover. Note any equivalences we discover are context
228 sensitive (ie, are dependent on traversing E) and must be unwound
229 when we're finished processing E. */
230 for (gsi = gsi_start_bb (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
232 tree cached_lhs = NULL;
234 stmt = gsi_stmt (gsi);
236 /* Ignore empty statements and labels. */
237 if (gimple_code (stmt) == GIMPLE_NOP
238 || gimple_code (stmt) == GIMPLE_LABEL
239 || is_gimple_debug (stmt))
240 continue;
242 /* If the statement has volatile operands, then we assume we
243 can not thread through this block. This is overly
244 conservative in some ways. */
245 if (gimple_code (stmt) == GIMPLE_ASM
246 && gimple_asm_volatile_p (as_a <gasm *> (stmt)))
247 return NULL;
249 /* If the statement is a unique builtin, we can not thread
250 through here. */
251 if (gimple_code (stmt) == GIMPLE_CALL
252 && gimple_call_internal_p (stmt)
253 && gimple_call_internal_unique_p (stmt))
254 return NULL;
256 /* If duplicating this block is going to cause too much code
257 expansion, then do not thread through this block. */
258 stmt_count++;
259 if (stmt_count > max_stmt_count)
260 return NULL;
262 /* If this is not a statement that sets an SSA_NAME to a new
263 value, then do not try to simplify this statement as it will
264 not simplify in any way that is helpful for jump threading. */
265 if ((gimple_code (stmt) != GIMPLE_ASSIGN
266 || TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
267 && (gimple_code (stmt) != GIMPLE_CALL
268 || gimple_call_lhs (stmt) == NULL_TREE
269 || TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME))
270 continue;
272 /* The result of __builtin_object_size depends on all the arguments
273 of a phi node. Temporarily using only one edge produces invalid
274 results. For example
276 if (x < 6)
277 goto l;
278 else
279 goto l;
282 r = PHI <&w[2].a[1](2), &a.a[6](3)>
283 __builtin_object_size (r, 0)
285 The result of __builtin_object_size is defined to be the maximum of
286 remaining bytes. If we use only one edge on the phi, the result will
287 change to be the remaining bytes for the corresponding phi argument.
289 Similarly for __builtin_constant_p:
291 r = PHI <1(2), 2(3)>
292 __builtin_constant_p (r)
294 Both PHI arguments are constant, but x ? 1 : 2 is still not
295 constant. */
297 if (is_gimple_call (stmt))
299 tree fndecl = gimple_call_fndecl (stmt);
300 if (fndecl
301 && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_OBJECT_SIZE
302 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P))
303 continue;
306 /* At this point we have a statement which assigns an RHS to an
307 SSA_VAR on the LHS. We want to try and simplify this statement
308 to expose more context sensitive equivalences which in turn may
309 allow us to simplify the condition at the end of the loop.
311 Handle simple copy operations as well as implied copies from
312 ASSERT_EXPRs. */
313 if (gimple_assign_single_p (stmt)
314 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
315 cached_lhs = gimple_assign_rhs1 (stmt);
316 else if (gimple_assign_single_p (stmt)
317 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
318 cached_lhs = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
319 else
321 /* A statement that is not a trivial copy or ASSERT_EXPR.
322 Try to fold the new expression. Inserting the
323 expression into the hash table is unlikely to help. */
324 /* ??? The DOM callback below can be changed to setting
325 the mprts_hook around the call to thread_across_edge,
326 avoiding the use substitution. The VRP hook should be
327 changed to properly valueize operands itself using
328 SSA_NAME_VALUE in addition to its own lattice. */
329 cached_lhs = gimple_fold_stmt_to_constant_1 (stmt,
330 threadedge_valueize);
331 if (NUM_SSA_OPERANDS (stmt, SSA_OP_ALL_USES) != 0
332 && (!cached_lhs
333 || (TREE_CODE (cached_lhs) != SSA_NAME
334 && !is_gimple_min_invariant (cached_lhs))))
336 /* We're going to temporarily copy propagate the operands
337 and see if that allows us to simplify this statement. */
338 tree *copy;
339 ssa_op_iter iter;
340 use_operand_p use_p;
341 unsigned int num, i = 0;
343 num = NUM_SSA_OPERANDS (stmt, SSA_OP_ALL_USES);
344 copy = XALLOCAVEC (tree, num);
346 /* Make a copy of the uses & vuses into USES_COPY, then cprop into
347 the operands. */
348 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
350 tree tmp = NULL;
351 tree use = USE_FROM_PTR (use_p);
353 copy[i++] = use;
354 if (TREE_CODE (use) == SSA_NAME)
355 tmp = SSA_NAME_VALUE (use);
356 if (tmp)
357 SET_USE (use_p, tmp);
360 cached_lhs = (*simplify) (stmt, stmt, avail_exprs_stack);
362 /* Restore the statement's original uses/defs. */
363 i = 0;
364 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
365 SET_USE (use_p, copy[i++]);
369 /* Record the context sensitive equivalence if we were able
370 to simplify this statement. */
371 if (cached_lhs
372 && (TREE_CODE (cached_lhs) == SSA_NAME
373 || is_gimple_min_invariant (cached_lhs)))
374 const_and_copies->record_const_or_copy (gimple_get_lhs (stmt),
375 cached_lhs);
377 return stmt;
380 static tree simplify_control_stmt_condition_1 (edge, gimple *,
381 class avail_exprs_stack *,
382 tree, enum tree_code, tree,
383 gcond *, pfn_simplify, bool,
384 unsigned);
386 /* Simplify the control statement at the end of the block E->dest.
388 To avoid allocating memory unnecessarily, a scratch GIMPLE_COND
389 is available to use/clobber in DUMMY_COND.
391 Use SIMPLIFY (a pointer to a callback function) to further simplify
392 a condition using pass specific information.
394 Return the simplified condition or NULL if simplification could
395 not be performed. When simplifying a GIMPLE_SWITCH, we may return
396 the CASE_LABEL_EXPR that will be taken.
398 The available expression table is referenced via AVAIL_EXPRS_STACK. */
400 static tree
401 simplify_control_stmt_condition (edge e,
402 gimple *stmt,
403 class avail_exprs_stack *avail_exprs_stack,
404 gcond *dummy_cond,
405 pfn_simplify simplify,
406 bool handle_dominating_asserts)
408 tree cond, cached_lhs;
409 enum gimple_code code = gimple_code (stmt);
411 /* For comparisons, we have to update both operands, then try
412 to simplify the comparison. */
413 if (code == GIMPLE_COND)
415 tree op0, op1;
416 enum tree_code cond_code;
418 op0 = gimple_cond_lhs (stmt);
419 op1 = gimple_cond_rhs (stmt);
420 cond_code = gimple_cond_code (stmt);
422 /* Get the current value of both operands. */
423 if (TREE_CODE (op0) == SSA_NAME)
425 for (int i = 0; i < 2; i++)
427 if (TREE_CODE (op0) == SSA_NAME
428 && SSA_NAME_VALUE (op0))
429 op0 = SSA_NAME_VALUE (op0);
430 else
431 break;
435 if (TREE_CODE (op1) == SSA_NAME)
437 for (int i = 0; i < 2; i++)
439 if (TREE_CODE (op1) == SSA_NAME
440 && SSA_NAME_VALUE (op1))
441 op1 = SSA_NAME_VALUE (op1);
442 else
443 break;
447 const unsigned recursion_limit = 4;
449 cached_lhs
450 = simplify_control_stmt_condition_1 (e, stmt, avail_exprs_stack,
451 op0, cond_code, op1,
452 dummy_cond, simplify,
453 handle_dominating_asserts,
454 recursion_limit);
456 /* If we were testing an integer/pointer against a constant, then
457 we can use the FSM code to trace the value of the SSA_NAME. If
458 a value is found, then the condition will collapse to a constant.
460 Return the SSA_NAME we want to trace back rather than the full
461 expression and give the FSM threader a chance to find its value. */
462 if (cached_lhs == NULL)
464 /* Recover the original operands. They may have been simplified
465 using context sensitive equivalences. Those context sensitive
466 equivalences may not be valid on paths found by the FSM optimizer. */
467 tree op0 = gimple_cond_lhs (stmt);
468 tree op1 = gimple_cond_rhs (stmt);
470 if ((INTEGRAL_TYPE_P (TREE_TYPE (op0))
471 || POINTER_TYPE_P (TREE_TYPE (op0)))
472 && TREE_CODE (op0) == SSA_NAME
473 && TREE_CODE (op1) == INTEGER_CST)
474 return op0;
477 return cached_lhs;
480 if (code == GIMPLE_SWITCH)
481 cond = gimple_switch_index (as_a <gswitch *> (stmt));
482 else if (code == GIMPLE_GOTO)
483 cond = gimple_goto_dest (stmt);
484 else
485 gcc_unreachable ();
487 /* We can have conditionals which just test the state of a variable
488 rather than use a relational operator. These are simpler to handle. */
489 if (TREE_CODE (cond) == SSA_NAME)
491 tree original_lhs = cond;
492 cached_lhs = cond;
494 /* Get the variable's current value from the equivalence chains.
496 It is possible to get loops in the SSA_NAME_VALUE chains
497 (consider threading the backedge of a loop where we have
498 a loop invariant SSA_NAME used in the condition). */
499 if (cached_lhs)
501 for (int i = 0; i < 2; i++)
503 if (TREE_CODE (cached_lhs) == SSA_NAME
504 && SSA_NAME_VALUE (cached_lhs))
505 cached_lhs = SSA_NAME_VALUE (cached_lhs);
506 else
507 break;
511 /* If we're dominated by a suitable ASSERT_EXPR, then
512 update CACHED_LHS appropriately. */
513 if (handle_dominating_asserts && TREE_CODE (cached_lhs) == SSA_NAME)
514 cached_lhs = lhs_of_dominating_assert (cached_lhs, e->src, stmt);
516 /* If we haven't simplified to an invariant yet, then use the
517 pass specific callback to try and simplify it further. */
518 if (cached_lhs && ! is_gimple_min_invariant (cached_lhs))
520 if (handle_dominating_asserts && code == GIMPLE_SWITCH)
522 /* Replace the index operand of the GIMPLE_SWITCH with the
523 dominating ASSERT_EXPR before handing it off to VRP. If
524 simplification is possible, the simplified value will be a
525 CASE_LABEL_EXPR of the label that is proven to be taken. */
526 gswitch *dummy_switch = as_a<gswitch *> (gimple_copy (stmt));
527 gimple_switch_set_index (dummy_switch, cached_lhs);
528 cached_lhs = (*simplify) (dummy_switch, stmt, avail_exprs_stack);
529 ggc_free (dummy_switch);
531 else
532 cached_lhs = (*simplify) (stmt, stmt, avail_exprs_stack);
535 /* We couldn't find an invariant. But, callers of this
536 function may be able to do something useful with the
537 unmodified destination. */
538 if (!cached_lhs)
539 cached_lhs = original_lhs;
541 else
542 cached_lhs = NULL;
544 return cached_lhs;
547 /* Recursive helper for simplify_control_stmt_condition. */
549 static tree
550 simplify_control_stmt_condition_1 (edge e,
551 gimple *stmt,
552 class avail_exprs_stack *avail_exprs_stack,
553 tree op0,
554 enum tree_code cond_code,
555 tree op1,
556 gcond *dummy_cond,
557 pfn_simplify simplify,
558 bool handle_dominating_asserts,
559 unsigned limit)
561 if (limit == 0)
562 return NULL_TREE;
564 /* We may need to canonicalize the comparison. For
565 example, op0 might be a constant while op1 is an
566 SSA_NAME. Failure to canonicalize will cause us to
567 miss threading opportunities. */
568 if (tree_swap_operands_p (op0, op1))
570 cond_code = swap_tree_comparison (cond_code);
571 std::swap (op0, op1);
574 /* If the condition has the form (A & B) CMP 0 or (A | B) CMP 0 then
575 recurse into the LHS to see if there is a dominating ASSERT_EXPR
576 of A or of B that makes this condition always true or always false
577 along the edge E. */
578 if (handle_dominating_asserts
579 && (cond_code == EQ_EXPR || cond_code == NE_EXPR)
580 && TREE_CODE (op0) == SSA_NAME
581 && integer_zerop (op1))
583 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
584 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
586 else if (gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR
587 || gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR)
589 enum tree_code rhs_code = gimple_assign_rhs_code (def_stmt);
590 const tree rhs1 = gimple_assign_rhs1 (def_stmt);
591 const tree rhs2 = gimple_assign_rhs2 (def_stmt);
593 /* Is A != 0 ? */
594 const tree res1
595 = simplify_control_stmt_condition_1 (e, def_stmt, avail_exprs_stack,
596 rhs1, NE_EXPR, op1,
597 dummy_cond, simplify,
598 handle_dominating_asserts,
599 limit - 1);
600 if (res1 == NULL_TREE)
602 else if (rhs_code == BIT_AND_EXPR && integer_zerop (res1))
604 /* If A == 0 then (A & B) != 0 is always false. */
605 if (cond_code == NE_EXPR)
606 return boolean_false_node;
607 /* If A == 0 then (A & B) == 0 is always true. */
608 if (cond_code == EQ_EXPR)
609 return boolean_true_node;
611 else if (rhs_code == BIT_IOR_EXPR && integer_nonzerop (res1))
613 /* If A != 0 then (A | B) != 0 is always true. */
614 if (cond_code == NE_EXPR)
615 return boolean_true_node;
616 /* If A != 0 then (A | B) == 0 is always false. */
617 if (cond_code == EQ_EXPR)
618 return boolean_false_node;
621 /* Is B != 0 ? */
622 const tree res2
623 = simplify_control_stmt_condition_1 (e, def_stmt, avail_exprs_stack,
624 rhs2, NE_EXPR, op1,
625 dummy_cond, simplify,
626 handle_dominating_asserts,
627 limit - 1);
628 if (res2 == NULL_TREE)
630 else if (rhs_code == BIT_AND_EXPR && integer_zerop (res2))
632 /* If B == 0 then (A & B) != 0 is always false. */
633 if (cond_code == NE_EXPR)
634 return boolean_false_node;
635 /* If B == 0 then (A & B) == 0 is always true. */
636 if (cond_code == EQ_EXPR)
637 return boolean_true_node;
639 else if (rhs_code == BIT_IOR_EXPR && integer_nonzerop (res2))
641 /* If B != 0 then (A | B) != 0 is always true. */
642 if (cond_code == NE_EXPR)
643 return boolean_true_node;
644 /* If B != 0 then (A | B) == 0 is always false. */
645 if (cond_code == EQ_EXPR)
646 return boolean_false_node;
649 if (res1 != NULL_TREE && res2 != NULL_TREE)
651 if (rhs_code == BIT_AND_EXPR
652 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
653 && integer_nonzerop (res1)
654 && integer_nonzerop (res2))
656 /* If A != 0 and B != 0 then (bool)(A & B) != 0 is true. */
657 if (cond_code == NE_EXPR)
658 return boolean_true_node;
659 /* If A != 0 and B != 0 then (bool)(A & B) == 0 is false. */
660 if (cond_code == EQ_EXPR)
661 return boolean_false_node;
664 if (rhs_code == BIT_IOR_EXPR
665 && integer_zerop (res1)
666 && integer_zerop (res2))
668 /* If A == 0 and B == 0 then (A | B) != 0 is false. */
669 if (cond_code == NE_EXPR)
670 return boolean_false_node;
671 /* If A == 0 and B == 0 then (A | B) == 0 is true. */
672 if (cond_code == EQ_EXPR)
673 return boolean_true_node;
677 /* Handle (A CMP B) CMP 0. */
678 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt))
679 == tcc_comparison)
681 tree rhs1 = gimple_assign_rhs1 (def_stmt);
682 tree rhs2 = gimple_assign_rhs2 (def_stmt);
684 tree_code new_cond = gimple_assign_rhs_code (def_stmt);
685 if (cond_code == EQ_EXPR)
686 new_cond = invert_tree_comparison (new_cond, false);
688 tree res
689 = simplify_control_stmt_condition_1 (e, def_stmt, avail_exprs_stack,
690 rhs1, new_cond, rhs2,
691 dummy_cond, simplify,
692 handle_dominating_asserts,
693 limit - 1);
694 if (res != NULL_TREE && is_gimple_min_invariant (res))
695 return res;
699 if (handle_dominating_asserts)
701 /* Now see if the operand was consumed by an ASSERT_EXPR
702 which dominates E->src. If so, we want to replace the
703 operand with the LHS of the ASSERT_EXPR. */
704 if (TREE_CODE (op0) == SSA_NAME)
705 op0 = lhs_of_dominating_assert (op0, e->src, stmt);
707 if (TREE_CODE (op1) == SSA_NAME)
708 op1 = lhs_of_dominating_assert (op1, e->src, stmt);
711 gimple_cond_set_code (dummy_cond, cond_code);
712 gimple_cond_set_lhs (dummy_cond, op0);
713 gimple_cond_set_rhs (dummy_cond, op1);
715 /* We absolutely do not care about any type conversions
716 we only care about a zero/nonzero value. */
717 fold_defer_overflow_warnings ();
719 tree res = fold_binary (cond_code, boolean_type_node, op0, op1);
720 if (res)
721 while (CONVERT_EXPR_P (res))
722 res = TREE_OPERAND (res, 0);
724 fold_undefer_overflow_warnings ((res && is_gimple_min_invariant (res)),
725 stmt, WARN_STRICT_OVERFLOW_CONDITIONAL);
727 /* If we have not simplified the condition down to an invariant,
728 then use the pass specific callback to simplify the condition. */
729 if (!res
730 || !is_gimple_min_invariant (res))
731 res = (*simplify) (dummy_cond, stmt, avail_exprs_stack);
733 return res;
736 /* Copy debug stmts from DEST's chain of single predecessors up to
737 SRC, so that we don't lose the bindings as PHI nodes are introduced
738 when DEST gains new predecessors. */
739 void
740 propagate_threaded_block_debug_into (basic_block dest, basic_block src)
742 if (!MAY_HAVE_DEBUG_STMTS)
743 return;
745 if (!single_pred_p (dest))
746 return;
748 gcc_checking_assert (dest != src);
750 gimple_stmt_iterator gsi = gsi_after_labels (dest);
751 int i = 0;
752 const int alloc_count = 16; // ?? Should this be a PARAM?
754 /* Estimate the number of debug vars overridden in the beginning of
755 DEST, to tell how many we're going to need to begin with. */
756 for (gimple_stmt_iterator si = gsi;
757 i * 4 <= alloc_count * 3 && !gsi_end_p (si); gsi_next (&si))
759 gimple *stmt = gsi_stmt (si);
760 if (!is_gimple_debug (stmt))
761 break;
762 i++;
765 auto_vec<tree, alloc_count> fewvars;
766 hash_set<tree> *vars = NULL;
768 /* If we're already starting with 3/4 of alloc_count, go for a
769 hash_set, otherwise start with an unordered stack-allocated
770 VEC. */
771 if (i * 4 > alloc_count * 3)
772 vars = new hash_set<tree>;
774 /* Now go through the initial debug stmts in DEST again, this time
775 actually inserting in VARS or FEWVARS. Don't bother checking for
776 duplicates in FEWVARS. */
777 for (gimple_stmt_iterator si = gsi; !gsi_end_p (si); gsi_next (&si))
779 gimple *stmt = gsi_stmt (si);
780 if (!is_gimple_debug (stmt))
781 break;
783 tree var;
785 if (gimple_debug_bind_p (stmt))
786 var = gimple_debug_bind_get_var (stmt);
787 else if (gimple_debug_source_bind_p (stmt))
788 var = gimple_debug_source_bind_get_var (stmt);
789 else
790 gcc_unreachable ();
792 if (vars)
793 vars->add (var);
794 else
795 fewvars.quick_push (var);
798 basic_block bb = dest;
802 bb = single_pred (bb);
803 for (gimple_stmt_iterator si = gsi_last_bb (bb);
804 !gsi_end_p (si); gsi_prev (&si))
806 gimple *stmt = gsi_stmt (si);
807 if (!is_gimple_debug (stmt))
808 continue;
810 tree var;
812 if (gimple_debug_bind_p (stmt))
813 var = gimple_debug_bind_get_var (stmt);
814 else if (gimple_debug_source_bind_p (stmt))
815 var = gimple_debug_source_bind_get_var (stmt);
816 else
817 gcc_unreachable ();
819 /* Discard debug bind overlaps. ??? Unlike stmts from src,
820 copied into a new block that will precede BB, debug bind
821 stmts in bypassed BBs may actually be discarded if
822 they're overwritten by subsequent debug bind stmts, which
823 might be a problem once we introduce stmt frontier notes
824 or somesuch. Adding `&& bb == src' to the condition
825 below will preserve all potentially relevant debug
826 notes. */
827 if (vars && vars->add (var))
828 continue;
829 else if (!vars)
831 int i = fewvars.length ();
832 while (i--)
833 if (fewvars[i] == var)
834 break;
835 if (i >= 0)
836 continue;
838 if (fewvars.length () < (unsigned) alloc_count)
839 fewvars.quick_push (var);
840 else
842 vars = new hash_set<tree>;
843 for (i = 0; i < alloc_count; i++)
844 vars->add (fewvars[i]);
845 fewvars.release ();
846 vars->add (var);
850 stmt = gimple_copy (stmt);
851 /* ??? Should we drop the location of the copy to denote
852 they're artificial bindings? */
853 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
856 while (bb != src && single_pred_p (bb));
858 if (vars)
859 delete vars;
860 else if (fewvars.exists ())
861 fewvars.release ();
864 /* See if TAKEN_EDGE->dest is a threadable block with no side effecs (ie, it
865 need not be duplicated as part of the CFG/SSA updating process).
867 If it is threadable, add it to PATH and VISITED and recurse, ultimately
868 returning TRUE from the toplevel call. Otherwise do nothing and
869 return false.
871 DUMMY_COND, HANDLE_DOMINATING_ASSERTS and SIMPLIFY are used to
872 try and simplify the condition at the end of TAKEN_EDGE->dest.
874 The available expression table is referenced via AVAIL_EXPRS_STACK. */
876 static bool
877 thread_around_empty_blocks (edge taken_edge,
878 gcond *dummy_cond,
879 class avail_exprs_stack *avail_exprs_stack,
880 bool handle_dominating_asserts,
881 pfn_simplify simplify,
882 bitmap visited,
883 vec<jump_thread_edge *> *path)
885 basic_block bb = taken_edge->dest;
886 gimple_stmt_iterator gsi;
887 gimple *stmt;
888 tree cond;
890 /* The key property of these blocks is that they need not be duplicated
891 when threading. Thus they can not have visible side effects such
892 as PHI nodes. */
893 if (!gsi_end_p (gsi_start_phis (bb)))
894 return false;
896 /* Skip over DEBUG statements at the start of the block. */
897 gsi = gsi_start_nondebug_bb (bb);
899 /* If the block has no statements, but does have a single successor, then
900 it's just a forwarding block and we can thread through it trivially.
902 However, note that just threading through empty blocks with single
903 successors is not inherently profitable. For the jump thread to
904 be profitable, we must avoid a runtime conditional.
906 By taking the return value from the recursive call, we get the
907 desired effect of returning TRUE when we found a profitable jump
908 threading opportunity and FALSE otherwise.
910 This is particularly important when this routine is called after
911 processing a joiner block. Returning TRUE too aggressively in
912 that case results in pointless duplication of the joiner block. */
913 if (gsi_end_p (gsi))
915 if (single_succ_p (bb))
917 taken_edge = single_succ_edge (bb);
919 if ((taken_edge->flags & EDGE_DFS_BACK) != 0)
920 return false;
922 if (!bitmap_bit_p (visited, taken_edge->dest->index))
924 jump_thread_edge *x
925 = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
926 path->safe_push (x);
927 bitmap_set_bit (visited, taken_edge->dest->index);
928 return thread_around_empty_blocks (taken_edge,
929 dummy_cond,
930 avail_exprs_stack,
931 handle_dominating_asserts,
932 simplify,
933 visited,
934 path);
938 /* We have a block with no statements, but multiple successors? */
939 return false;
942 /* The only real statements this block can have are a control
943 flow altering statement. Anything else stops the thread. */
944 stmt = gsi_stmt (gsi);
945 if (gimple_code (stmt) != GIMPLE_COND
946 && gimple_code (stmt) != GIMPLE_GOTO
947 && gimple_code (stmt) != GIMPLE_SWITCH)
948 return false;
950 /* Extract and simplify the condition. */
951 cond = simplify_control_stmt_condition (taken_edge, stmt,
952 avail_exprs_stack, dummy_cond,
953 simplify, handle_dominating_asserts);
955 /* If the condition can be statically computed and we have not already
956 visited the destination edge, then add the taken edge to our thread
957 path. */
958 if (cond != NULL_TREE
959 && (is_gimple_min_invariant (cond)
960 || TREE_CODE (cond) == CASE_LABEL_EXPR))
962 if (TREE_CODE (cond) == CASE_LABEL_EXPR)
963 taken_edge = find_edge (bb, label_to_block (CASE_LABEL (cond)));
964 else
965 taken_edge = find_taken_edge (bb, cond);
967 if ((taken_edge->flags & EDGE_DFS_BACK) != 0)
968 return false;
970 if (bitmap_bit_p (visited, taken_edge->dest->index))
971 return false;
972 bitmap_set_bit (visited, taken_edge->dest->index);
974 jump_thread_edge *x
975 = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
976 path->safe_push (x);
978 thread_around_empty_blocks (taken_edge,
979 dummy_cond,
980 avail_exprs_stack,
981 handle_dominating_asserts,
982 simplify,
983 visited,
984 path);
985 return true;
988 return false;
991 /* We are exiting E->src, see if E->dest ends with a conditional
992 jump which has a known value when reached via E.
994 E->dest can have arbitrary side effects which, if threading is
995 successful, will be maintained.
997 Special care is necessary if E is a back edge in the CFG as we
998 may have already recorded equivalences for E->dest into our
999 various tables, including the result of the conditional at
1000 the end of E->dest. Threading opportunities are severely
1001 limited in that case to avoid short-circuiting the loop
1002 incorrectly.
1004 DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
1005 to avoid allocating memory.
1007 HANDLE_DOMINATING_ASSERTS is true if we should try to replace operands of
1008 the simplified condition with left-hand sides of ASSERT_EXPRs they are
1009 used in.
1011 STACK is used to undo temporary equivalences created during the walk of
1012 E->dest.
1014 SIMPLIFY is a pass-specific function used to simplify statements.
1016 Our caller is responsible for restoring the state of the expression
1017 and const_and_copies stacks.
1019 Positive return value is success. Zero return value is failure, but
1020 the block can still be duplicated as a joiner in a jump thread path,
1021 negative indicates the block should not be duplicated and thus is not
1022 suitable for a joiner in a jump threading path. */
1024 static int
1025 thread_through_normal_block (edge e,
1026 gcond *dummy_cond,
1027 bool handle_dominating_asserts,
1028 const_and_copies *const_and_copies,
1029 avail_exprs_stack *avail_exprs_stack,
1030 pfn_simplify simplify,
1031 vec<jump_thread_edge *> *path,
1032 bitmap visited)
1034 /* We want to record any equivalences created by traversing E. */
1035 if (!handle_dominating_asserts)
1036 record_temporary_equivalences (e, const_and_copies, avail_exprs_stack);
1038 /* PHIs create temporary equivalences.
1039 Note that if we found a PHI that made the block non-threadable, then
1040 we need to bubble that up to our caller in the same manner we do
1041 when we prematurely stop processing statements below. */
1042 if (!record_temporary_equivalences_from_phis (e, const_and_copies))
1043 return -1;
1045 /* Now walk each statement recording any context sensitive
1046 temporary equivalences we can detect. */
1047 gimple *stmt
1048 = record_temporary_equivalences_from_stmts_at_dest (e, const_and_copies,
1049 avail_exprs_stack,
1050 simplify);
1052 /* There's two reasons STMT might be null, and distinguishing
1053 between them is important.
1055 First the block may not have had any statements. For example, it
1056 might have some PHIs and unconditionally transfer control elsewhere.
1057 Such blocks are suitable for jump threading, particularly as a
1058 joiner block.
1060 The second reason would be if we did not process all the statements
1061 in the block (because there were too many to make duplicating the
1062 block profitable. If we did not look at all the statements, then
1063 we may not have invalidated everything needing invalidation. Thus
1064 we must signal to our caller that this block is not suitable for
1065 use as a joiner in a threading path. */
1066 if (!stmt)
1068 /* First case. The statement simply doesn't have any instructions, but
1069 does have PHIs. */
1070 if (gsi_end_p (gsi_start_nondebug_bb (e->dest))
1071 && !gsi_end_p (gsi_start_phis (e->dest)))
1072 return 0;
1074 /* Second case. */
1075 return -1;
1078 /* If we stopped at a COND_EXPR or SWITCH_EXPR, see if we know which arm
1079 will be taken. */
1080 if (gimple_code (stmt) == GIMPLE_COND
1081 || gimple_code (stmt) == GIMPLE_GOTO
1082 || gimple_code (stmt) == GIMPLE_SWITCH)
1084 tree cond;
1086 /* Extract and simplify the condition. */
1087 cond = simplify_control_stmt_condition (e, stmt, avail_exprs_stack,
1088 dummy_cond, simplify,
1089 handle_dominating_asserts);
1091 if (!cond)
1092 return 0;
1094 if (is_gimple_min_invariant (cond)
1095 || TREE_CODE (cond) == CASE_LABEL_EXPR)
1097 edge taken_edge;
1098 if (TREE_CODE (cond) == CASE_LABEL_EXPR)
1099 taken_edge = find_edge (e->dest,
1100 label_to_block (CASE_LABEL (cond)));
1101 else
1102 taken_edge = find_taken_edge (e->dest, cond);
1104 basic_block dest = (taken_edge ? taken_edge->dest : NULL);
1106 /* DEST could be NULL for a computed jump to an absolute
1107 address. */
1108 if (dest == NULL
1109 || dest == e->dest
1110 || (taken_edge->flags & EDGE_DFS_BACK) != 0
1111 || bitmap_bit_p (visited, dest->index))
1112 return 0;
1114 /* Only push the EDGE_START_JUMP_THREAD marker if this is
1115 first edge on the path. */
1116 if (path->length () == 0)
1118 jump_thread_edge *x
1119 = new jump_thread_edge (e, EDGE_START_JUMP_THREAD);
1120 path->safe_push (x);
1123 jump_thread_edge *x
1124 = new jump_thread_edge (taken_edge, EDGE_COPY_SRC_BLOCK);
1125 path->safe_push (x);
1127 /* See if we can thread through DEST as well, this helps capture
1128 secondary effects of threading without having to re-run DOM or
1129 VRP.
1131 We don't want to thread back to a block we have already
1132 visited. This may be overly conservative. */
1133 bitmap_set_bit (visited, dest->index);
1134 bitmap_set_bit (visited, e->dest->index);
1135 thread_around_empty_blocks (taken_edge,
1136 dummy_cond,
1137 avail_exprs_stack,
1138 handle_dominating_asserts,
1139 simplify,
1140 visited,
1141 path);
1142 return 1;
1145 return 0;
1148 /* We are exiting E->src, see if E->dest ends with a conditional
1149 jump which has a known value when reached via E.
1151 DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
1152 to avoid allocating memory.
1154 HANDLE_DOMINATING_ASSERTS is true if we should try to replace operands of
1155 the simplified condition with left-hand sides of ASSERT_EXPRs they are
1156 used in.
1158 CONST_AND_COPIES is used to undo temporary equivalences created during the
1159 walk of E->dest.
1161 The available expression table is referenced vai AVAIL_EXPRS_STACK.
1163 SIMPLIFY is a pass-specific function used to simplify statements. */
1165 void
1166 thread_across_edge (gcond *dummy_cond,
1167 edge e,
1168 bool handle_dominating_asserts,
1169 class const_and_copies *const_and_copies,
1170 class avail_exprs_stack *avail_exprs_stack,
1171 tree (*simplify) (gimple *, gimple *,
1172 class avail_exprs_stack *))
1174 bitmap visited = BITMAP_ALLOC (NULL);
1176 stmt_count = 0;
1178 vec<jump_thread_edge *> *path = new vec<jump_thread_edge *> ();
1179 bitmap_clear (visited);
1180 bitmap_set_bit (visited, e->src->index);
1181 bitmap_set_bit (visited, e->dest->index);
1183 int threaded;
1184 if ((e->flags & EDGE_DFS_BACK) == 0)
1185 threaded = thread_through_normal_block (e, dummy_cond,
1186 handle_dominating_asserts,
1187 const_and_copies,
1188 avail_exprs_stack,
1189 simplify, path,
1190 visited);
1191 else
1192 threaded = 0;
1194 if (threaded > 0)
1196 propagate_threaded_block_debug_into (path->last ()->e->dest,
1197 e->dest);
1198 const_and_copies->pop_to_marker ();
1199 BITMAP_FREE (visited);
1200 register_jump_thread (path);
1201 return;
1203 else
1205 /* Negative and zero return values indicate no threading was possible,
1206 thus there should be no edges on the thread path and no need to walk
1207 through the vector entries. */
1208 gcc_assert (path->length () == 0);
1209 path->release ();
1210 delete path;
1212 /* A negative status indicates the target block was deemed too big to
1213 duplicate. Just quit now rather than trying to use the block as
1214 a joiner in a jump threading path.
1216 This prevents unnecessary code growth, but more importantly if we
1217 do not look at all the statements in the block, then we may have
1218 missed some invalidations if we had traversed a backedge! */
1219 if (threaded < 0)
1221 BITMAP_FREE (visited);
1222 const_and_copies->pop_to_marker ();
1223 return;
1227 /* We were unable to determine what out edge from E->dest is taken. However,
1228 we might still be able to thread through successors of E->dest. This
1229 often occurs when E->dest is a joiner block which then fans back out
1230 based on redundant tests.
1232 If so, we'll copy E->dest and redirect the appropriate predecessor to
1233 the copy. Within the copy of E->dest, we'll thread one or more edges
1234 to points deeper in the CFG.
1236 This is a stopgap until we have a more structured approach to path
1237 isolation. */
1239 edge taken_edge;
1240 edge_iterator ei;
1241 bool found;
1243 /* If E->dest has abnormal outgoing edges, then there's no guarantee
1244 we can safely redirect any of the edges. Just punt those cases. */
1245 FOR_EACH_EDGE (taken_edge, ei, e->dest->succs)
1246 if (taken_edge->flags & EDGE_ABNORMAL)
1248 const_and_copies->pop_to_marker ();
1249 BITMAP_FREE (visited);
1250 return;
1253 /* Look at each successor of E->dest to see if we can thread through it. */
1254 FOR_EACH_EDGE (taken_edge, ei, e->dest->succs)
1256 if ((e->flags & EDGE_DFS_BACK) != 0
1257 || (taken_edge->flags & EDGE_DFS_BACK) != 0)
1258 continue;
1260 /* Push a fresh marker so we can unwind the equivalences created
1261 for each of E->dest's successors. */
1262 const_and_copies->push_marker ();
1263 if (avail_exprs_stack)
1264 avail_exprs_stack->push_marker ();
1266 /* Avoid threading to any block we have already visited. */
1267 bitmap_clear (visited);
1268 bitmap_set_bit (visited, e->src->index);
1269 bitmap_set_bit (visited, e->dest->index);
1270 bitmap_set_bit (visited, taken_edge->dest->index);
1271 vec<jump_thread_edge *> *path = new vec<jump_thread_edge *> ();
1273 /* Record whether or not we were able to thread through a successor
1274 of E->dest. */
1275 jump_thread_edge *x = new jump_thread_edge (e, EDGE_START_JUMP_THREAD);
1276 path->safe_push (x);
1278 x = new jump_thread_edge (taken_edge, EDGE_COPY_SRC_JOINER_BLOCK);
1279 path->safe_push (x);
1280 found = false;
1281 found = thread_around_empty_blocks (taken_edge,
1282 dummy_cond,
1283 avail_exprs_stack,
1284 handle_dominating_asserts,
1285 simplify,
1286 visited,
1287 path);
1289 if (!found)
1290 found = thread_through_normal_block (path->last ()->e, dummy_cond,
1291 handle_dominating_asserts,
1292 const_and_copies,
1293 avail_exprs_stack,
1294 simplify, path,
1295 visited) > 0;
1297 /* If we were able to thread through a successor of E->dest, then
1298 record the jump threading opportunity. */
1299 if (found)
1301 propagate_threaded_block_debug_into (path->last ()->e->dest,
1302 taken_edge->dest);
1303 register_jump_thread (path);
1305 else
1306 delete_jump_thread_path (path);
1308 /* And unwind the equivalence table. */
1309 if (avail_exprs_stack)
1310 avail_exprs_stack->pop_to_marker ();
1311 const_and_copies->pop_to_marker ();
1313 BITMAP_FREE (visited);
1316 const_and_copies->pop_to_marker ();