* gcc-interface/trans.c (Subprogram_Body_to_gnu): Initialize locus.
[official-gcc.git] / gcc / tree-ssa-threadedge.c
blobb7781dc7d608636d759d29e18e875a856d733400
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"
40 #include "alloc-pool.h"
41 #include "vr-values.h"
42 #include "gimple-ssa-evrp-analyze.h"
44 /* To avoid code explosion due to jump threading, we limit the
45 number of statements we are going to copy. This variable
46 holds the number of statements currently seen that we'll have
47 to copy as part of the jump threading process. */
48 static int stmt_count;
50 /* Array to record value-handles per SSA_NAME. */
51 vec<tree> ssa_name_values;
53 typedef tree (pfn_simplify) (gimple *, gimple *,
54 class avail_exprs_stack *,
55 basic_block);
57 /* Set the value for the SSA name NAME to VALUE. */
59 void
60 set_ssa_name_value (tree name, tree value)
62 if (SSA_NAME_VERSION (name) >= ssa_name_values.length ())
63 ssa_name_values.safe_grow_cleared (SSA_NAME_VERSION (name) + 1);
64 if (value && TREE_OVERFLOW_P (value))
65 value = drop_tree_overflow (value);
66 ssa_name_values[SSA_NAME_VERSION (name)] = value;
69 /* Initialize the per SSA_NAME value-handles array. Returns it. */
70 void
71 threadedge_initialize_values (void)
73 gcc_assert (!ssa_name_values.exists ());
74 ssa_name_values.create (num_ssa_names);
77 /* Free the per SSA_NAME value-handle array. */
78 void
79 threadedge_finalize_values (void)
81 ssa_name_values.release ();
84 /* Return TRUE if we may be able to thread an incoming edge into
85 BB to an outgoing edge from BB. Return FALSE otherwise. */
87 bool
88 potentially_threadable_block (basic_block bb)
90 gimple_stmt_iterator gsi;
92 /* Special case. We can get blocks that are forwarders, but are
93 not optimized away because they forward from outside a loop
94 to the loop header. We want to thread through them as we can
95 sometimes thread to the loop exit, which is obviously profitable.
96 the interesting case here is when the block has PHIs. */
97 if (gsi_end_p (gsi_start_nondebug_bb (bb))
98 && !gsi_end_p (gsi_start_phis (bb)))
99 return true;
101 /* If BB has a single successor or a single predecessor, then
102 there is no threading opportunity. */
103 if (single_succ_p (bb) || single_pred_p (bb))
104 return false;
106 /* If BB does not end with a conditional, switch or computed goto,
107 then there is no threading opportunity. */
108 gsi = gsi_last_bb (bb);
109 if (gsi_end_p (gsi)
110 || ! gsi_stmt (gsi)
111 || (gimple_code (gsi_stmt (gsi)) != GIMPLE_COND
112 && gimple_code (gsi_stmt (gsi)) != GIMPLE_GOTO
113 && gimple_code (gsi_stmt (gsi)) != GIMPLE_SWITCH))
114 return false;
116 return true;
119 /* Record temporary equivalences created by PHIs at the target of the
120 edge E. Record unwind information for the equivalences into
121 CONST_AND_COPIES and EVRP_RANGE_DATA.
123 If a PHI which prevents threading is encountered, then return FALSE
124 indicating we should not thread this edge, else return TRUE. */
126 static bool
127 record_temporary_equivalences_from_phis (edge e,
128 const_and_copies *const_and_copies,
129 evrp_range_analyzer *evrp_range_analyzer)
131 gphi_iterator gsi;
133 /* Each PHI creates a temporary equivalence, record them.
134 These are context sensitive equivalences and will be removed
135 later. */
136 for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
138 gphi *phi = gsi.phi ();
139 tree src = PHI_ARG_DEF_FROM_EDGE (phi, e);
140 tree dst = gimple_phi_result (phi);
142 /* If the desired argument is not the same as this PHI's result
143 and it is set by a PHI in E->dest, then we can not thread
144 through E->dest. */
145 if (src != dst
146 && TREE_CODE (src) == SSA_NAME
147 && gimple_code (SSA_NAME_DEF_STMT (src)) == GIMPLE_PHI
148 && gimple_bb (SSA_NAME_DEF_STMT (src)) == e->dest)
149 return false;
151 /* We consider any non-virtual PHI as a statement since it
152 count result in a constant assignment or copy operation. */
153 if (!virtual_operand_p (dst))
154 stmt_count++;
156 const_and_copies->record_const_or_copy (dst, src);
158 /* Also update the value range associated with DST, using
159 the range from SRC. */
160 if (evrp_range_analyzer && TREE_CODE (src) == SSA_NAME)
162 value_range *vr = evrp_range_analyzer->get_value_range (src);
163 evrp_range_analyzer->push_value_range (dst, vr);
166 return true;
169 /* Valueize hook for gimple_fold_stmt_to_constant_1. */
171 static tree
172 threadedge_valueize (tree t)
174 if (TREE_CODE (t) == SSA_NAME)
176 tree tem = SSA_NAME_VALUE (t);
177 if (tem)
178 return tem;
180 return t;
183 /* Try to simplify each statement in E->dest, ultimately leading to
184 a simplification of the COND_EXPR at the end of E->dest.
186 Record unwind information for temporary equivalences onto STACK.
188 Use SIMPLIFY (a pointer to a callback function) to further simplify
189 statements using pass specific information.
191 We might consider marking just those statements which ultimately
192 feed the COND_EXPR. It's not clear if the overhead of bookkeeping
193 would be recovered by trying to simplify fewer statements.
195 If we are able to simplify a statement into the form
196 SSA_NAME = (SSA_NAME | gimple invariant), then we can record
197 a context sensitive equivalence which may help us simplify
198 later statements in E->dest. */
200 static gimple *
201 record_temporary_equivalences_from_stmts_at_dest (edge e,
202 const_and_copies *const_and_copies,
203 avail_exprs_stack *avail_exprs_stack,
204 evrp_range_analyzer *evrp_range_analyzer,
205 pfn_simplify simplify)
207 gimple *stmt = NULL;
208 gimple_stmt_iterator gsi;
209 int max_stmt_count;
211 max_stmt_count = PARAM_VALUE (PARAM_MAX_JUMP_THREAD_DUPLICATION_STMTS);
213 /* Walk through each statement in the block recording equivalences
214 we discover. Note any equivalences we discover are context
215 sensitive (ie, are dependent on traversing E) and must be unwound
216 when we're finished processing E. */
217 for (gsi = gsi_start_bb (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
219 tree cached_lhs = NULL;
221 stmt = gsi_stmt (gsi);
223 /* Ignore empty statements and labels. */
224 if (gimple_code (stmt) == GIMPLE_NOP
225 || gimple_code (stmt) == GIMPLE_LABEL
226 || is_gimple_debug (stmt))
227 continue;
229 /* If the statement has volatile operands, then we assume we
230 can not thread through this block. This is overly
231 conservative in some ways. */
232 if (gimple_code (stmt) == GIMPLE_ASM
233 && gimple_asm_volatile_p (as_a <gasm *> (stmt)))
234 return NULL;
236 /* If the statement is a unique builtin, we can not thread
237 through here. */
238 if (gimple_code (stmt) == GIMPLE_CALL
239 && gimple_call_internal_p (stmt)
240 && gimple_call_internal_unique_p (stmt))
241 return NULL;
243 /* If duplicating this block is going to cause too much code
244 expansion, then do not thread through this block. */
245 stmt_count++;
246 if (stmt_count > max_stmt_count)
247 return NULL;
249 /* These are temporary ranges, do nto reflect them back into
250 the global range data. */
251 if (evrp_range_analyzer)
252 evrp_range_analyzer->record_ranges_from_stmt (stmt, true);
254 /* If this is not a statement that sets an SSA_NAME to a new
255 value, then do not try to simplify this statement as it will
256 not simplify in any way that is helpful for jump threading. */
257 if ((gimple_code (stmt) != GIMPLE_ASSIGN
258 || TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
259 && (gimple_code (stmt) != GIMPLE_CALL
260 || gimple_call_lhs (stmt) == NULL_TREE
261 || TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME))
262 continue;
264 /* The result of __builtin_object_size depends on all the arguments
265 of a phi node. Temporarily using only one edge produces invalid
266 results. For example
268 if (x < 6)
269 goto l;
270 else
271 goto l;
274 r = PHI <&w[2].a[1](2), &a.a[6](3)>
275 __builtin_object_size (r, 0)
277 The result of __builtin_object_size is defined to be the maximum of
278 remaining bytes. If we use only one edge on the phi, the result will
279 change to be the remaining bytes for the corresponding phi argument.
281 Similarly for __builtin_constant_p:
283 r = PHI <1(2), 2(3)>
284 __builtin_constant_p (r)
286 Both PHI arguments are constant, but x ? 1 : 2 is still not
287 constant. */
289 if (is_gimple_call (stmt))
291 tree fndecl = gimple_call_fndecl (stmt);
292 if (fndecl
293 && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_OBJECT_SIZE
294 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P))
295 continue;
298 /* At this point we have a statement which assigns an RHS to an
299 SSA_VAR on the LHS. We want to try and simplify this statement
300 to expose more context sensitive equivalences which in turn may
301 allow us to simplify the condition at the end of the loop.
303 Handle simple copy operations as well as implied copies from
304 ASSERT_EXPRs. */
305 if (gimple_assign_single_p (stmt)
306 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
307 cached_lhs = gimple_assign_rhs1 (stmt);
308 else if (gimple_assign_single_p (stmt)
309 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
310 cached_lhs = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
311 else
313 /* A statement that is not a trivial copy or ASSERT_EXPR.
314 Try to fold the new expression. Inserting the
315 expression into the hash table is unlikely to help. */
316 /* ??? The DOM callback below can be changed to setting
317 the mprts_hook around the call to thread_across_edge,
318 avoiding the use substitution. The VRP hook should be
319 changed to properly valueize operands itself using
320 SSA_NAME_VALUE in addition to its own lattice. */
321 cached_lhs = gimple_fold_stmt_to_constant_1 (stmt,
322 threadedge_valueize);
323 if (NUM_SSA_OPERANDS (stmt, SSA_OP_ALL_USES) != 0
324 && (!cached_lhs
325 || (TREE_CODE (cached_lhs) != SSA_NAME
326 && !is_gimple_min_invariant (cached_lhs))))
328 /* We're going to temporarily copy propagate the operands
329 and see if that allows us to simplify this statement. */
330 tree *copy;
331 ssa_op_iter iter;
332 use_operand_p use_p;
333 unsigned int num, i = 0;
335 num = NUM_SSA_OPERANDS (stmt, SSA_OP_ALL_USES);
336 copy = XALLOCAVEC (tree, num);
338 /* Make a copy of the uses & vuses into USES_COPY, then cprop into
339 the operands. */
340 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
342 tree tmp = NULL;
343 tree use = USE_FROM_PTR (use_p);
345 copy[i++] = use;
346 if (TREE_CODE (use) == SSA_NAME)
347 tmp = SSA_NAME_VALUE (use);
348 if (tmp)
349 SET_USE (use_p, tmp);
352 cached_lhs = (*simplify) (stmt, stmt, avail_exprs_stack, e->src);
354 /* Restore the statement's original uses/defs. */
355 i = 0;
356 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
357 SET_USE (use_p, copy[i++]);
361 /* Record the context sensitive equivalence if we were able
362 to simplify this statement. */
363 if (cached_lhs
364 && (TREE_CODE (cached_lhs) == SSA_NAME
365 || is_gimple_min_invariant (cached_lhs)))
366 const_and_copies->record_const_or_copy (gimple_get_lhs (stmt),
367 cached_lhs);
369 return stmt;
372 static tree simplify_control_stmt_condition_1 (edge, gimple *,
373 class avail_exprs_stack *,
374 tree, enum tree_code, tree,
375 gcond *, pfn_simplify,
376 unsigned);
378 /* Simplify the control statement at the end of the block E->dest.
380 To avoid allocating memory unnecessarily, a scratch GIMPLE_COND
381 is available to use/clobber in DUMMY_COND.
383 Use SIMPLIFY (a pointer to a callback function) to further simplify
384 a condition using pass specific information.
386 Return the simplified condition or NULL if simplification could
387 not be performed. When simplifying a GIMPLE_SWITCH, we may return
388 the CASE_LABEL_EXPR that will be taken.
390 The available expression table is referenced via AVAIL_EXPRS_STACK. */
392 static tree
393 simplify_control_stmt_condition (edge e,
394 gimple *stmt,
395 class avail_exprs_stack *avail_exprs_stack,
396 gcond *dummy_cond,
397 pfn_simplify simplify)
399 tree cond, cached_lhs;
400 enum gimple_code code = gimple_code (stmt);
402 /* For comparisons, we have to update both operands, then try
403 to simplify the comparison. */
404 if (code == GIMPLE_COND)
406 tree op0, op1;
407 enum tree_code cond_code;
409 op0 = gimple_cond_lhs (stmt);
410 op1 = gimple_cond_rhs (stmt);
411 cond_code = gimple_cond_code (stmt);
413 /* Get the current value of both operands. */
414 if (TREE_CODE (op0) == SSA_NAME)
416 for (int i = 0; i < 2; i++)
418 if (TREE_CODE (op0) == SSA_NAME
419 && SSA_NAME_VALUE (op0))
420 op0 = SSA_NAME_VALUE (op0);
421 else
422 break;
426 if (TREE_CODE (op1) == SSA_NAME)
428 for (int i = 0; i < 2; i++)
430 if (TREE_CODE (op1) == SSA_NAME
431 && SSA_NAME_VALUE (op1))
432 op1 = SSA_NAME_VALUE (op1);
433 else
434 break;
438 const unsigned recursion_limit = 4;
440 cached_lhs
441 = simplify_control_stmt_condition_1 (e, stmt, avail_exprs_stack,
442 op0, cond_code, op1,
443 dummy_cond, simplify,
444 recursion_limit);
446 /* If we were testing an integer/pointer against a constant, then
447 we can use the FSM code to trace the value of the SSA_NAME. If
448 a value is found, then the condition will collapse to a constant.
450 Return the SSA_NAME we want to trace back rather than the full
451 expression and give the FSM threader a chance to find its value. */
452 if (cached_lhs == NULL)
454 /* Recover the original operands. They may have been simplified
455 using context sensitive equivalences. Those context sensitive
456 equivalences may not be valid on paths found by the FSM optimizer. */
457 tree op0 = gimple_cond_lhs (stmt);
458 tree op1 = gimple_cond_rhs (stmt);
460 if ((INTEGRAL_TYPE_P (TREE_TYPE (op0))
461 || POINTER_TYPE_P (TREE_TYPE (op0)))
462 && TREE_CODE (op0) == SSA_NAME
463 && TREE_CODE (op1) == INTEGER_CST)
464 return op0;
467 return cached_lhs;
470 if (code == GIMPLE_SWITCH)
471 cond = gimple_switch_index (as_a <gswitch *> (stmt));
472 else if (code == GIMPLE_GOTO)
473 cond = gimple_goto_dest (stmt);
474 else
475 gcc_unreachable ();
477 /* We can have conditionals which just test the state of a variable
478 rather than use a relational operator. These are simpler to handle. */
479 if (TREE_CODE (cond) == SSA_NAME)
481 tree original_lhs = cond;
482 cached_lhs = cond;
484 /* Get the variable's current value from the equivalence chains.
486 It is possible to get loops in the SSA_NAME_VALUE chains
487 (consider threading the backedge of a loop where we have
488 a loop invariant SSA_NAME used in the condition). */
489 if (cached_lhs)
491 for (int i = 0; i < 2; i++)
493 if (TREE_CODE (cached_lhs) == SSA_NAME
494 && SSA_NAME_VALUE (cached_lhs))
495 cached_lhs = SSA_NAME_VALUE (cached_lhs);
496 else
497 break;
501 /* If we haven't simplified to an invariant yet, then use the
502 pass specific callback to try and simplify it further. */
503 if (cached_lhs && ! is_gimple_min_invariant (cached_lhs))
505 if (code == GIMPLE_SWITCH)
507 /* Replace the index operand of the GIMPLE_SWITCH with any LHS
508 we found before handing off to VRP. If simplification is
509 possible, the simplified value will be a CASE_LABEL_EXPR of
510 the label that is proven to be taken. */
511 gswitch *dummy_switch = as_a<gswitch *> (gimple_copy (stmt));
512 gimple_switch_set_index (dummy_switch, cached_lhs);
513 cached_lhs = (*simplify) (dummy_switch, stmt,
514 avail_exprs_stack, e->src);
515 ggc_free (dummy_switch);
517 else
518 cached_lhs = (*simplify) (stmt, stmt, avail_exprs_stack, e->src);
521 /* We couldn't find an invariant. But, callers of this
522 function may be able to do something useful with the
523 unmodified destination. */
524 if (!cached_lhs)
525 cached_lhs = original_lhs;
527 else
528 cached_lhs = NULL;
530 return cached_lhs;
533 /* Recursive helper for simplify_control_stmt_condition. */
535 static tree
536 simplify_control_stmt_condition_1 (edge e,
537 gimple *stmt,
538 class avail_exprs_stack *avail_exprs_stack,
539 tree op0,
540 enum tree_code cond_code,
541 tree op1,
542 gcond *dummy_cond,
543 pfn_simplify simplify,
544 unsigned limit)
546 if (limit == 0)
547 return NULL_TREE;
549 /* We may need to canonicalize the comparison. For
550 example, op0 might be a constant while op1 is an
551 SSA_NAME. Failure to canonicalize will cause us to
552 miss threading opportunities. */
553 if (tree_swap_operands_p (op0, op1))
555 cond_code = swap_tree_comparison (cond_code);
556 std::swap (op0, op1);
559 /* If the condition has the form (A & B) CMP 0 or (A | B) CMP 0 then
560 recurse into the LHS to see if there is a dominating ASSERT_EXPR
561 of A or of B that makes this condition always true or always false
562 along the edge E. */
563 if ((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);
577 /* Is A != 0 ? */
578 const tree res1
579 = simplify_control_stmt_condition_1 (e, def_stmt, avail_exprs_stack,
580 rhs1, NE_EXPR, op1,
581 dummy_cond, simplify,
582 limit - 1);
583 if (res1 == NULL_TREE)
585 else if (rhs_code == BIT_AND_EXPR && integer_zerop (res1))
587 /* If A == 0 then (A & B) != 0 is always false. */
588 if (cond_code == NE_EXPR)
589 return boolean_false_node;
590 /* If A == 0 then (A & B) == 0 is always true. */
591 if (cond_code == EQ_EXPR)
592 return boolean_true_node;
594 else if (rhs_code == BIT_IOR_EXPR && integer_nonzerop (res1))
596 /* If A != 0 then (A | B) != 0 is always true. */
597 if (cond_code == NE_EXPR)
598 return boolean_true_node;
599 /* If A != 0 then (A | B) == 0 is always false. */
600 if (cond_code == EQ_EXPR)
601 return boolean_false_node;
604 /* Is B != 0 ? */
605 const tree res2
606 = simplify_control_stmt_condition_1 (e, def_stmt, avail_exprs_stack,
607 rhs2, NE_EXPR, op1,
608 dummy_cond, simplify,
609 limit - 1);
610 if (res2 == NULL_TREE)
612 else if (rhs_code == BIT_AND_EXPR && integer_zerop (res2))
614 /* If B == 0 then (A & B) != 0 is always false. */
615 if (cond_code == NE_EXPR)
616 return boolean_false_node;
617 /* If B == 0 then (A & B) == 0 is always true. */
618 if (cond_code == EQ_EXPR)
619 return boolean_true_node;
621 else if (rhs_code == BIT_IOR_EXPR && integer_nonzerop (res2))
623 /* If B != 0 then (A | B) != 0 is always true. */
624 if (cond_code == NE_EXPR)
625 return boolean_true_node;
626 /* If B != 0 then (A | B) == 0 is always false. */
627 if (cond_code == EQ_EXPR)
628 return boolean_false_node;
631 if (res1 != NULL_TREE && res2 != NULL_TREE)
633 if (rhs_code == BIT_AND_EXPR
634 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
635 && integer_nonzerop (res1)
636 && integer_nonzerop (res2))
638 /* If A != 0 and B != 0 then (bool)(A & B) != 0 is true. */
639 if (cond_code == NE_EXPR)
640 return boolean_true_node;
641 /* If A != 0 and B != 0 then (bool)(A & B) == 0 is false. */
642 if (cond_code == EQ_EXPR)
643 return boolean_false_node;
646 if (rhs_code == BIT_IOR_EXPR
647 && integer_zerop (res1)
648 && integer_zerop (res2))
650 /* If A == 0 and B == 0 then (A | B) != 0 is false. */
651 if (cond_code == NE_EXPR)
652 return boolean_false_node;
653 /* If A == 0 and B == 0 then (A | B) == 0 is true. */
654 if (cond_code == EQ_EXPR)
655 return boolean_true_node;
659 /* Handle (A CMP B) CMP 0. */
660 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt))
661 == tcc_comparison)
663 tree rhs1 = gimple_assign_rhs1 (def_stmt);
664 tree rhs2 = gimple_assign_rhs2 (def_stmt);
666 tree_code new_cond = gimple_assign_rhs_code (def_stmt);
667 if (cond_code == EQ_EXPR)
668 new_cond = invert_tree_comparison (new_cond, false);
670 tree res
671 = simplify_control_stmt_condition_1 (e, def_stmt, avail_exprs_stack,
672 rhs1, new_cond, rhs2,
673 dummy_cond, simplify,
674 limit - 1);
675 if (res != NULL_TREE && is_gimple_min_invariant (res))
676 return res;
680 gimple_cond_set_code (dummy_cond, cond_code);
681 gimple_cond_set_lhs (dummy_cond, op0);
682 gimple_cond_set_rhs (dummy_cond, op1);
684 /* We absolutely do not care about any type conversions
685 we only care about a zero/nonzero value. */
686 fold_defer_overflow_warnings ();
688 tree res = fold_binary (cond_code, boolean_type_node, op0, op1);
689 if (res)
690 while (CONVERT_EXPR_P (res))
691 res = TREE_OPERAND (res, 0);
693 fold_undefer_overflow_warnings ((res && is_gimple_min_invariant (res)),
694 stmt, WARN_STRICT_OVERFLOW_CONDITIONAL);
696 /* If we have not simplified the condition down to an invariant,
697 then use the pass specific callback to simplify the condition. */
698 if (!res
699 || !is_gimple_min_invariant (res))
700 res = (*simplify) (dummy_cond, stmt, avail_exprs_stack, e->src);
702 return res;
705 /* Copy debug stmts from DEST's chain of single predecessors up to
706 SRC, so that we don't lose the bindings as PHI nodes are introduced
707 when DEST gains new predecessors. */
708 void
709 propagate_threaded_block_debug_into (basic_block dest, basic_block src)
711 if (!MAY_HAVE_DEBUG_BIND_STMTS)
712 return;
714 if (!single_pred_p (dest))
715 return;
717 gcc_checking_assert (dest != src);
719 gimple_stmt_iterator gsi = gsi_after_labels (dest);
720 int i = 0;
721 const int alloc_count = 16; // ?? Should this be a PARAM?
723 /* Estimate the number of debug vars overridden in the beginning of
724 DEST, to tell how many we're going to need to begin with. */
725 for (gimple_stmt_iterator si = gsi;
726 i * 4 <= alloc_count * 3 && !gsi_end_p (si); gsi_next (&si))
728 gimple *stmt = gsi_stmt (si);
729 if (!is_gimple_debug (stmt))
730 break;
731 if (gimple_debug_nonbind_marker_p (stmt))
732 continue;
733 i++;
736 auto_vec<tree, alloc_count> fewvars;
737 hash_set<tree> *vars = NULL;
739 /* If we're already starting with 3/4 of alloc_count, go for a
740 hash_set, otherwise start with an unordered stack-allocated
741 VEC. */
742 if (i * 4 > alloc_count * 3)
743 vars = new hash_set<tree>;
745 /* Now go through the initial debug stmts in DEST again, this time
746 actually inserting in VARS or FEWVARS. Don't bother checking for
747 duplicates in FEWVARS. */
748 for (gimple_stmt_iterator si = gsi; !gsi_end_p (si); gsi_next (&si))
750 gimple *stmt = gsi_stmt (si);
751 if (!is_gimple_debug (stmt))
752 break;
754 tree var;
756 if (gimple_debug_bind_p (stmt))
757 var = gimple_debug_bind_get_var (stmt);
758 else if (gimple_debug_source_bind_p (stmt))
759 var = gimple_debug_source_bind_get_var (stmt);
760 else if (gimple_debug_nonbind_marker_p (stmt))
761 continue;
762 else
763 gcc_unreachable ();
765 if (vars)
766 vars->add (var);
767 else
768 fewvars.quick_push (var);
771 basic_block bb = dest;
775 bb = single_pred (bb);
776 for (gimple_stmt_iterator si = gsi_last_bb (bb);
777 !gsi_end_p (si); gsi_prev (&si))
779 gimple *stmt = gsi_stmt (si);
780 if (!is_gimple_debug (stmt))
781 continue;
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 if (gimple_debug_nonbind_marker_p (stmt))
790 continue;
791 else
792 gcc_unreachable ();
794 /* Discard debug bind overlaps. Unlike stmts from src,
795 copied into a new block that will precede BB, debug bind
796 stmts in bypassed BBs may actually be discarded if
797 they're overwritten by subsequent debug bind stmts. We
798 want to copy binds for all modified variables, so that we
799 retain a bind to the shared def if there is one, or to a
800 newly introduced PHI node if there is one. Our bind will
801 end up reset if the value is dead, but that implies the
802 variable couldn't have survived, so it's fine. We are
803 not actually running the code that performed the binds at
804 this point, we're just adding binds so that they survive
805 the new confluence, so markers should not be copied. */
806 if (vars && vars->add (var))
807 continue;
808 else if (!vars)
810 int i = fewvars.length ();
811 while (i--)
812 if (fewvars[i] == var)
813 break;
814 if (i >= 0)
815 continue;
816 else if (fewvars.length () < (unsigned) alloc_count)
817 fewvars.quick_push (var);
818 else
820 vars = new hash_set<tree>;
821 for (i = 0; i < alloc_count; i++)
822 vars->add (fewvars[i]);
823 fewvars.release ();
824 vars->add (var);
828 stmt = gimple_copy (stmt);
829 /* ??? Should we drop the location of the copy to denote
830 they're artificial bindings? */
831 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
834 while (bb != src && single_pred_p (bb));
836 if (vars)
837 delete vars;
838 else if (fewvars.exists ())
839 fewvars.release ();
842 /* See if TAKEN_EDGE->dest is a threadable block with no side effecs (ie, it
843 need not be duplicated as part of the CFG/SSA updating process).
845 If it is threadable, add it to PATH and VISITED and recurse, ultimately
846 returning TRUE from the toplevel call. Otherwise do nothing and
847 return false.
849 DUMMY_COND, SIMPLIFY are used to try and simplify the condition at the
850 end of TAKEN_EDGE->dest.
852 The available expression table is referenced via AVAIL_EXPRS_STACK. */
854 static bool
855 thread_around_empty_blocks (edge taken_edge,
856 gcond *dummy_cond,
857 class avail_exprs_stack *avail_exprs_stack,
858 pfn_simplify simplify,
859 bitmap visited,
860 vec<jump_thread_edge *> *path)
862 basic_block bb = taken_edge->dest;
863 gimple_stmt_iterator gsi;
864 gimple *stmt;
865 tree cond;
867 /* The key property of these blocks is that they need not be duplicated
868 when threading. Thus they can not have visible side effects such
869 as PHI nodes. */
870 if (!gsi_end_p (gsi_start_phis (bb)))
871 return false;
873 /* Skip over DEBUG statements at the start of the block. */
874 gsi = gsi_start_nondebug_bb (bb);
876 /* If the block has no statements, but does have a single successor, then
877 it's just a forwarding block and we can thread through it trivially.
879 However, note that just threading through empty blocks with single
880 successors is not inherently profitable. For the jump thread to
881 be profitable, we must avoid a runtime conditional.
883 By taking the return value from the recursive call, we get the
884 desired effect of returning TRUE when we found a profitable jump
885 threading opportunity and FALSE otherwise.
887 This is particularly important when this routine is called after
888 processing a joiner block. Returning TRUE too aggressively in
889 that case results in pointless duplication of the joiner block. */
890 if (gsi_end_p (gsi))
892 if (single_succ_p (bb))
894 taken_edge = single_succ_edge (bb);
896 if ((taken_edge->flags & EDGE_DFS_BACK) != 0)
897 return false;
899 if (!bitmap_bit_p (visited, taken_edge->dest->index))
901 jump_thread_edge *x
902 = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
903 path->safe_push (x);
904 bitmap_set_bit (visited, taken_edge->dest->index);
905 return thread_around_empty_blocks (taken_edge,
906 dummy_cond,
907 avail_exprs_stack,
908 simplify,
909 visited,
910 path);
914 /* We have a block with no statements, but multiple successors? */
915 return false;
918 /* The only real statements this block can have are a control
919 flow altering statement. Anything else stops the thread. */
920 stmt = gsi_stmt (gsi);
921 if (gimple_code (stmt) != GIMPLE_COND
922 && gimple_code (stmt) != GIMPLE_GOTO
923 && gimple_code (stmt) != GIMPLE_SWITCH)
924 return false;
926 /* Extract and simplify the condition. */
927 cond = simplify_control_stmt_condition (taken_edge, stmt,
928 avail_exprs_stack, dummy_cond,
929 simplify);
931 /* If the condition can be statically computed and we have not already
932 visited the destination edge, then add the taken edge to our thread
933 path. */
934 if (cond != NULL_TREE
935 && (is_gimple_min_invariant (cond)
936 || TREE_CODE (cond) == CASE_LABEL_EXPR))
938 if (TREE_CODE (cond) == CASE_LABEL_EXPR)
939 taken_edge = find_edge (bb, label_to_block (CASE_LABEL (cond)));
940 else
941 taken_edge = find_taken_edge (bb, cond);
943 if ((taken_edge->flags & EDGE_DFS_BACK) != 0)
944 return false;
946 if (bitmap_bit_p (visited, taken_edge->dest->index))
947 return false;
948 bitmap_set_bit (visited, taken_edge->dest->index);
950 jump_thread_edge *x
951 = new jump_thread_edge (taken_edge, EDGE_NO_COPY_SRC_BLOCK);
952 path->safe_push (x);
954 thread_around_empty_blocks (taken_edge,
955 dummy_cond,
956 avail_exprs_stack,
957 simplify,
958 visited,
959 path);
960 return true;
963 return false;
966 /* We are exiting E->src, see if E->dest ends with a conditional
967 jump which has a known value when reached via E.
969 E->dest can have arbitrary side effects which, if threading is
970 successful, will be maintained.
972 Special care is necessary if E is a back edge in the CFG as we
973 may have already recorded equivalences for E->dest into our
974 various tables, including the result of the conditional at
975 the end of E->dest. Threading opportunities are severely
976 limited in that case to avoid short-circuiting the loop
977 incorrectly.
979 DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
980 to avoid allocating memory.
982 STACK is used to undo temporary equivalences created during the walk of
983 E->dest.
985 SIMPLIFY is a pass-specific function used to simplify statements.
987 Our caller is responsible for restoring the state of the expression
988 and const_and_copies stacks.
990 Positive return value is success. Zero return value is failure, but
991 the block can still be duplicated as a joiner in a jump thread path,
992 negative indicates the block should not be duplicated and thus is not
993 suitable for a joiner in a jump threading path. */
995 static int
996 thread_through_normal_block (edge e,
997 gcond *dummy_cond,
998 const_and_copies *const_and_copies,
999 avail_exprs_stack *avail_exprs_stack,
1000 evrp_range_analyzer *evrp_range_analyzer,
1001 pfn_simplify simplify,
1002 vec<jump_thread_edge *> *path,
1003 bitmap visited)
1005 /* We want to record any equivalences created by traversing E. */
1006 record_temporary_equivalences (e, const_and_copies, avail_exprs_stack);
1008 /* PHIs create temporary equivalences.
1009 Note that if we found a PHI that made the block non-threadable, then
1010 we need to bubble that up to our caller in the same manner we do
1011 when we prematurely stop processing statements below. */
1012 if (!record_temporary_equivalences_from_phis (e, const_and_copies,
1013 evrp_range_analyzer))
1014 return -1;
1016 /* Now walk each statement recording any context sensitive
1017 temporary equivalences we can detect. */
1018 gimple *stmt
1019 = record_temporary_equivalences_from_stmts_at_dest (e, const_and_copies,
1020 avail_exprs_stack,
1021 evrp_range_analyzer,
1022 simplify);
1024 /* There's two reasons STMT might be null, and distinguishing
1025 between them is important.
1027 First the block may not have had any statements. For example, it
1028 might have some PHIs and unconditionally transfer control elsewhere.
1029 Such blocks are suitable for jump threading, particularly as a
1030 joiner block.
1032 The second reason would be if we did not process all the statements
1033 in the block (because there were too many to make duplicating the
1034 block profitable. If we did not look at all the statements, then
1035 we may not have invalidated everything needing invalidation. Thus
1036 we must signal to our caller that this block is not suitable for
1037 use as a joiner in a threading path. */
1038 if (!stmt)
1040 /* First case. The statement simply doesn't have any instructions, but
1041 does have PHIs. */
1042 if (gsi_end_p (gsi_start_nondebug_bb (e->dest))
1043 && !gsi_end_p (gsi_start_phis (e->dest)))
1044 return 0;
1046 /* Second case. */
1047 return -1;
1050 /* If we stopped at a COND_EXPR or SWITCH_EXPR, see if we know which arm
1051 will be taken. */
1052 if (gimple_code (stmt) == GIMPLE_COND
1053 || gimple_code (stmt) == GIMPLE_GOTO
1054 || gimple_code (stmt) == GIMPLE_SWITCH)
1056 tree cond;
1058 /* Extract and simplify the condition. */
1059 cond = simplify_control_stmt_condition (e, stmt, avail_exprs_stack,
1060 dummy_cond, simplify);
1062 if (!cond)
1063 return 0;
1065 if (is_gimple_min_invariant (cond)
1066 || TREE_CODE (cond) == CASE_LABEL_EXPR)
1068 edge taken_edge;
1069 if (TREE_CODE (cond) == CASE_LABEL_EXPR)
1070 taken_edge = find_edge (e->dest,
1071 label_to_block (CASE_LABEL (cond)));
1072 else
1073 taken_edge = find_taken_edge (e->dest, cond);
1075 basic_block dest = (taken_edge ? taken_edge->dest : NULL);
1077 /* DEST could be NULL for a computed jump to an absolute
1078 address. */
1079 if (dest == NULL
1080 || dest == e->dest
1081 || (taken_edge->flags & EDGE_DFS_BACK) != 0
1082 || bitmap_bit_p (visited, dest->index))
1083 return 0;
1085 /* Only push the EDGE_START_JUMP_THREAD marker if this is
1086 first edge on the path. */
1087 if (path->length () == 0)
1089 jump_thread_edge *x
1090 = new jump_thread_edge (e, EDGE_START_JUMP_THREAD);
1091 path->safe_push (x);
1094 jump_thread_edge *x
1095 = new jump_thread_edge (taken_edge, EDGE_COPY_SRC_BLOCK);
1096 path->safe_push (x);
1098 /* See if we can thread through DEST as well, this helps capture
1099 secondary effects of threading without having to re-run DOM or
1100 VRP.
1102 We don't want to thread back to a block we have already
1103 visited. This may be overly conservative. */
1104 bitmap_set_bit (visited, dest->index);
1105 bitmap_set_bit (visited, e->dest->index);
1106 thread_around_empty_blocks (taken_edge,
1107 dummy_cond,
1108 avail_exprs_stack,
1109 simplify,
1110 visited,
1111 path);
1112 return 1;
1115 return 0;
1118 /* We are exiting E->src, see if E->dest ends with a conditional
1119 jump which has a known value when reached via E.
1121 DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
1122 to avoid allocating memory.
1124 CONST_AND_COPIES is used to undo temporary equivalences created during the
1125 walk of E->dest.
1127 The available expression table is referenced vai AVAIL_EXPRS_STACK.
1129 SIMPLIFY is a pass-specific function used to simplify statements. */
1131 static void
1132 thread_across_edge (gcond *dummy_cond,
1133 edge e,
1134 class const_and_copies *const_and_copies,
1135 class avail_exprs_stack *avail_exprs_stack,
1136 class evrp_range_analyzer *evrp_range_analyzer,
1137 pfn_simplify simplify)
1139 bitmap visited = BITMAP_ALLOC (NULL);
1141 const_and_copies->push_marker ();
1142 avail_exprs_stack->push_marker ();
1143 if (evrp_range_analyzer)
1144 evrp_range_analyzer->push_marker ();
1146 stmt_count = 0;
1148 vec<jump_thread_edge *> *path = new vec<jump_thread_edge *> ();
1149 bitmap_clear (visited);
1150 bitmap_set_bit (visited, e->src->index);
1151 bitmap_set_bit (visited, e->dest->index);
1153 int threaded;
1154 if ((e->flags & EDGE_DFS_BACK) == 0)
1155 threaded = thread_through_normal_block (e, dummy_cond,
1156 const_and_copies,
1157 avail_exprs_stack,
1158 evrp_range_analyzer,
1159 simplify, path,
1160 visited);
1161 else
1162 threaded = 0;
1164 if (threaded > 0)
1166 propagate_threaded_block_debug_into (path->last ()->e->dest,
1167 e->dest);
1168 const_and_copies->pop_to_marker ();
1169 avail_exprs_stack->pop_to_marker ();
1170 if (evrp_range_analyzer)
1171 evrp_range_analyzer->pop_to_marker ();
1172 BITMAP_FREE (visited);
1173 register_jump_thread (path);
1174 return;
1176 else
1178 /* Negative and zero return values indicate no threading was possible,
1179 thus there should be no edges on the thread path and no need to walk
1180 through the vector entries. */
1181 gcc_assert (path->length () == 0);
1182 path->release ();
1183 delete path;
1185 /* A negative status indicates the target block was deemed too big to
1186 duplicate. Just quit now rather than trying to use the block as
1187 a joiner in a jump threading path.
1189 This prevents unnecessary code growth, but more importantly if we
1190 do not look at all the statements in the block, then we may have
1191 missed some invalidations if we had traversed a backedge! */
1192 if (threaded < 0)
1194 BITMAP_FREE (visited);
1195 const_and_copies->pop_to_marker ();
1196 avail_exprs_stack->pop_to_marker ();
1197 if (evrp_range_analyzer)
1198 evrp_range_analyzer->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 avail_exprs_stack->pop_to_marker ();
1226 if (evrp_range_analyzer)
1227 evrp_range_analyzer->pop_to_marker ();
1228 BITMAP_FREE (visited);
1229 return;
1232 /* Look at each successor of E->dest to see if we can thread through it. */
1233 FOR_EACH_EDGE (taken_edge, ei, e->dest->succs)
1235 if ((e->flags & EDGE_DFS_BACK) != 0
1236 || (taken_edge->flags & EDGE_DFS_BACK) != 0)
1237 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 avail_exprs_stack->push_marker ();
1243 if (evrp_range_analyzer)
1244 evrp_range_analyzer->push_marker ();
1246 /* Avoid threading to any block we have already visited. */
1247 bitmap_clear (visited);
1248 bitmap_set_bit (visited, e->src->index);
1249 bitmap_set_bit (visited, e->dest->index);
1250 bitmap_set_bit (visited, taken_edge->dest->index);
1251 vec<jump_thread_edge *> *path = new vec<jump_thread_edge *> ();
1253 /* Record whether or not we were able to thread through a successor
1254 of E->dest. */
1255 jump_thread_edge *x = new jump_thread_edge (e, EDGE_START_JUMP_THREAD);
1256 path->safe_push (x);
1258 x = new jump_thread_edge (taken_edge, EDGE_COPY_SRC_JOINER_BLOCK);
1259 path->safe_push (x);
1260 found = false;
1261 found = thread_around_empty_blocks (taken_edge,
1262 dummy_cond,
1263 avail_exprs_stack,
1264 simplify,
1265 visited,
1266 path);
1268 if (!found)
1269 found = thread_through_normal_block (path->last ()->e, dummy_cond,
1270 const_and_copies,
1271 avail_exprs_stack,
1272 evrp_range_analyzer,
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
1285 delete_jump_thread_path (path);
1287 /* And unwind the equivalence table. */
1288 if (evrp_range_analyzer)
1289 evrp_range_analyzer->pop_to_marker ();
1290 avail_exprs_stack->pop_to_marker ();
1291 const_and_copies->pop_to_marker ();
1293 BITMAP_FREE (visited);
1296 if (evrp_range_analyzer)
1297 evrp_range_analyzer->pop_to_marker ();
1298 const_and_copies->pop_to_marker ();
1299 avail_exprs_stack->pop_to_marker ();
1302 /* Examine the outgoing edges from BB and conditionally
1303 try to thread them.
1305 DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
1306 to avoid allocating memory.
1308 CONST_AND_COPIES is used to undo temporary equivalences created during the
1309 walk of E->dest.
1311 The available expression table is referenced vai AVAIL_EXPRS_STACK.
1313 SIMPLIFY is a pass-specific function used to simplify statements. */
1315 void
1316 thread_outgoing_edges (basic_block bb, gcond *dummy_cond,
1317 class const_and_copies *const_and_copies,
1318 class avail_exprs_stack *avail_exprs_stack,
1319 class evrp_range_analyzer *evrp_range_analyzer,
1320 tree (*simplify) (gimple *, gimple *,
1321 class avail_exprs_stack *,
1322 basic_block))
1324 int flags = (EDGE_IGNORE | EDGE_COMPLEX | EDGE_ABNORMAL);
1325 gimple *last;
1327 /* If we have an outgoing edge to a block with multiple incoming and
1328 outgoing edges, then we may be able to thread the edge, i.e., we
1329 may be able to statically determine which of the outgoing edges
1330 will be traversed when the incoming edge from BB is traversed. */
1331 if (single_succ_p (bb)
1332 && (single_succ_edge (bb)->flags & flags) == 0
1333 && potentially_threadable_block (single_succ (bb)))
1335 thread_across_edge (dummy_cond, single_succ_edge (bb),
1336 const_and_copies, avail_exprs_stack,
1337 evrp_range_analyzer, simplify);
1339 else if ((last = last_stmt (bb))
1340 && gimple_code (last) == GIMPLE_COND
1341 && EDGE_COUNT (bb->succs) == 2
1342 && (EDGE_SUCC (bb, 0)->flags & flags) == 0
1343 && (EDGE_SUCC (bb, 1)->flags & flags) == 0)
1345 edge true_edge, false_edge;
1347 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1349 /* Only try to thread the edge if it reaches a target block with
1350 more than one predecessor and more than one successor. */
1351 if (potentially_threadable_block (true_edge->dest))
1352 thread_across_edge (dummy_cond, true_edge,
1353 const_and_copies, avail_exprs_stack,
1354 evrp_range_analyzer, simplify);
1356 /* Similarly for the ELSE arm. */
1357 if (potentially_threadable_block (false_edge->dest))
1358 thread_across_edge (dummy_cond, false_edge,
1359 const_and_copies, avail_exprs_stack,
1360 evrp_range_analyzer, simplify);