Fix memory leak in tree-vect-slp.c
[official-gcc.git] / gcc / tree-ssa-threadedge.c
blobeca3812090f35c7d62504ca9e10239dcf0d80cd1
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-threadbackward.h"
38 #include "tree-ssa-dom.h"
39 #include "gimple-fold.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.
396 The available expression table is referenced via AVAIL_EXPRS_STACK. */
398 static tree
399 simplify_control_stmt_condition (edge e,
400 gimple *stmt,
401 class avail_exprs_stack *avail_exprs_stack,
402 gcond *dummy_cond,
403 pfn_simplify simplify,
404 bool handle_dominating_asserts)
406 tree cond, cached_lhs;
407 enum gimple_code code = gimple_code (stmt);
409 /* For comparisons, we have to update both operands, then try
410 to simplify the comparison. */
411 if (code == GIMPLE_COND)
413 tree op0, op1;
414 enum tree_code cond_code;
416 op0 = gimple_cond_lhs (stmt);
417 op1 = gimple_cond_rhs (stmt);
418 cond_code = gimple_cond_code (stmt);
420 /* Get the current value of both operands. */
421 if (TREE_CODE (op0) == SSA_NAME)
423 for (int i = 0; i < 2; i++)
425 if (TREE_CODE (op0) == SSA_NAME
426 && SSA_NAME_VALUE (op0))
427 op0 = SSA_NAME_VALUE (op0);
428 else
429 break;
433 if (TREE_CODE (op1) == SSA_NAME)
435 for (int i = 0; i < 2; i++)
437 if (TREE_CODE (op1) == SSA_NAME
438 && SSA_NAME_VALUE (op1))
439 op1 = SSA_NAME_VALUE (op1);
440 else
441 break;
445 const unsigned recursion_limit = 4;
447 cached_lhs
448 = simplify_control_stmt_condition_1 (e, stmt, avail_exprs_stack,
449 op0, cond_code, op1,
450 dummy_cond, simplify,
451 handle_dominating_asserts,
452 recursion_limit);
454 /* If we were testing an integer/pointer against a constant, then
455 we can use the FSM code to trace the value of the SSA_NAME. If
456 a value is found, then the condition will collapse to a constant.
458 Return the SSA_NAME we want to trace back rather than the full
459 expression and give the FSM threader a chance to find its value. */
460 if (cached_lhs == NULL)
462 /* Recover the original operands. They may have been simplified
463 using context sensitive equivalences. Those context sensitive
464 equivalences may not be valid on paths found by the FSM optimizer. */
465 tree op0 = gimple_cond_lhs (stmt);
466 tree op1 = gimple_cond_rhs (stmt);
468 if ((INTEGRAL_TYPE_P (TREE_TYPE (op0))
469 || POINTER_TYPE_P (TREE_TYPE (op0)))
470 && TREE_CODE (op0) == SSA_NAME
471 && TREE_CODE (op1) == INTEGER_CST)
472 return op0;
475 return cached_lhs;
478 if (code == GIMPLE_SWITCH)
479 cond = gimple_switch_index (as_a <gswitch *> (stmt));
480 else if (code == GIMPLE_GOTO)
481 cond = gimple_goto_dest (stmt);
482 else
483 gcc_unreachable ();
485 /* We can have conditionals which just test the state of a variable
486 rather than use a relational operator. These are simpler to handle. */
487 if (TREE_CODE (cond) == SSA_NAME)
489 tree original_lhs = cond;
490 cached_lhs = cond;
492 /* Get the variable's current value from the equivalence chains.
494 It is possible to get loops in the SSA_NAME_VALUE chains
495 (consider threading the backedge of a loop where we have
496 a loop invariant SSA_NAME used in the condition). */
497 if (cached_lhs)
499 for (int i = 0; i < 2; i++)
501 if (TREE_CODE (cached_lhs) == SSA_NAME
502 && SSA_NAME_VALUE (cached_lhs))
503 cached_lhs = SSA_NAME_VALUE (cached_lhs);
504 else
505 break;
509 /* If we're dominated by a suitable ASSERT_EXPR, then
510 update CACHED_LHS appropriately. */
511 if (handle_dominating_asserts && TREE_CODE (cached_lhs) == SSA_NAME)
512 cached_lhs = lhs_of_dominating_assert (cached_lhs, e->src, stmt);
514 /* If we haven't simplified to an invariant yet, then use the
515 pass specific callback to try and simplify it further. */
516 if (cached_lhs && ! is_gimple_min_invariant (cached_lhs))
517 cached_lhs = (*simplify) (stmt, stmt, avail_exprs_stack);
519 /* We couldn't find an invariant. But, callers of this
520 function may be able to do something useful with the
521 unmodified destination. */
522 if (!cached_lhs)
523 cached_lhs = original_lhs;
525 else
526 cached_lhs = NULL;
528 return cached_lhs;
531 /* Recursive helper for simplify_control_stmt_condition. */
533 static tree
534 simplify_control_stmt_condition_1 (edge e,
535 gimple *stmt,
536 class avail_exprs_stack *avail_exprs_stack,
537 tree op0,
538 enum tree_code cond_code,
539 tree op1,
540 gcond *dummy_cond,
541 pfn_simplify simplify,
542 bool handle_dominating_asserts,
543 unsigned limit)
545 if (limit == 0)
546 return NULL_TREE;
548 /* We may need to canonicalize the comparison. For
549 example, op0 might be a constant while op1 is an
550 SSA_NAME. Failure to canonicalize will cause us to
551 miss threading opportunities. */
552 if (tree_swap_operands_p (op0, op1, false))
554 cond_code = swap_tree_comparison (cond_code);
555 std::swap (op0, op1);
558 /* If the condition has the form (A & B) CMP 0 or (A | B) CMP 0 then
559 recurse into the LHS to see if there is a dominating ASSERT_EXPR
560 of A or of B that makes this condition always true or always false
561 along the edge E. */
562 if (handle_dominating_asserts
563 && (cond_code == EQ_EXPR || cond_code == NE_EXPR)
564 && TREE_CODE (op0) == SSA_NAME
565 && integer_zerop (op1))
567 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
568 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
570 else if (gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR
571 || gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR)
573 enum tree_code rhs_code = gimple_assign_rhs_code (def_stmt);
574 const tree rhs1 = gimple_assign_rhs1 (def_stmt);
575 const tree rhs2 = gimple_assign_rhs2 (def_stmt);
576 const tree zero_cst = build_zero_cst (TREE_TYPE (op0));
577 const tree one_cst = build_one_cst (TREE_TYPE (op0));
579 /* Is A != 0 ? */
580 const tree res1
581 = simplify_control_stmt_condition_1 (e, def_stmt, avail_exprs_stack,
582 rhs1, NE_EXPR, op1,
583 dummy_cond, simplify,
584 handle_dominating_asserts,
585 limit - 1);
586 if (res1 == NULL_TREE)
588 else if (rhs_code == BIT_AND_EXPR && integer_zerop (res1))
590 /* If A == 0 then (A & B) != 0 is always false. */
591 if (cond_code == NE_EXPR)
592 return zero_cst;
593 /* If A == 0 then (A & B) == 0 is always true. */
594 if (cond_code == EQ_EXPR)
595 return one_cst;
597 else if (rhs_code == BIT_IOR_EXPR && integer_nonzerop (res1))
599 /* If A != 0 then (A | B) != 0 is always true. */
600 if (cond_code == NE_EXPR)
601 return one_cst;
602 /* If A != 0 then (A | B) == 0 is always false. */
603 if (cond_code == EQ_EXPR)
604 return zero_cst;
607 /* Is B != 0 ? */
608 const tree res2
609 = simplify_control_stmt_condition_1 (e, def_stmt, avail_exprs_stack,
610 rhs2, NE_EXPR, op1,
611 dummy_cond, simplify,
612 handle_dominating_asserts,
613 limit - 1);
614 if (res2 == NULL_TREE)
616 else if (rhs_code == BIT_AND_EXPR && integer_zerop (res2))
618 /* If B == 0 then (A & B) != 0 is always false. */
619 if (cond_code == NE_EXPR)
620 return zero_cst;
621 /* If B == 0 then (A & B) == 0 is always true. */
622 if (cond_code == EQ_EXPR)
623 return one_cst;
625 else if (rhs_code == BIT_IOR_EXPR && integer_nonzerop (res2))
627 /* If B != 0 then (A | B) != 0 is always true. */
628 if (cond_code == NE_EXPR)
629 return one_cst;
630 /* If B != 0 then (A | B) == 0 is always false. */
631 if (cond_code == EQ_EXPR)
632 return zero_cst;
635 if (res1 != NULL_TREE && res2 != NULL_TREE)
637 if (rhs_code == BIT_AND_EXPR
638 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
639 && integer_nonzerop (res1)
640 && integer_nonzerop (res2))
642 /* If A != 0 and B != 0 then (bool)(A & B) != 0 is true. */
643 if (cond_code == NE_EXPR)
644 return one_cst;
645 /* If A != 0 and B != 0 then (bool)(A & B) == 0 is false. */
646 if (cond_code == EQ_EXPR)
647 return zero_cst;
650 if (rhs_code == BIT_IOR_EXPR
651 && integer_zerop (res1)
652 && integer_zerop (res2))
654 /* If A == 0 and B == 0 then (A | B) != 0 is false. */
655 if (cond_code == NE_EXPR)
656 return zero_cst;
657 /* If A == 0 and B == 0 then (A | B) == 0 is true. */
658 if (cond_code == EQ_EXPR)
659 return one_cst;
663 /* Handle (A CMP B) CMP 0. */
664 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt))
665 == tcc_comparison)
667 tree rhs1 = gimple_assign_rhs1 (def_stmt);
668 tree rhs2 = gimple_assign_rhs2 (def_stmt);
670 tree_code new_cond = gimple_assign_rhs_code (def_stmt);
671 if (cond_code == EQ_EXPR)
672 new_cond = invert_tree_comparison (new_cond, false);
674 tree res
675 = simplify_control_stmt_condition_1 (e, def_stmt, avail_exprs_stack,
676 rhs1, new_cond, rhs2,
677 dummy_cond, simplify,
678 handle_dominating_asserts,
679 limit - 1);
680 if (res != NULL_TREE && is_gimple_min_invariant (res))
681 return res;
685 if (handle_dominating_asserts)
687 /* Now see if the operand was consumed by an ASSERT_EXPR
688 which dominates E->src. If so, we want to replace the
689 operand with the LHS of the ASSERT_EXPR. */
690 if (TREE_CODE (op0) == SSA_NAME)
691 op0 = lhs_of_dominating_assert (op0, e->src, stmt);
693 if (TREE_CODE (op1) == SSA_NAME)
694 op1 = lhs_of_dominating_assert (op1, e->src, stmt);
697 gimple_cond_set_code (dummy_cond, cond_code);
698 gimple_cond_set_lhs (dummy_cond, op0);
699 gimple_cond_set_rhs (dummy_cond, op1);
701 /* We absolutely do not care about any type conversions
702 we only care about a zero/nonzero value. */
703 fold_defer_overflow_warnings ();
705 tree res = fold_binary (cond_code, boolean_type_node, op0, op1);
706 if (res)
707 while (CONVERT_EXPR_P (res))
708 res = TREE_OPERAND (res, 0);
710 fold_undefer_overflow_warnings ((res && is_gimple_min_invariant (res)),
711 stmt, WARN_STRICT_OVERFLOW_CONDITIONAL);
713 /* If we have not simplified the condition down to an invariant,
714 then use the pass specific callback to simplify the condition. */
715 if (!res
716 || !is_gimple_min_invariant (res))
717 res = (*simplify) (dummy_cond, stmt, avail_exprs_stack);
719 return res;
722 /* Copy debug stmts from DEST's chain of single predecessors up to
723 SRC, so that we don't lose the bindings as PHI nodes are introduced
724 when DEST gains new predecessors. */
725 void
726 propagate_threaded_block_debug_into (basic_block dest, basic_block src)
728 if (!MAY_HAVE_DEBUG_STMTS)
729 return;
731 if (!single_pred_p (dest))
732 return;
734 gcc_checking_assert (dest != src);
736 gimple_stmt_iterator gsi = gsi_after_labels (dest);
737 int i = 0;
738 const int alloc_count = 16; // ?? Should this be a PARAM?
740 /* Estimate the number of debug vars overridden in the beginning of
741 DEST, to tell how many we're going to need to begin with. */
742 for (gimple_stmt_iterator si = gsi;
743 i * 4 <= alloc_count * 3 && !gsi_end_p (si); gsi_next (&si))
745 gimple *stmt = gsi_stmt (si);
746 if (!is_gimple_debug (stmt))
747 break;
748 i++;
751 auto_vec<tree, alloc_count> fewvars;
752 hash_set<tree> *vars = NULL;
754 /* If we're already starting with 3/4 of alloc_count, go for a
755 hash_set, otherwise start with an unordered stack-allocated
756 VEC. */
757 if (i * 4 > alloc_count * 3)
758 vars = new hash_set<tree>;
760 /* Now go through the initial debug stmts in DEST again, this time
761 actually inserting in VARS or FEWVARS. Don't bother checking for
762 duplicates in FEWVARS. */
763 for (gimple_stmt_iterator si = gsi; !gsi_end_p (si); gsi_next (&si))
765 gimple *stmt = gsi_stmt (si);
766 if (!is_gimple_debug (stmt))
767 break;
769 tree var;
771 if (gimple_debug_bind_p (stmt))
772 var = gimple_debug_bind_get_var (stmt);
773 else if (gimple_debug_source_bind_p (stmt))
774 var = gimple_debug_source_bind_get_var (stmt);
775 else
776 gcc_unreachable ();
778 if (vars)
779 vars->add (var);
780 else
781 fewvars.quick_push (var);
784 basic_block bb = dest;
788 bb = single_pred (bb);
789 for (gimple_stmt_iterator si = gsi_last_bb (bb);
790 !gsi_end_p (si); gsi_prev (&si))
792 gimple *stmt = gsi_stmt (si);
793 if (!is_gimple_debug (stmt))
794 continue;
796 tree var;
798 if (gimple_debug_bind_p (stmt))
799 var = gimple_debug_bind_get_var (stmt);
800 else if (gimple_debug_source_bind_p (stmt))
801 var = gimple_debug_source_bind_get_var (stmt);
802 else
803 gcc_unreachable ();
805 /* Discard debug bind overlaps. ??? Unlike stmts from src,
806 copied into a new block that will precede BB, debug bind
807 stmts in bypassed BBs may actually be discarded if
808 they're overwritten by subsequent debug bind stmts, which
809 might be a problem once we introduce stmt frontier notes
810 or somesuch. Adding `&& bb == src' to the condition
811 below will preserve all potentially relevant debug
812 notes. */
813 if (vars && vars->add (var))
814 continue;
815 else if (!vars)
817 int i = fewvars.length ();
818 while (i--)
819 if (fewvars[i] == var)
820 break;
821 if (i >= 0)
822 continue;
824 if (fewvars.length () < (unsigned) alloc_count)
825 fewvars.quick_push (var);
826 else
828 vars = new hash_set<tree>;
829 for (i = 0; i < alloc_count; i++)
830 vars->add (fewvars[i]);
831 fewvars.release ();
832 vars->add (var);
836 stmt = gimple_copy (stmt);
837 /* ??? Should we drop the location of the copy to denote
838 they're artificial bindings? */
839 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
842 while (bb != src && single_pred_p (bb));
844 if (vars)
845 delete vars;
846 else if (fewvars.exists ())
847 fewvars.release ();
850 /* See if TAKEN_EDGE->dest is a threadable block with no side effecs (ie, it
851 need not be duplicated as part of the CFG/SSA updating process).
853 If it is threadable, add it to PATH and VISITED and recurse, ultimately
854 returning TRUE from the toplevel call. Otherwise do nothing and
855 return false.
857 DUMMY_COND, HANDLE_DOMINATING_ASSERTS and SIMPLIFY are used to
858 try and simplify the condition at the end of TAKEN_EDGE->dest.
860 The available expression table is referenced via AVAIL_EXPRS_STACK. */
862 static bool
863 thread_around_empty_blocks (edge taken_edge,
864 gcond *dummy_cond,
865 class avail_exprs_stack *avail_exprs_stack,
866 bool handle_dominating_asserts,
867 pfn_simplify simplify,
868 bitmap visited,
869 vec<jump_thread_edge *> *path)
871 basic_block bb = taken_edge->dest;
872 gimple_stmt_iterator gsi;
873 gimple *stmt;
874 tree cond;
876 /* The key property of these blocks is that they need not be duplicated
877 when threading. Thus they can not have visible side effects such
878 as PHI nodes. */
879 if (!gsi_end_p (gsi_start_phis (bb)))
880 return false;
882 /* Skip over DEBUG statements at the start of the block. */
883 gsi = gsi_start_nondebug_bb (bb);
885 /* If the block has no statements, but does have a single successor, then
886 it's just a forwarding block and we can thread through it trivially.
888 However, note that just threading through empty blocks with single
889 successors is not inherently profitable. For the jump thread to
890 be profitable, we must avoid a runtime conditional.
892 By taking the return value from the recursive call, we get the
893 desired effect of returning TRUE when we found a profitable jump
894 threading opportunity and FALSE otherwise.
896 This is particularly important when this routine is called after
897 processing a joiner block. Returning TRUE too aggressively in
898 that case results in pointless duplication of the joiner block. */
899 if (gsi_end_p (gsi))
901 if (single_succ_p (bb))
903 taken_edge = single_succ_edge (bb);
905 if ((taken_edge->flags & EDGE_DFS_BACK) != 0)
906 return false;
908 if (!bitmap_bit_p (visited, taken_edge->dest->index))
910 jump_thread_edge *x
911 = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
912 path->safe_push (x);
913 bitmap_set_bit (visited, taken_edge->dest->index);
914 return thread_around_empty_blocks (taken_edge,
915 dummy_cond,
916 avail_exprs_stack,
917 handle_dominating_asserts,
918 simplify,
919 visited,
920 path);
924 /* We have a block with no statements, but multiple successors? */
925 return false;
928 /* The only real statements this block can have are a control
929 flow altering statement. Anything else stops the thread. */
930 stmt = gsi_stmt (gsi);
931 if (gimple_code (stmt) != GIMPLE_COND
932 && gimple_code (stmt) != GIMPLE_GOTO
933 && gimple_code (stmt) != GIMPLE_SWITCH)
934 return false;
936 /* Extract and simplify the condition. */
937 cond = simplify_control_stmt_condition (taken_edge, stmt,
938 avail_exprs_stack, dummy_cond,
939 simplify, handle_dominating_asserts);
941 /* If the condition can be statically computed and we have not already
942 visited the destination edge, then add the taken edge to our thread
943 path. */
944 if (cond && is_gimple_min_invariant (cond))
946 taken_edge = find_taken_edge (bb, cond);
948 if ((taken_edge->flags & EDGE_DFS_BACK) != 0)
949 return false;
951 if (bitmap_bit_p (visited, taken_edge->dest->index))
952 return false;
953 bitmap_set_bit (visited, taken_edge->dest->index);
955 jump_thread_edge *x
956 = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
957 path->safe_push (x);
959 thread_around_empty_blocks (taken_edge,
960 dummy_cond,
961 avail_exprs_stack,
962 handle_dominating_asserts,
963 simplify,
964 visited,
965 path);
966 return true;
969 return false;
972 /* We are exiting E->src, see if E->dest ends with a conditional
973 jump which has a known value when reached via E.
975 E->dest can have arbitrary side effects which, if threading is
976 successful, will be maintained.
978 Special care is necessary if E is a back edge in the CFG as we
979 may have already recorded equivalences for E->dest into our
980 various tables, including the result of the conditional at
981 the end of E->dest. Threading opportunities are severely
982 limited in that case to avoid short-circuiting the loop
983 incorrectly.
985 DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
986 to avoid allocating memory.
988 HANDLE_DOMINATING_ASSERTS is true if we should try to replace operands of
989 the simplified condition with left-hand sides of ASSERT_EXPRs they are
990 used in.
992 STACK is used to undo temporary equivalences created during the walk of
993 E->dest.
995 SIMPLIFY is a pass-specific function used to simplify statements.
997 Our caller is responsible for restoring the state of the expression
998 and const_and_copies stacks.
1000 Positive return value is success. Zero return value is failure, but
1001 the block can still be duplicated as a joiner in a jump thread path,
1002 negative indicates the block should not be duplicated and thus is not
1003 suitable for a joiner in a jump threading path. */
1005 static int
1006 thread_through_normal_block (edge e,
1007 gcond *dummy_cond,
1008 bool handle_dominating_asserts,
1009 const_and_copies *const_and_copies,
1010 avail_exprs_stack *avail_exprs_stack,
1011 pfn_simplify simplify,
1012 vec<jump_thread_edge *> *path,
1013 bitmap visited)
1015 /* We want to record any equivalences created by traversing E. */
1016 if (!handle_dominating_asserts)
1017 record_temporary_equivalences (e, const_and_copies, avail_exprs_stack);
1019 /* PHIs create temporary equivalences.
1020 Note that if we found a PHI that made the block non-threadable, then
1021 we need to bubble that up to our caller in the same manner we do
1022 when we prematurely stop processing statements below. */
1023 if (!record_temporary_equivalences_from_phis (e, const_and_copies))
1024 return -1;
1026 /* Now walk each statement recording any context sensitive
1027 temporary equivalences we can detect. */
1028 gimple *stmt
1029 = record_temporary_equivalences_from_stmts_at_dest (e, const_and_copies,
1030 avail_exprs_stack,
1031 simplify);
1033 /* There's two reasons STMT might be null, and distinguishing
1034 between them is important.
1036 First the block may not have had any statements. For example, it
1037 might have some PHIs and unconditionally transfer control elsewhere.
1038 Such blocks are suitable for jump threading, particularly as a
1039 joiner block.
1041 The second reason would be if we did not process all the statements
1042 in the block (because there were too many to make duplicating the
1043 block profitable. If we did not look at all the statements, then
1044 we may not have invalidated everything needing invalidation. Thus
1045 we must signal to our caller that this block is not suitable for
1046 use as a joiner in a threading path. */
1047 if (!stmt)
1049 /* First case. The statement simply doesn't have any instructions, but
1050 does have PHIs. */
1051 if (gsi_end_p (gsi_start_nondebug_bb (e->dest))
1052 && !gsi_end_p (gsi_start_phis (e->dest)))
1053 return 0;
1055 /* Second case. */
1056 return -1;
1059 /* If we stopped at a COND_EXPR or SWITCH_EXPR, see if we know which arm
1060 will be taken. */
1061 if (gimple_code (stmt) == GIMPLE_COND
1062 || gimple_code (stmt) == GIMPLE_GOTO
1063 || gimple_code (stmt) == GIMPLE_SWITCH)
1065 tree cond;
1067 /* Extract and simplify the condition. */
1068 cond = simplify_control_stmt_condition (e, stmt, avail_exprs_stack,
1069 dummy_cond, simplify,
1070 handle_dominating_asserts);
1072 if (!cond)
1073 return 0;
1075 if (is_gimple_min_invariant (cond))
1077 edge taken_edge = find_taken_edge (e->dest, cond);
1078 basic_block dest = (taken_edge ? taken_edge->dest : NULL);
1080 /* DEST could be NULL for a computed jump to an absolute
1081 address. */
1082 if (dest == NULL
1083 || dest == e->dest
1084 || (taken_edge->flags & EDGE_DFS_BACK) != 0
1085 || bitmap_bit_p (visited, dest->index))
1086 return 0;
1088 /* Only push the EDGE_START_JUMP_THREAD marker if this is
1089 first edge on the path. */
1090 if (path->length () == 0)
1092 jump_thread_edge *x
1093 = new jump_thread_edge (e, EDGE_START_JUMP_THREAD);
1094 path->safe_push (x);
1097 jump_thread_edge *x
1098 = new jump_thread_edge (taken_edge, EDGE_COPY_SRC_BLOCK);
1099 path->safe_push (x);
1101 /* See if we can thread through DEST as well, this helps capture
1102 secondary effects of threading without having to re-run DOM or
1103 VRP.
1105 We don't want to thread back to a block we have already
1106 visited. This may be overly conservative. */
1107 bitmap_set_bit (visited, dest->index);
1108 bitmap_set_bit (visited, e->dest->index);
1109 thread_around_empty_blocks (taken_edge,
1110 dummy_cond,
1111 avail_exprs_stack,
1112 handle_dominating_asserts,
1113 simplify,
1114 visited,
1115 path);
1116 return 1;
1119 return 0;
1122 /* We are exiting E->src, see if E->dest ends with a conditional
1123 jump which has a known value when reached via E.
1125 DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
1126 to avoid allocating memory.
1128 HANDLE_DOMINATING_ASSERTS is true if we should try to replace operands of
1129 the simplified condition with left-hand sides of ASSERT_EXPRs they are
1130 used in.
1132 CONST_AND_COPIES is used to undo temporary equivalences created during the
1133 walk of E->dest.
1135 The available expression table is referenced vai AVAIL_EXPRS_STACK.
1137 SIMPLIFY is a pass-specific function used to simplify statements. */
1139 void
1140 thread_across_edge (gcond *dummy_cond,
1141 edge e,
1142 bool handle_dominating_asserts,
1143 class const_and_copies *const_and_copies,
1144 class avail_exprs_stack *avail_exprs_stack,
1145 tree (*simplify) (gimple *, gimple *,
1146 class avail_exprs_stack *))
1148 bitmap visited = BITMAP_ALLOC (NULL);
1150 stmt_count = 0;
1152 vec<jump_thread_edge *> *path = new vec<jump_thread_edge *> ();
1153 bitmap_clear (visited);
1154 bitmap_set_bit (visited, e->src->index);
1155 bitmap_set_bit (visited, e->dest->index);
1157 int threaded;
1158 if ((e->flags & EDGE_DFS_BACK) == 0)
1159 threaded = thread_through_normal_block (e, dummy_cond,
1160 handle_dominating_asserts,
1161 const_and_copies,
1162 avail_exprs_stack,
1163 simplify, path,
1164 visited);
1165 else
1166 threaded = 0;
1168 if (threaded > 0)
1170 propagate_threaded_block_debug_into (path->last ()->e->dest,
1171 e->dest);
1172 const_and_copies->pop_to_marker ();
1173 BITMAP_FREE (visited);
1174 register_jump_thread (path);
1175 return;
1177 else
1179 /* Negative and zero return values indicate no threading was possible,
1180 thus there should be no edges on the thread path and no need to walk
1181 through the vector entries. */
1182 gcc_assert (path->length () == 0);
1183 path->release ();
1184 delete path;
1186 find_jump_threads_backwards (e);
1188 /* A negative status indicates the target block was deemed too big to
1189 duplicate. Just quit now rather than trying to use the block as
1190 a joiner in a jump threading path.
1192 This prevents unnecessary code growth, but more importantly if we
1193 do not look at all the statements in the block, then we may have
1194 missed some invalidations if we had traversed a backedge! */
1195 if (threaded < 0)
1197 BITMAP_FREE (visited);
1198 const_and_copies->pop_to_marker ();
1199 return;
1203 /* We were unable to determine what out edge from E->dest is taken. However,
1204 we might still be able to thread through successors of E->dest. This
1205 often occurs when E->dest is a joiner block which then fans back out
1206 based on redundant tests.
1208 If so, we'll copy E->dest and redirect the appropriate predecessor to
1209 the copy. Within the copy of E->dest, we'll thread one or more edges
1210 to points deeper in the CFG.
1212 This is a stopgap until we have a more structured approach to path
1213 isolation. */
1215 edge taken_edge;
1216 edge_iterator ei;
1217 bool found;
1219 /* If E->dest has abnormal outgoing edges, then there's no guarantee
1220 we can safely redirect any of the edges. Just punt those cases. */
1221 FOR_EACH_EDGE (taken_edge, ei, e->dest->succs)
1222 if (taken_edge->flags & EDGE_ABNORMAL)
1224 const_and_copies->pop_to_marker ();
1225 BITMAP_FREE (visited);
1226 return;
1229 /* Look at each successor of E->dest to see if we can thread through it. */
1230 FOR_EACH_EDGE (taken_edge, ei, e->dest->succs)
1232 if ((e->flags & EDGE_DFS_BACK) != 0
1233 || (taken_edge->flags & EDGE_DFS_BACK) != 0)
1235 find_jump_threads_backwards (taken_edge);
1236 continue;
1239 /* Push a fresh marker so we can unwind the equivalences created
1240 for each of E->dest's successors. */
1241 const_and_copies->push_marker ();
1242 if (avail_exprs_stack)
1243 avail_exprs_stack->push_marker ();
1245 /* Avoid threading to any block we have already visited. */
1246 bitmap_clear (visited);
1247 bitmap_set_bit (visited, e->src->index);
1248 bitmap_set_bit (visited, e->dest->index);
1249 bitmap_set_bit (visited, taken_edge->dest->index);
1250 vec<jump_thread_edge *> *path = new vec<jump_thread_edge *> ();
1252 /* Record whether or not we were able to thread through a successor
1253 of E->dest. */
1254 jump_thread_edge *x = new jump_thread_edge (e, EDGE_START_JUMP_THREAD);
1255 path->safe_push (x);
1257 x = new jump_thread_edge (taken_edge, EDGE_COPY_SRC_JOINER_BLOCK);
1258 path->safe_push (x);
1259 found = false;
1260 found = thread_around_empty_blocks (taken_edge,
1261 dummy_cond,
1262 avail_exprs_stack,
1263 handle_dominating_asserts,
1264 simplify,
1265 visited,
1266 path);
1268 if (!found)
1269 found = thread_through_normal_block (path->last ()->e, dummy_cond,
1270 handle_dominating_asserts,
1271 const_and_copies,
1272 avail_exprs_stack,
1273 simplify, path,
1274 visited) > 0;
1276 /* If we were able to thread through a successor of E->dest, then
1277 record the jump threading opportunity. */
1278 if (found)
1280 propagate_threaded_block_debug_into (path->last ()->e->dest,
1281 taken_edge->dest);
1282 register_jump_thread (path);
1284 else
1286 find_jump_threads_backwards (path->last ()->e);
1287 delete_jump_thread_path (path);
1290 /* And unwind the equivalence table. */
1291 if (avail_exprs_stack)
1292 avail_exprs_stack->pop_to_marker ();
1293 const_and_copies->pop_to_marker ();
1295 BITMAP_FREE (visited);
1298 const_and_copies->pop_to_marker ();