* testsuite/26_numerics/headers/cmath/hypot.cc: XFAIL on AIX.
[official-gcc.git] / gcc / tree-ssa-threadedge.c
blob534292c19e5a4ce3c6b0ae129adcacdc9635bc89
1 /* SSA Jump Threading
2 Copyright (C) 2005-2016 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 (!cached_lhs
332 || (TREE_CODE (cached_lhs) != SSA_NAME
333 && !is_gimple_min_invariant (cached_lhs)))
335 /* We're going to temporarily copy propagate the operands
336 and see if that allows us to simplify this statement. */
337 tree *copy;
338 ssa_op_iter iter;
339 use_operand_p use_p;
340 unsigned int num, i = 0;
342 num = NUM_SSA_OPERANDS (stmt, SSA_OP_ALL_USES);
343 copy = XALLOCAVEC (tree, num);
345 /* Make a copy of the uses & vuses into USES_COPY, then cprop into
346 the operands. */
347 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
349 tree tmp = NULL;
350 tree use = USE_FROM_PTR (use_p);
352 copy[i++] = use;
353 if (TREE_CODE (use) == SSA_NAME)
354 tmp = SSA_NAME_VALUE (use);
355 if (tmp)
356 SET_USE (use_p, tmp);
359 cached_lhs = (*simplify) (stmt, stmt, avail_exprs_stack);
361 /* Restore the statement's original uses/defs. */
362 i = 0;
363 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
364 SET_USE (use_p, copy[i++]);
368 /* Record the context sensitive equivalence if we were able
369 to simplify this statement. */
370 if (cached_lhs
371 && (TREE_CODE (cached_lhs) == SSA_NAME
372 || is_gimple_min_invariant (cached_lhs)))
373 const_and_copies->record_const_or_copy (gimple_get_lhs (stmt),
374 cached_lhs);
376 return stmt;
379 static tree simplify_control_stmt_condition_1 (edge, gimple *,
380 class avail_exprs_stack *,
381 tree, enum tree_code, tree,
382 gcond *, pfn_simplify, bool,
383 unsigned);
385 /* Simplify the control statement at the end of the block E->dest.
387 To avoid allocating memory unnecessarily, a scratch GIMPLE_COND
388 is available to use/clobber in DUMMY_COND.
390 Use SIMPLIFY (a pointer to a callback function) to further simplify
391 a condition using pass specific information.
393 Return the simplified condition or NULL if simplification could
394 not be performed. When simplifying a GIMPLE_SWITCH, we may return
395 the CASE_LABEL_EXPR that will be taken.
397 The available expression table is referenced via AVAIL_EXPRS_STACK. */
399 static tree
400 simplify_control_stmt_condition (edge e,
401 gimple *stmt,
402 class avail_exprs_stack *avail_exprs_stack,
403 gcond *dummy_cond,
404 pfn_simplify simplify,
405 bool handle_dominating_asserts)
407 tree cond, cached_lhs;
408 enum gimple_code code = gimple_code (stmt);
410 /* For comparisons, we have to update both operands, then try
411 to simplify the comparison. */
412 if (code == GIMPLE_COND)
414 tree op0, op1;
415 enum tree_code cond_code;
417 op0 = gimple_cond_lhs (stmt);
418 op1 = gimple_cond_rhs (stmt);
419 cond_code = gimple_cond_code (stmt);
421 /* Get the current value of both operands. */
422 if (TREE_CODE (op0) == SSA_NAME)
424 for (int i = 0; i < 2; i++)
426 if (TREE_CODE (op0) == SSA_NAME
427 && SSA_NAME_VALUE (op0))
428 op0 = SSA_NAME_VALUE (op0);
429 else
430 break;
434 if (TREE_CODE (op1) == SSA_NAME)
436 for (int i = 0; i < 2; i++)
438 if (TREE_CODE (op1) == SSA_NAME
439 && SSA_NAME_VALUE (op1))
440 op1 = SSA_NAME_VALUE (op1);
441 else
442 break;
446 const unsigned recursion_limit = 4;
448 cached_lhs
449 = simplify_control_stmt_condition_1 (e, stmt, avail_exprs_stack,
450 op0, cond_code, op1,
451 dummy_cond, simplify,
452 handle_dominating_asserts,
453 recursion_limit);
455 /* If we were testing an integer/pointer against a constant, then
456 we can use the FSM code to trace the value of the SSA_NAME. If
457 a value is found, then the condition will collapse to a constant.
459 Return the SSA_NAME we want to trace back rather than the full
460 expression and give the FSM threader a chance to find its value. */
461 if (cached_lhs == NULL)
463 /* Recover the original operands. They may have been simplified
464 using context sensitive equivalences. Those context sensitive
465 equivalences may not be valid on paths found by the FSM optimizer. */
466 tree op0 = gimple_cond_lhs (stmt);
467 tree op1 = gimple_cond_rhs (stmt);
469 if ((INTEGRAL_TYPE_P (TREE_TYPE (op0))
470 || POINTER_TYPE_P (TREE_TYPE (op0)))
471 && TREE_CODE (op0) == SSA_NAME
472 && TREE_CODE (op1) == INTEGER_CST)
473 return op0;
476 return cached_lhs;
479 if (code == GIMPLE_SWITCH)
480 cond = gimple_switch_index (as_a <gswitch *> (stmt));
481 else if (code == GIMPLE_GOTO)
482 cond = gimple_goto_dest (stmt);
483 else
484 gcc_unreachable ();
486 /* We can have conditionals which just test the state of a variable
487 rather than use a relational operator. These are simpler to handle. */
488 if (TREE_CODE (cond) == SSA_NAME)
490 tree original_lhs = cond;
491 cached_lhs = cond;
493 /* Get the variable's current value from the equivalence chains.
495 It is possible to get loops in the SSA_NAME_VALUE chains
496 (consider threading the backedge of a loop where we have
497 a loop invariant SSA_NAME used in the condition). */
498 if (cached_lhs)
500 for (int i = 0; i < 2; i++)
502 if (TREE_CODE (cached_lhs) == SSA_NAME
503 && SSA_NAME_VALUE (cached_lhs))
504 cached_lhs = SSA_NAME_VALUE (cached_lhs);
505 else
506 break;
510 /* If we're dominated by a suitable ASSERT_EXPR, then
511 update CACHED_LHS appropriately. */
512 if (handle_dominating_asserts && TREE_CODE (cached_lhs) == SSA_NAME)
513 cached_lhs = lhs_of_dominating_assert (cached_lhs, e->src, stmt);
515 /* If we haven't simplified to an invariant yet, then use the
516 pass specific callback to try and simplify it further. */
517 if (cached_lhs && ! is_gimple_min_invariant (cached_lhs))
519 if (handle_dominating_asserts && code == GIMPLE_SWITCH)
521 /* Replace the index operand of the GIMPLE_SWITCH with the
522 dominating ASSERT_EXPR before handing it off to VRP. If
523 simplification is possible, the simplified value will be a
524 CASE_LABEL_EXPR of the label that is proven to be taken. */
525 gswitch *dummy_switch = as_a<gswitch *> (gimple_copy (stmt));
526 gimple_switch_set_index (dummy_switch, cached_lhs);
527 cached_lhs = (*simplify) (dummy_switch, stmt, avail_exprs_stack);
528 ggc_free (dummy_switch);
530 else
531 cached_lhs = (*simplify) (stmt, stmt, avail_exprs_stack);
534 /* We couldn't find an invariant. But, callers of this
535 function may be able to do something useful with the
536 unmodified destination. */
537 if (!cached_lhs)
538 cached_lhs = original_lhs;
540 else
541 cached_lhs = NULL;
543 return cached_lhs;
546 /* Recursive helper for simplify_control_stmt_condition. */
548 static tree
549 simplify_control_stmt_condition_1 (edge e,
550 gimple *stmt,
551 class avail_exprs_stack *avail_exprs_stack,
552 tree op0,
553 enum tree_code cond_code,
554 tree op1,
555 gcond *dummy_cond,
556 pfn_simplify simplify,
557 bool handle_dominating_asserts,
558 unsigned limit)
560 if (limit == 0)
561 return NULL_TREE;
563 /* We may need to canonicalize the comparison. For
564 example, op0 might be a constant while op1 is an
565 SSA_NAME. Failure to canonicalize will cause us to
566 miss threading opportunities. */
567 if (tree_swap_operands_p (op0, op1))
569 cond_code = swap_tree_comparison (cond_code);
570 std::swap (op0, op1);
573 /* If the condition has the form (A & B) CMP 0 or (A | B) CMP 0 then
574 recurse into the LHS to see if there is a dominating ASSERT_EXPR
575 of A or of B that makes this condition always true or always false
576 along the edge E. */
577 if (handle_dominating_asserts
578 && (cond_code == EQ_EXPR || cond_code == NE_EXPR)
579 && TREE_CODE (op0) == SSA_NAME
580 && integer_zerop (op1))
582 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
583 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
585 else if (gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR
586 || gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR)
588 enum tree_code rhs_code = gimple_assign_rhs_code (def_stmt);
589 const tree rhs1 = gimple_assign_rhs1 (def_stmt);
590 const tree rhs2 = gimple_assign_rhs2 (def_stmt);
592 /* Is A != 0 ? */
593 const tree res1
594 = simplify_control_stmt_condition_1 (e, def_stmt, avail_exprs_stack,
595 rhs1, NE_EXPR, op1,
596 dummy_cond, simplify,
597 handle_dominating_asserts,
598 limit - 1);
599 if (res1 == NULL_TREE)
601 else if (rhs_code == BIT_AND_EXPR && integer_zerop (res1))
603 /* If A == 0 then (A & B) != 0 is always false. */
604 if (cond_code == NE_EXPR)
605 return boolean_false_node;
606 /* If A == 0 then (A & B) == 0 is always true. */
607 if (cond_code == EQ_EXPR)
608 return boolean_true_node;
610 else if (rhs_code == BIT_IOR_EXPR && integer_nonzerop (res1))
612 /* If A != 0 then (A | B) != 0 is always true. */
613 if (cond_code == NE_EXPR)
614 return boolean_true_node;
615 /* If A != 0 then (A | B) == 0 is always false. */
616 if (cond_code == EQ_EXPR)
617 return boolean_false_node;
620 /* Is B != 0 ? */
621 const tree res2
622 = simplify_control_stmt_condition_1 (e, def_stmt, avail_exprs_stack,
623 rhs2, NE_EXPR, op1,
624 dummy_cond, simplify,
625 handle_dominating_asserts,
626 limit - 1);
627 if (res2 == NULL_TREE)
629 else if (rhs_code == BIT_AND_EXPR && integer_zerop (res2))
631 /* If B == 0 then (A & B) != 0 is always false. */
632 if (cond_code == NE_EXPR)
633 return boolean_false_node;
634 /* If B == 0 then (A & B) == 0 is always true. */
635 if (cond_code == EQ_EXPR)
636 return boolean_true_node;
638 else if (rhs_code == BIT_IOR_EXPR && integer_nonzerop (res2))
640 /* If B != 0 then (A | B) != 0 is always true. */
641 if (cond_code == NE_EXPR)
642 return boolean_true_node;
643 /* If B != 0 then (A | B) == 0 is always false. */
644 if (cond_code == EQ_EXPR)
645 return boolean_false_node;
648 if (res1 != NULL_TREE && res2 != NULL_TREE)
650 if (rhs_code == BIT_AND_EXPR
651 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
652 && integer_nonzerop (res1)
653 && integer_nonzerop (res2))
655 /* If A != 0 and B != 0 then (bool)(A & B) != 0 is true. */
656 if (cond_code == NE_EXPR)
657 return boolean_true_node;
658 /* If A != 0 and B != 0 then (bool)(A & B) == 0 is false. */
659 if (cond_code == EQ_EXPR)
660 return boolean_false_node;
663 if (rhs_code == BIT_IOR_EXPR
664 && integer_zerop (res1)
665 && integer_zerop (res2))
667 /* If A == 0 and B == 0 then (A | B) != 0 is false. */
668 if (cond_code == NE_EXPR)
669 return boolean_false_node;
670 /* If A == 0 and B == 0 then (A | B) == 0 is true. */
671 if (cond_code == EQ_EXPR)
672 return boolean_true_node;
676 /* Handle (A CMP B) CMP 0. */
677 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt))
678 == tcc_comparison)
680 tree rhs1 = gimple_assign_rhs1 (def_stmt);
681 tree rhs2 = gimple_assign_rhs2 (def_stmt);
683 tree_code new_cond = gimple_assign_rhs_code (def_stmt);
684 if (cond_code == EQ_EXPR)
685 new_cond = invert_tree_comparison (new_cond, false);
687 tree res
688 = simplify_control_stmt_condition_1 (e, def_stmt, avail_exprs_stack,
689 rhs1, new_cond, rhs2,
690 dummy_cond, simplify,
691 handle_dominating_asserts,
692 limit - 1);
693 if (res != NULL_TREE && is_gimple_min_invariant (res))
694 return res;
698 if (handle_dominating_asserts)
700 /* Now see if the operand was consumed by an ASSERT_EXPR
701 which dominates E->src. If so, we want to replace the
702 operand with the LHS of the ASSERT_EXPR. */
703 if (TREE_CODE (op0) == SSA_NAME)
704 op0 = lhs_of_dominating_assert (op0, e->src, stmt);
706 if (TREE_CODE (op1) == SSA_NAME)
707 op1 = lhs_of_dominating_assert (op1, e->src, stmt);
710 gimple_cond_set_code (dummy_cond, cond_code);
711 gimple_cond_set_lhs (dummy_cond, op0);
712 gimple_cond_set_rhs (dummy_cond, op1);
714 /* We absolutely do not care about any type conversions
715 we only care about a zero/nonzero value. */
716 fold_defer_overflow_warnings ();
718 tree res = fold_binary (cond_code, boolean_type_node, op0, op1);
719 if (res)
720 while (CONVERT_EXPR_P (res))
721 res = TREE_OPERAND (res, 0);
723 fold_undefer_overflow_warnings ((res && is_gimple_min_invariant (res)),
724 stmt, WARN_STRICT_OVERFLOW_CONDITIONAL);
726 /* If we have not simplified the condition down to an invariant,
727 then use the pass specific callback to simplify the condition. */
728 if (!res
729 || !is_gimple_min_invariant (res))
730 res = (*simplify) (dummy_cond, stmt, avail_exprs_stack);
732 return res;
735 /* Copy debug stmts from DEST's chain of single predecessors up to
736 SRC, so that we don't lose the bindings as PHI nodes are introduced
737 when DEST gains new predecessors. */
738 void
739 propagate_threaded_block_debug_into (basic_block dest, basic_block src)
741 if (!MAY_HAVE_DEBUG_STMTS)
742 return;
744 if (!single_pred_p (dest))
745 return;
747 gcc_checking_assert (dest != src);
749 gimple_stmt_iterator gsi = gsi_after_labels (dest);
750 int i = 0;
751 const int alloc_count = 16; // ?? Should this be a PARAM?
753 /* Estimate the number of debug vars overridden in the beginning of
754 DEST, to tell how many we're going to need to begin with. */
755 for (gimple_stmt_iterator si = gsi;
756 i * 4 <= alloc_count * 3 && !gsi_end_p (si); gsi_next (&si))
758 gimple *stmt = gsi_stmt (si);
759 if (!is_gimple_debug (stmt))
760 break;
761 i++;
764 auto_vec<tree, alloc_count> fewvars;
765 hash_set<tree> *vars = NULL;
767 /* If we're already starting with 3/4 of alloc_count, go for a
768 hash_set, otherwise start with an unordered stack-allocated
769 VEC. */
770 if (i * 4 > alloc_count * 3)
771 vars = new hash_set<tree>;
773 /* Now go through the initial debug stmts in DEST again, this time
774 actually inserting in VARS or FEWVARS. Don't bother checking for
775 duplicates in FEWVARS. */
776 for (gimple_stmt_iterator si = gsi; !gsi_end_p (si); gsi_next (&si))
778 gimple *stmt = gsi_stmt (si);
779 if (!is_gimple_debug (stmt))
780 break;
782 tree var;
784 if (gimple_debug_bind_p (stmt))
785 var = gimple_debug_bind_get_var (stmt);
786 else if (gimple_debug_source_bind_p (stmt))
787 var = gimple_debug_source_bind_get_var (stmt);
788 else
789 gcc_unreachable ();
791 if (vars)
792 vars->add (var);
793 else
794 fewvars.quick_push (var);
797 basic_block bb = dest;
801 bb = single_pred (bb);
802 for (gimple_stmt_iterator si = gsi_last_bb (bb);
803 !gsi_end_p (si); gsi_prev (&si))
805 gimple *stmt = gsi_stmt (si);
806 if (!is_gimple_debug (stmt))
807 continue;
809 tree var;
811 if (gimple_debug_bind_p (stmt))
812 var = gimple_debug_bind_get_var (stmt);
813 else if (gimple_debug_source_bind_p (stmt))
814 var = gimple_debug_source_bind_get_var (stmt);
815 else
816 gcc_unreachable ();
818 /* Discard debug bind overlaps. ??? Unlike stmts from src,
819 copied into a new block that will precede BB, debug bind
820 stmts in bypassed BBs may actually be discarded if
821 they're overwritten by subsequent debug bind stmts, which
822 might be a problem once we introduce stmt frontier notes
823 or somesuch. Adding `&& bb == src' to the condition
824 below will preserve all potentially relevant debug
825 notes. */
826 if (vars && vars->add (var))
827 continue;
828 else if (!vars)
830 int i = fewvars.length ();
831 while (i--)
832 if (fewvars[i] == var)
833 break;
834 if (i >= 0)
835 continue;
837 if (fewvars.length () < (unsigned) alloc_count)
838 fewvars.quick_push (var);
839 else
841 vars = new hash_set<tree>;
842 for (i = 0; i < alloc_count; i++)
843 vars->add (fewvars[i]);
844 fewvars.release ();
845 vars->add (var);
849 stmt = gimple_copy (stmt);
850 /* ??? Should we drop the location of the copy to denote
851 they're artificial bindings? */
852 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
855 while (bb != src && single_pred_p (bb));
857 if (vars)
858 delete vars;
859 else if (fewvars.exists ())
860 fewvars.release ();
863 /* See if TAKEN_EDGE->dest is a threadable block with no side effecs (ie, it
864 need not be duplicated as part of the CFG/SSA updating process).
866 If it is threadable, add it to PATH and VISITED and recurse, ultimately
867 returning TRUE from the toplevel call. Otherwise do nothing and
868 return false.
870 DUMMY_COND, HANDLE_DOMINATING_ASSERTS and SIMPLIFY are used to
871 try and simplify the condition at the end of TAKEN_EDGE->dest.
873 The available expression table is referenced via AVAIL_EXPRS_STACK. */
875 static bool
876 thread_around_empty_blocks (edge taken_edge,
877 gcond *dummy_cond,
878 class avail_exprs_stack *avail_exprs_stack,
879 bool handle_dominating_asserts,
880 pfn_simplify simplify,
881 bitmap visited,
882 vec<jump_thread_edge *> *path)
884 basic_block bb = taken_edge->dest;
885 gimple_stmt_iterator gsi;
886 gimple *stmt;
887 tree cond;
889 /* The key property of these blocks is that they need not be duplicated
890 when threading. Thus they can not have visible side effects such
891 as PHI nodes. */
892 if (!gsi_end_p (gsi_start_phis (bb)))
893 return false;
895 /* Skip over DEBUG statements at the start of the block. */
896 gsi = gsi_start_nondebug_bb (bb);
898 /* If the block has no statements, but does have a single successor, then
899 it's just a forwarding block and we can thread through it trivially.
901 However, note that just threading through empty blocks with single
902 successors is not inherently profitable. For the jump thread to
903 be profitable, we must avoid a runtime conditional.
905 By taking the return value from the recursive call, we get the
906 desired effect of returning TRUE when we found a profitable jump
907 threading opportunity and FALSE otherwise.
909 This is particularly important when this routine is called after
910 processing a joiner block. Returning TRUE too aggressively in
911 that case results in pointless duplication of the joiner block. */
912 if (gsi_end_p (gsi))
914 if (single_succ_p (bb))
916 taken_edge = single_succ_edge (bb);
918 if ((taken_edge->flags & EDGE_DFS_BACK) != 0)
919 return false;
921 if (!bitmap_bit_p (visited, taken_edge->dest->index))
923 jump_thread_edge *x
924 = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
925 path->safe_push (x);
926 bitmap_set_bit (visited, taken_edge->dest->index);
927 return thread_around_empty_blocks (taken_edge,
928 dummy_cond,
929 avail_exprs_stack,
930 handle_dominating_asserts,
931 simplify,
932 visited,
933 path);
937 /* We have a block with no statements, but multiple successors? */
938 return false;
941 /* The only real statements this block can have are a control
942 flow altering statement. Anything else stops the thread. */
943 stmt = gsi_stmt (gsi);
944 if (gimple_code (stmt) != GIMPLE_COND
945 && gimple_code (stmt) != GIMPLE_GOTO
946 && gimple_code (stmt) != GIMPLE_SWITCH)
947 return false;
949 /* Extract and simplify the condition. */
950 cond = simplify_control_stmt_condition (taken_edge, stmt,
951 avail_exprs_stack, dummy_cond,
952 simplify, handle_dominating_asserts);
954 /* If the condition can be statically computed and we have not already
955 visited the destination edge, then add the taken edge to our thread
956 path. */
957 if (cond != NULL_TREE
958 && (is_gimple_min_invariant (cond)
959 || TREE_CODE (cond) == CASE_LABEL_EXPR))
961 if (TREE_CODE (cond) == CASE_LABEL_EXPR)
962 taken_edge = find_edge (bb, label_to_block (CASE_LABEL (cond)));
963 else
964 taken_edge = find_taken_edge (bb, cond);
966 if ((taken_edge->flags & EDGE_DFS_BACK) != 0)
967 return false;
969 if (bitmap_bit_p (visited, taken_edge->dest->index))
970 return false;
971 bitmap_set_bit (visited, taken_edge->dest->index);
973 jump_thread_edge *x
974 = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
975 path->safe_push (x);
977 thread_around_empty_blocks (taken_edge,
978 dummy_cond,
979 avail_exprs_stack,
980 handle_dominating_asserts,
981 simplify,
982 visited,
983 path);
984 return true;
987 return false;
990 /* We are exiting E->src, see if E->dest ends with a conditional
991 jump which has a known value when reached via E.
993 E->dest can have arbitrary side effects which, if threading is
994 successful, will be maintained.
996 Special care is necessary if E is a back edge in the CFG as we
997 may have already recorded equivalences for E->dest into our
998 various tables, including the result of the conditional at
999 the end of E->dest. Threading opportunities are severely
1000 limited in that case to avoid short-circuiting the loop
1001 incorrectly.
1003 DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
1004 to avoid allocating memory.
1006 HANDLE_DOMINATING_ASSERTS is true if we should try to replace operands of
1007 the simplified condition with left-hand sides of ASSERT_EXPRs they are
1008 used in.
1010 STACK is used to undo temporary equivalences created during the walk of
1011 E->dest.
1013 SIMPLIFY is a pass-specific function used to simplify statements.
1015 Our caller is responsible for restoring the state of the expression
1016 and const_and_copies stacks.
1018 Positive return value is success. Zero return value is failure, but
1019 the block can still be duplicated as a joiner in a jump thread path,
1020 negative indicates the block should not be duplicated and thus is not
1021 suitable for a joiner in a jump threading path. */
1023 static int
1024 thread_through_normal_block (edge e,
1025 gcond *dummy_cond,
1026 bool handle_dominating_asserts,
1027 const_and_copies *const_and_copies,
1028 avail_exprs_stack *avail_exprs_stack,
1029 pfn_simplify simplify,
1030 vec<jump_thread_edge *> *path,
1031 bitmap visited)
1033 /* We want to record any equivalences created by traversing E. */
1034 if (!handle_dominating_asserts)
1035 record_temporary_equivalences (e, const_and_copies, avail_exprs_stack);
1037 /* PHIs create temporary equivalences.
1038 Note that if we found a PHI that made the block non-threadable, then
1039 we need to bubble that up to our caller in the same manner we do
1040 when we prematurely stop processing statements below. */
1041 if (!record_temporary_equivalences_from_phis (e, const_and_copies))
1042 return -1;
1044 /* Now walk each statement recording any context sensitive
1045 temporary equivalences we can detect. */
1046 gimple *stmt
1047 = record_temporary_equivalences_from_stmts_at_dest (e, const_and_copies,
1048 avail_exprs_stack,
1049 simplify);
1051 /* There's two reasons STMT might be null, and distinguishing
1052 between them is important.
1054 First the block may not have had any statements. For example, it
1055 might have some PHIs and unconditionally transfer control elsewhere.
1056 Such blocks are suitable for jump threading, particularly as a
1057 joiner block.
1059 The second reason would be if we did not process all the statements
1060 in the block (because there were too many to make duplicating the
1061 block profitable. If we did not look at all the statements, then
1062 we may not have invalidated everything needing invalidation. Thus
1063 we must signal to our caller that this block is not suitable for
1064 use as a joiner in a threading path. */
1065 if (!stmt)
1067 /* First case. The statement simply doesn't have any instructions, but
1068 does have PHIs. */
1069 if (gsi_end_p (gsi_start_nondebug_bb (e->dest))
1070 && !gsi_end_p (gsi_start_phis (e->dest)))
1071 return 0;
1073 /* Second case. */
1074 return -1;
1077 /* If we stopped at a COND_EXPR or SWITCH_EXPR, see if we know which arm
1078 will be taken. */
1079 if (gimple_code (stmt) == GIMPLE_COND
1080 || gimple_code (stmt) == GIMPLE_GOTO
1081 || gimple_code (stmt) == GIMPLE_SWITCH)
1083 tree cond;
1085 /* Extract and simplify the condition. */
1086 cond = simplify_control_stmt_condition (e, stmt, avail_exprs_stack,
1087 dummy_cond, simplify,
1088 handle_dominating_asserts);
1090 if (!cond)
1091 return 0;
1093 if (is_gimple_min_invariant (cond)
1094 || TREE_CODE (cond) == CASE_LABEL_EXPR)
1096 edge taken_edge;
1097 if (TREE_CODE (cond) == CASE_LABEL_EXPR)
1098 taken_edge = find_edge (e->dest,
1099 label_to_block (CASE_LABEL (cond)));
1100 else
1101 taken_edge = find_taken_edge (e->dest, cond);
1103 basic_block dest = (taken_edge ? taken_edge->dest : NULL);
1105 /* DEST could be NULL for a computed jump to an absolute
1106 address. */
1107 if (dest == NULL
1108 || dest == e->dest
1109 || (taken_edge->flags & EDGE_DFS_BACK) != 0
1110 || bitmap_bit_p (visited, dest->index))
1111 return 0;
1113 /* Only push the EDGE_START_JUMP_THREAD marker if this is
1114 first edge on the path. */
1115 if (path->length () == 0)
1117 jump_thread_edge *x
1118 = new jump_thread_edge (e, EDGE_START_JUMP_THREAD);
1119 path->safe_push (x);
1122 jump_thread_edge *x
1123 = new jump_thread_edge (taken_edge, EDGE_COPY_SRC_BLOCK);
1124 path->safe_push (x);
1126 /* See if we can thread through DEST as well, this helps capture
1127 secondary effects of threading without having to re-run DOM or
1128 VRP.
1130 We don't want to thread back to a block we have already
1131 visited. This may be overly conservative. */
1132 bitmap_set_bit (visited, dest->index);
1133 bitmap_set_bit (visited, e->dest->index);
1134 thread_around_empty_blocks (taken_edge,
1135 dummy_cond,
1136 avail_exprs_stack,
1137 handle_dominating_asserts,
1138 simplify,
1139 visited,
1140 path);
1141 return 1;
1144 return 0;
1147 /* We are exiting E->src, see if E->dest ends with a conditional
1148 jump which has a known value when reached via E.
1150 DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
1151 to avoid allocating memory.
1153 HANDLE_DOMINATING_ASSERTS is true if we should try to replace operands of
1154 the simplified condition with left-hand sides of ASSERT_EXPRs they are
1155 used in.
1157 CONST_AND_COPIES is used to undo temporary equivalences created during the
1158 walk of E->dest.
1160 The available expression table is referenced vai AVAIL_EXPRS_STACK.
1162 SIMPLIFY is a pass-specific function used to simplify statements. */
1164 void
1165 thread_across_edge (gcond *dummy_cond,
1166 edge e,
1167 bool handle_dominating_asserts,
1168 class const_and_copies *const_and_copies,
1169 class avail_exprs_stack *avail_exprs_stack,
1170 tree (*simplify) (gimple *, gimple *,
1171 class avail_exprs_stack *))
1173 bitmap visited = BITMAP_ALLOC (NULL);
1175 stmt_count = 0;
1177 vec<jump_thread_edge *> *path = new vec<jump_thread_edge *> ();
1178 bitmap_clear (visited);
1179 bitmap_set_bit (visited, e->src->index);
1180 bitmap_set_bit (visited, e->dest->index);
1182 int threaded;
1183 if ((e->flags & EDGE_DFS_BACK) == 0)
1184 threaded = thread_through_normal_block (e, dummy_cond,
1185 handle_dominating_asserts,
1186 const_and_copies,
1187 avail_exprs_stack,
1188 simplify, path,
1189 visited);
1190 else
1191 threaded = 0;
1193 if (threaded > 0)
1195 propagate_threaded_block_debug_into (path->last ()->e->dest,
1196 e->dest);
1197 const_and_copies->pop_to_marker ();
1198 BITMAP_FREE (visited);
1199 register_jump_thread (path);
1200 return;
1202 else
1204 /* Negative and zero return values indicate no threading was possible,
1205 thus there should be no edges on the thread path and no need to walk
1206 through the vector entries. */
1207 gcc_assert (path->length () == 0);
1208 path->release ();
1209 delete path;
1211 /* A negative status indicates the target block was deemed too big to
1212 duplicate. Just quit now rather than trying to use the block as
1213 a joiner in a jump threading path.
1215 This prevents unnecessary code growth, but more importantly if we
1216 do not look at all the statements in the block, then we may have
1217 missed some invalidations if we had traversed a backedge! */
1218 if (threaded < 0)
1220 BITMAP_FREE (visited);
1221 const_and_copies->pop_to_marker ();
1222 return;
1226 /* We were unable to determine what out edge from E->dest is taken. However,
1227 we might still be able to thread through successors of E->dest. This
1228 often occurs when E->dest is a joiner block which then fans back out
1229 based on redundant tests.
1231 If so, we'll copy E->dest and redirect the appropriate predecessor to
1232 the copy. Within the copy of E->dest, we'll thread one or more edges
1233 to points deeper in the CFG.
1235 This is a stopgap until we have a more structured approach to path
1236 isolation. */
1238 edge taken_edge;
1239 edge_iterator ei;
1240 bool found;
1242 /* If E->dest has abnormal outgoing edges, then there's no guarantee
1243 we can safely redirect any of the edges. Just punt those cases. */
1244 FOR_EACH_EDGE (taken_edge, ei, e->dest->succs)
1245 if (taken_edge->flags & EDGE_ABNORMAL)
1247 const_and_copies->pop_to_marker ();
1248 BITMAP_FREE (visited);
1249 return;
1252 /* Look at each successor of E->dest to see if we can thread through it. */
1253 FOR_EACH_EDGE (taken_edge, ei, e->dest->succs)
1255 if ((e->flags & EDGE_DFS_BACK) != 0
1256 || (taken_edge->flags & EDGE_DFS_BACK) != 0)
1257 continue;
1259 /* Push a fresh marker so we can unwind the equivalences created
1260 for each of E->dest's successors. */
1261 const_and_copies->push_marker ();
1262 if (avail_exprs_stack)
1263 avail_exprs_stack->push_marker ();
1265 /* Avoid threading to any block we have already visited. */
1266 bitmap_clear (visited);
1267 bitmap_set_bit (visited, e->src->index);
1268 bitmap_set_bit (visited, e->dest->index);
1269 bitmap_set_bit (visited, taken_edge->dest->index);
1270 vec<jump_thread_edge *> *path = new vec<jump_thread_edge *> ();
1272 /* Record whether or not we were able to thread through a successor
1273 of E->dest. */
1274 jump_thread_edge *x = new jump_thread_edge (e, EDGE_START_JUMP_THREAD);
1275 path->safe_push (x);
1277 x = new jump_thread_edge (taken_edge, EDGE_COPY_SRC_JOINER_BLOCK);
1278 path->safe_push (x);
1279 found = false;
1280 found = thread_around_empty_blocks (taken_edge,
1281 dummy_cond,
1282 avail_exprs_stack,
1283 handle_dominating_asserts,
1284 simplify,
1285 visited,
1286 path);
1288 if (!found)
1289 found = thread_through_normal_block (path->last ()->e, dummy_cond,
1290 handle_dominating_asserts,
1291 const_and_copies,
1292 avail_exprs_stack,
1293 simplify, path,
1294 visited) > 0;
1296 /* If we were able to thread through a successor of E->dest, then
1297 record the jump threading opportunity. */
1298 if (found)
1300 propagate_threaded_block_debug_into (path->last ()->e->dest,
1301 taken_edge->dest);
1302 register_jump_thread (path);
1304 else
1305 delete_jump_thread_path (path);
1307 /* And unwind the equivalence table. */
1308 if (avail_exprs_stack)
1309 avail_exprs_stack->pop_to_marker ();
1310 const_and_copies->pop_to_marker ();
1312 BITMAP_FREE (visited);
1315 const_and_copies->pop_to_marker ();