* config/darwin-c.c: Remove c-tree.h include.
[official-gcc.git] / gcc / tree-ssa-threadedge.c
blob631d97b8bfaa11cd31a2578aeb685d44f8bf6f22
1 /* SSA Jump Threading
2 Copyright (C) 2005, 2006, 2007, 2008, 2009 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 "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "tm_p.h"
28 #include "basic-block.h"
29 #include "cfgloop.h"
30 #include "output.h"
31 #include "expr.h"
32 #include "function.h"
33 #include "diagnostic.h"
34 #include "timevar.h"
35 #include "tree-dump.h"
36 #include "tree-flow.h"
37 #include "tree-pass.h"
38 #include "tree-ssa-propagate.h"
39 #include "langhooks.h"
40 #include "params.h"
42 /* To avoid code explosion due to jump threading, we limit the
43 number of statements we are going to copy. This variable
44 holds the number of statements currently seen that we'll have
45 to copy as part of the jump threading process. */
46 static int stmt_count;
48 /* Array to record value-handles per SSA_NAME. */
49 VEC(tree,heap) *ssa_name_values;
51 /* Set the value for the SSA name NAME to VALUE. */
53 void
54 set_ssa_name_value (tree name, tree value)
56 if (SSA_NAME_VERSION (name) >= VEC_length (tree, ssa_name_values))
57 VEC_safe_grow_cleared (tree, heap, ssa_name_values,
58 SSA_NAME_VERSION (name) + 1);
59 VEC_replace (tree, ssa_name_values, SSA_NAME_VERSION (name), value);
62 /* Initialize the per SSA_NAME value-handles array. Returns it. */
63 void
64 threadedge_initialize_values (void)
66 gcc_assert (ssa_name_values == NULL);
67 ssa_name_values = VEC_alloc(tree, heap, num_ssa_names);
70 /* Free the per SSA_NAME value-handle array. */
71 void
72 threadedge_finalize_values (void)
74 VEC_free(tree, heap, ssa_name_values);
77 /* Return TRUE if we may be able to thread an incoming edge into
78 BB to an outgoing edge from BB. Return FALSE otherwise. */
80 bool
81 potentially_threadable_block (basic_block bb)
83 gimple_stmt_iterator gsi;
85 /* If BB has a single successor or a single predecessor, then
86 there is no threading opportunity. */
87 if (single_succ_p (bb) || single_pred_p (bb))
88 return false;
90 /* If BB does not end with a conditional, switch or computed goto,
91 then there is no threading opportunity. */
92 gsi = gsi_last_bb (bb);
93 if (gsi_end_p (gsi)
94 || ! gsi_stmt (gsi)
95 || (gimple_code (gsi_stmt (gsi)) != GIMPLE_COND
96 && gimple_code (gsi_stmt (gsi)) != GIMPLE_GOTO
97 && gimple_code (gsi_stmt (gsi)) != GIMPLE_SWITCH))
98 return false;
100 return true;
103 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
104 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
105 BB. If no such ASSERT_EXPR is found, return OP. */
107 static tree
108 lhs_of_dominating_assert (tree op, basic_block bb, gimple stmt)
110 imm_use_iterator imm_iter;
111 gimple use_stmt;
112 use_operand_p use_p;
114 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
116 use_stmt = USE_STMT (use_p);
117 if (use_stmt != stmt
118 && gimple_assign_single_p (use_stmt)
119 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ASSERT_EXPR
120 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) == op
121 && dominated_by_p (CDI_DOMINATORS, bb, gimple_bb (use_stmt)))
123 return gimple_assign_lhs (use_stmt);
126 return op;
129 /* We record temporary equivalences created by PHI nodes or
130 statements within the target block. Doing so allows us to
131 identify more jump threading opportunities, even in blocks
132 with side effects.
134 We keep track of those temporary equivalences in a stack
135 structure so that we can unwind them when we're done processing
136 a particular edge. This routine handles unwinding the data
137 structures. */
139 static void
140 remove_temporary_equivalences (VEC(tree, heap) **stack)
142 while (VEC_length (tree, *stack) > 0)
144 tree prev_value, dest;
146 dest = VEC_pop (tree, *stack);
148 /* A NULL value indicates we should stop unwinding, otherwise
149 pop off the next entry as they're recorded in pairs. */
150 if (dest == NULL)
151 break;
153 prev_value = VEC_pop (tree, *stack);
154 set_ssa_name_value (dest, prev_value);
158 /* Record a temporary equivalence, saving enough information so that
159 we can restore the state of recorded equivalences when we're
160 done processing the current edge. */
162 static void
163 record_temporary_equivalence (tree x, tree y, VEC(tree, heap) **stack)
165 tree prev_x = SSA_NAME_VALUE (x);
167 if (TREE_CODE (y) == SSA_NAME)
169 tree tmp = SSA_NAME_VALUE (y);
170 y = tmp ? tmp : y;
173 set_ssa_name_value (x, y);
174 VEC_reserve (tree, heap, *stack, 2);
175 VEC_quick_push (tree, *stack, prev_x);
176 VEC_quick_push (tree, *stack, x);
179 /* Record temporary equivalences created by PHIs at the target of the
180 edge E. Record unwind information for the equivalences onto STACK.
182 If a PHI which prevents threading is encountered, then return FALSE
183 indicating we should not thread this edge, else return TRUE. */
185 static bool
186 record_temporary_equivalences_from_phis (edge e, VEC(tree, heap) **stack)
188 gimple_stmt_iterator gsi;
190 /* Each PHI creates a temporary equivalence, record them.
191 These are context sensitive equivalences and will be removed
192 later. */
193 for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
195 gimple phi = gsi_stmt (gsi);
196 tree src = PHI_ARG_DEF_FROM_EDGE (phi, e);
197 tree dst = gimple_phi_result (phi);
199 /* If the desired argument is not the same as this PHI's result
200 and it is set by a PHI in E->dest, then we can not thread
201 through E->dest. */
202 if (src != dst
203 && TREE_CODE (src) == SSA_NAME
204 && gimple_code (SSA_NAME_DEF_STMT (src)) == GIMPLE_PHI
205 && gimple_bb (SSA_NAME_DEF_STMT (src)) == e->dest)
206 return false;
208 /* We consider any non-virtual PHI as a statement since it
209 count result in a constant assignment or copy operation. */
210 if (is_gimple_reg (dst))
211 stmt_count++;
213 record_temporary_equivalence (dst, src, stack);
215 return true;
218 /* Fold the RHS of an assignment statement and return it as a tree.
219 May return NULL_TREE if no simplification is possible. */
221 static tree
222 fold_assignment_stmt (gimple stmt)
224 enum tree_code subcode = gimple_assign_rhs_code (stmt);
226 switch (get_gimple_rhs_class (subcode))
228 case GIMPLE_SINGLE_RHS:
230 tree rhs = gimple_assign_rhs1 (stmt);
232 if (TREE_CODE (rhs) == COND_EXPR)
234 /* Sadly, we have to handle conditional assignments specially
235 here, because fold expects all the operands of an expression
236 to be folded before the expression itself is folded, but we
237 can't just substitute the folded condition here. */
238 tree cond = fold (COND_EXPR_COND (rhs));
239 if (cond == boolean_true_node)
240 rhs = COND_EXPR_THEN (rhs);
241 else if (cond == boolean_false_node)
242 rhs = COND_EXPR_ELSE (rhs);
245 return fold (rhs);
247 break;
248 case GIMPLE_UNARY_RHS:
250 tree lhs = gimple_assign_lhs (stmt);
251 tree op0 = gimple_assign_rhs1 (stmt);
252 return fold_unary (subcode, TREE_TYPE (lhs), op0);
254 break;
255 case GIMPLE_BINARY_RHS:
257 tree lhs = gimple_assign_lhs (stmt);
258 tree op0 = gimple_assign_rhs1 (stmt);
259 tree op1 = gimple_assign_rhs2 (stmt);
260 return fold_binary (subcode, TREE_TYPE (lhs), op0, op1);
262 break;
263 default:
264 gcc_unreachable ();
268 /* Try to simplify each statement in E->dest, ultimately leading to
269 a simplification of the COND_EXPR at the end of E->dest.
271 Record unwind information for temporary equivalences onto STACK.
273 Use SIMPLIFY (a pointer to a callback function) to further simplify
274 statements using pass specific information.
276 We might consider marking just those statements which ultimately
277 feed the COND_EXPR. It's not clear if the overhead of bookkeeping
278 would be recovered by trying to simplify fewer statements.
280 If we are able to simplify a statement into the form
281 SSA_NAME = (SSA_NAME | gimple invariant), then we can record
282 a context sensitive equivalence which may help us simplify
283 later statements in E->dest. */
285 static gimple
286 record_temporary_equivalences_from_stmts_at_dest (edge e,
287 VEC(tree, heap) **stack,
288 tree (*simplify) (gimple,
289 gimple))
291 gimple stmt = NULL;
292 gimple_stmt_iterator gsi;
293 int max_stmt_count;
295 max_stmt_count = PARAM_VALUE (PARAM_MAX_JUMP_THREAD_DUPLICATION_STMTS);
297 /* Walk through each statement in the block recording equivalences
298 we discover. Note any equivalences we discover are context
299 sensitive (ie, are dependent on traversing E) and must be unwound
300 when we're finished processing E. */
301 for (gsi = gsi_start_bb (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
303 tree cached_lhs = NULL;
305 stmt = gsi_stmt (gsi);
307 /* Ignore empty statements and labels. */
308 if (gimple_code (stmt) == GIMPLE_NOP
309 || gimple_code (stmt) == GIMPLE_LABEL
310 || is_gimple_debug (stmt))
311 continue;
313 /* If the statement has volatile operands, then we assume we
314 can not thread through this block. This is overly
315 conservative in some ways. */
316 if (gimple_code (stmt) == GIMPLE_ASM && gimple_asm_volatile_p (stmt))
317 return NULL;
319 /* If duplicating this block is going to cause too much code
320 expansion, then do not thread through this block. */
321 stmt_count++;
322 if (stmt_count > max_stmt_count)
323 return NULL;
325 /* If this is not a statement that sets an SSA_NAME to a new
326 value, then do not try to simplify this statement as it will
327 not simplify in any way that is helpful for jump threading. */
328 if ((gimple_code (stmt) != GIMPLE_ASSIGN
329 || TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
330 && (gimple_code (stmt) != GIMPLE_CALL
331 || gimple_call_lhs (stmt) == NULL_TREE
332 || TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME))
333 continue;
335 /* The result of __builtin_object_size depends on all the arguments
336 of a phi node. Temporarily using only one edge produces invalid
337 results. For example
339 if (x < 6)
340 goto l;
341 else
342 goto l;
345 r = PHI <&w[2].a[1](2), &a.a[6](3)>
346 __builtin_object_size (r, 0)
348 The result of __builtin_object_size is defined to be the maximum of
349 remaining bytes. If we use only one edge on the phi, the result will
350 change to be the remaining bytes for the corresponding phi argument.
352 Similarly for __builtin_constant_p:
354 r = PHI <1(2), 2(3)>
355 __builtin_constant_p (r)
357 Both PHI arguments are constant, but x ? 1 : 2 is still not
358 constant. */
360 if (is_gimple_call (stmt))
362 tree fndecl = gimple_call_fndecl (stmt);
363 if (fndecl
364 && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_OBJECT_SIZE
365 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P))
366 continue;
369 /* At this point we have a statement which assigns an RHS to an
370 SSA_VAR on the LHS. We want to try and simplify this statement
371 to expose more context sensitive equivalences which in turn may
372 allow us to simplify the condition at the end of the loop.
374 Handle simple copy operations as well as implied copies from
375 ASSERT_EXPRs. */
376 if (gimple_assign_single_p (stmt)
377 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
378 cached_lhs = gimple_assign_rhs1 (stmt);
379 else if (gimple_assign_single_p (stmt)
380 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
381 cached_lhs = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
382 else
384 /* A statement that is not a trivial copy or ASSERT_EXPR.
385 We're going to temporarily copy propagate the operands
386 and see if that allows us to simplify this statement. */
387 tree *copy;
388 ssa_op_iter iter;
389 use_operand_p use_p;
390 unsigned int num, i = 0;
392 num = NUM_SSA_OPERANDS (stmt, (SSA_OP_USE | SSA_OP_VUSE));
393 copy = XCNEWVEC (tree, num);
395 /* Make a copy of the uses & vuses into USES_COPY, then cprop into
396 the operands. */
397 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE | SSA_OP_VUSE)
399 tree tmp = NULL;
400 tree use = USE_FROM_PTR (use_p);
402 copy[i++] = use;
403 if (TREE_CODE (use) == SSA_NAME)
404 tmp = SSA_NAME_VALUE (use);
405 if (tmp)
406 SET_USE (use_p, tmp);
409 /* Try to fold/lookup the new expression. Inserting the
410 expression into the hash table is unlikely to help. */
411 if (is_gimple_call (stmt))
412 cached_lhs = fold_call_stmt (stmt, false);
413 else
414 cached_lhs = fold_assignment_stmt (stmt);
416 if (!cached_lhs
417 || (TREE_CODE (cached_lhs) != SSA_NAME
418 && !is_gimple_min_invariant (cached_lhs)))
419 cached_lhs = (*simplify) (stmt, stmt);
421 /* Restore the statement's original uses/defs. */
422 i = 0;
423 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE | SSA_OP_VUSE)
424 SET_USE (use_p, copy[i++]);
426 free (copy);
429 /* Record the context sensitive equivalence if we were able
430 to simplify this statement. */
431 if (cached_lhs
432 && (TREE_CODE (cached_lhs) == SSA_NAME
433 || is_gimple_min_invariant (cached_lhs)))
434 record_temporary_equivalence (gimple_get_lhs (stmt), cached_lhs, stack);
436 return stmt;
439 /* Simplify the control statement at the end of the block E->dest.
441 To avoid allocating memory unnecessarily, a scratch GIMPLE_COND
442 is available to use/clobber in DUMMY_COND.
444 Use SIMPLIFY (a pointer to a callback function) to further simplify
445 a condition using pass specific information.
447 Return the simplified condition or NULL if simplification could
448 not be performed. */
450 static tree
451 simplify_control_stmt_condition (edge e,
452 gimple stmt,
453 gimple dummy_cond,
454 tree (*simplify) (gimple, gimple),
455 bool handle_dominating_asserts)
457 tree cond, cached_lhs;
458 enum gimple_code code = gimple_code (stmt);
460 /* For comparisons, we have to update both operands, then try
461 to simplify the comparison. */
462 if (code == GIMPLE_COND)
464 tree op0, op1;
465 enum tree_code cond_code;
467 op0 = gimple_cond_lhs (stmt);
468 op1 = gimple_cond_rhs (stmt);
469 cond_code = gimple_cond_code (stmt);
471 /* Get the current value of both operands. */
472 if (TREE_CODE (op0) == SSA_NAME)
474 tree tmp = SSA_NAME_VALUE (op0);
475 if (tmp)
476 op0 = tmp;
479 if (TREE_CODE (op1) == SSA_NAME)
481 tree tmp = SSA_NAME_VALUE (op1);
482 if (tmp)
483 op1 = tmp;
486 if (handle_dominating_asserts)
488 /* Now see if the operand was consumed by an ASSERT_EXPR
489 which dominates E->src. If so, we want to replace the
490 operand with the LHS of the ASSERT_EXPR. */
491 if (TREE_CODE (op0) == SSA_NAME)
492 op0 = lhs_of_dominating_assert (op0, e->src, stmt);
494 if (TREE_CODE (op1) == SSA_NAME)
495 op1 = lhs_of_dominating_assert (op1, e->src, stmt);
498 /* We may need to canonicalize the comparison. For
499 example, op0 might be a constant while op1 is an
500 SSA_NAME. Failure to canonicalize will cause us to
501 miss threading opportunities. */
502 if (tree_swap_operands_p (op0, op1, false))
504 tree tmp;
505 cond_code = swap_tree_comparison (cond_code);
506 tmp = op0;
507 op0 = op1;
508 op1 = tmp;
511 /* Stuff the operator and operands into our dummy conditional
512 expression. */
513 gimple_cond_set_code (dummy_cond, cond_code);
514 gimple_cond_set_lhs (dummy_cond, op0);
515 gimple_cond_set_rhs (dummy_cond, op1);
517 /* We absolutely do not care about any type conversions
518 we only care about a zero/nonzero value. */
519 fold_defer_overflow_warnings ();
521 cached_lhs = fold_binary (cond_code, boolean_type_node, op0, op1);
522 if (cached_lhs)
523 while (CONVERT_EXPR_P (cached_lhs))
524 cached_lhs = TREE_OPERAND (cached_lhs, 0);
526 fold_undefer_overflow_warnings ((cached_lhs
527 && is_gimple_min_invariant (cached_lhs)),
528 stmt, WARN_STRICT_OVERFLOW_CONDITIONAL);
530 /* If we have not simplified the condition down to an invariant,
531 then use the pass specific callback to simplify the condition. */
532 if (!cached_lhs
533 || !is_gimple_min_invariant (cached_lhs))
534 cached_lhs = (*simplify) (dummy_cond, stmt);
536 return cached_lhs;
539 if (code == GIMPLE_SWITCH)
540 cond = gimple_switch_index (stmt);
541 else if (code == GIMPLE_GOTO)
542 cond = gimple_goto_dest (stmt);
543 else
544 gcc_unreachable ();
546 /* We can have conditionals which just test the state of a variable
547 rather than use a relational operator. These are simpler to handle. */
548 if (TREE_CODE (cond) == SSA_NAME)
550 cached_lhs = cond;
552 /* Get the variable's current value from the equivalence chains.
554 It is possible to get loops in the SSA_NAME_VALUE chains
555 (consider threading the backedge of a loop where we have
556 a loop invariant SSA_NAME used in the condition. */
557 if (cached_lhs
558 && TREE_CODE (cached_lhs) == SSA_NAME
559 && SSA_NAME_VALUE (cached_lhs))
560 cached_lhs = SSA_NAME_VALUE (cached_lhs);
562 /* If we're dominated by a suitable ASSERT_EXPR, then
563 update CACHED_LHS appropriately. */
564 if (handle_dominating_asserts && TREE_CODE (cached_lhs) == SSA_NAME)
565 cached_lhs = lhs_of_dominating_assert (cached_lhs, e->src, stmt);
567 /* If we haven't simplified to an invariant yet, then use the
568 pass specific callback to try and simplify it further. */
569 if (cached_lhs && ! is_gimple_min_invariant (cached_lhs))
570 cached_lhs = (*simplify) (stmt, stmt);
572 else
573 cached_lhs = NULL;
575 return cached_lhs;
578 /* We are exiting E->src, see if E->dest ends with a conditional
579 jump which has a known value when reached via E.
581 Special care is necessary if E is a back edge in the CFG as we
582 may have already recorded equivalences for E->dest into our
583 various tables, including the result of the conditional at
584 the end of E->dest. Threading opportunities are severely
585 limited in that case to avoid short-circuiting the loop
586 incorrectly.
588 Note it is quite common for the first block inside a loop to
589 end with a conditional which is either always true or always
590 false when reached via the loop backedge. Thus we do not want
591 to blindly disable threading across a loop backedge.
593 DUMMY_COND is a shared cond_expr used by condition simplification as scratch,
594 to avoid allocating memory.
596 HANDLE_DOMINATING_ASSERTS is true if we should try to replace operands of
597 the simplified condition with left-hand sides of ASSERT_EXPRs they are
598 used in.
600 STACK is used to undo temporary equivalences created during the walk of
601 E->dest.
603 SIMPLIFY is a pass-specific function used to simplify statements. */
605 void
606 thread_across_edge (gimple dummy_cond,
607 edge e,
608 bool handle_dominating_asserts,
609 VEC(tree, heap) **stack,
610 tree (*simplify) (gimple, gimple))
612 gimple stmt;
614 /* If E is a backedge, then we want to verify that the COND_EXPR,
615 SWITCH_EXPR or GOTO_EXPR at the end of e->dest is not affected
616 by any statements in e->dest. If it is affected, then it is not
617 safe to thread this edge. */
618 if (e->flags & EDGE_DFS_BACK)
620 ssa_op_iter iter;
621 use_operand_p use_p;
622 gimple last = gsi_stmt (gsi_last_bb (e->dest));
624 FOR_EACH_SSA_USE_OPERAND (use_p, last, iter, SSA_OP_USE | SSA_OP_VUSE)
626 tree use = USE_FROM_PTR (use_p);
628 if (TREE_CODE (use) == SSA_NAME
629 && gimple_code (SSA_NAME_DEF_STMT (use)) != GIMPLE_PHI
630 && gimple_bb (SSA_NAME_DEF_STMT (use)) == e->dest)
631 goto fail;
635 stmt_count = 0;
637 /* PHIs create temporary equivalences. */
638 if (!record_temporary_equivalences_from_phis (e, stack))
639 goto fail;
641 /* Now walk each statement recording any context sensitive
642 temporary equivalences we can detect. */
643 stmt = record_temporary_equivalences_from_stmts_at_dest (e, stack, simplify);
644 if (!stmt)
645 goto fail;
647 /* If we stopped at a COND_EXPR or SWITCH_EXPR, see if we know which arm
648 will be taken. */
649 if (gimple_code (stmt) == GIMPLE_COND
650 || gimple_code (stmt) == GIMPLE_GOTO
651 || gimple_code (stmt) == GIMPLE_SWITCH)
653 tree cond;
655 /* Extract and simplify the condition. */
656 cond = simplify_control_stmt_condition (e, stmt, dummy_cond, simplify, handle_dominating_asserts);
658 if (cond && is_gimple_min_invariant (cond))
660 edge taken_edge = find_taken_edge (e->dest, cond);
661 basic_block dest = (taken_edge ? taken_edge->dest : NULL);
663 if (dest == e->dest)
664 goto fail;
666 remove_temporary_equivalences (stack);
667 register_jump_thread (e, taken_edge);
671 fail:
672 remove_temporary_equivalences (stack);