Daily bump.
[official-gcc.git] / gcc / tree-ssa-phiopt.c
blobcaea9ac723f399da2bf9488623ac91260be46dca
1 /* Optimization of PHI nodes by converting them into straightline code.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 "ggc.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "tm_p.h"
29 #include "basic-block.h"
30 #include "timevar.h"
31 #include "tree-flow.h"
32 #include "tree-pass.h"
33 #include "tree-dump.h"
34 #include "langhooks.h"
35 #include "pointer-set.h"
36 #include "domwalk.h"
37 #include "cfgloop.h"
38 #include "tree-data-ref.h"
40 static unsigned int tree_ssa_phiopt (void);
41 static unsigned int tree_ssa_phiopt_worker (bool);
42 static bool conditional_replacement (basic_block, basic_block,
43 edge, edge, gimple, tree, tree);
44 static bool value_replacement (basic_block, basic_block,
45 edge, edge, gimple, tree, tree);
46 static bool minmax_replacement (basic_block, basic_block,
47 edge, edge, gimple, tree, tree);
48 static bool abs_replacement (basic_block, basic_block,
49 edge, edge, gimple, tree, tree);
50 static bool cond_store_replacement (basic_block, basic_block, edge, edge,
51 struct pointer_set_t *);
52 static bool cond_if_else_store_replacement (basic_block, basic_block, basic_block);
53 static struct pointer_set_t * get_non_trapping (void);
54 static void replace_phi_edge_with_variable (basic_block, edge, gimple, tree);
56 /* This pass tries to replaces an if-then-else block with an
57 assignment. We have four kinds of transformations. Some of these
58 transformations are also performed by the ifcvt RTL optimizer.
60 Conditional Replacement
61 -----------------------
63 This transformation, implemented in conditional_replacement,
64 replaces
66 bb0:
67 if (cond) goto bb2; else goto bb1;
68 bb1:
69 bb2:
70 x = PHI <0 (bb1), 1 (bb0), ...>;
72 with
74 bb0:
75 x' = cond;
76 goto bb2;
77 bb2:
78 x = PHI <x' (bb0), ...>;
80 We remove bb1 as it becomes unreachable. This occurs often due to
81 gimplification of conditionals.
83 Value Replacement
84 -----------------
86 This transformation, implemented in value_replacement, replaces
88 bb0:
89 if (a != b) goto bb2; else goto bb1;
90 bb1:
91 bb2:
92 x = PHI <a (bb1), b (bb0), ...>;
94 with
96 bb0:
97 bb2:
98 x = PHI <b (bb0), ...>;
100 This opportunity can sometimes occur as a result of other
101 optimizations.
103 ABS Replacement
104 ---------------
106 This transformation, implemented in abs_replacement, replaces
108 bb0:
109 if (a >= 0) goto bb2; else goto bb1;
110 bb1:
111 x = -a;
112 bb2:
113 x = PHI <x (bb1), a (bb0), ...>;
115 with
117 bb0:
118 x' = ABS_EXPR< a >;
119 bb2:
120 x = PHI <x' (bb0), ...>;
122 MIN/MAX Replacement
123 -------------------
125 This transformation, minmax_replacement replaces
127 bb0:
128 if (a <= b) goto bb2; else goto bb1;
129 bb1:
130 bb2:
131 x = PHI <b (bb1), a (bb0), ...>;
133 with
135 bb0:
136 x' = MIN_EXPR (a, b)
137 bb2:
138 x = PHI <x' (bb0), ...>;
140 A similar transformation is done for MAX_EXPR. */
142 static unsigned int
143 tree_ssa_phiopt (void)
145 return tree_ssa_phiopt_worker (false);
148 /* This pass tries to transform conditional stores into unconditional
149 ones, enabling further simplifications with the simpler then and else
150 blocks. In particular it replaces this:
152 bb0:
153 if (cond) goto bb2; else goto bb1;
154 bb1:
155 *p = RHS;
156 bb2:
158 with
160 bb0:
161 if (cond) goto bb1; else goto bb2;
162 bb1:
163 condtmp' = *p;
164 bb2:
165 condtmp = PHI <RHS, condtmp'>
166 *p = condtmp;
168 This transformation can only be done under several constraints,
169 documented below. It also replaces:
171 bb0:
172 if (cond) goto bb2; else goto bb1;
173 bb1:
174 *p = RHS1;
175 goto bb3;
176 bb2:
177 *p = RHS2;
178 bb3:
180 with
182 bb0:
183 if (cond) goto bb3; else goto bb1;
184 bb1:
185 bb3:
186 condtmp = PHI <RHS1, RHS2>
187 *p = condtmp; */
189 static unsigned int
190 tree_ssa_cs_elim (void)
192 return tree_ssa_phiopt_worker (true);
195 /* For conditional store replacement we need a temporary to
196 put the old contents of the memory in. */
197 static tree condstoretemp;
199 /* The core routine of conditional store replacement and normal
200 phi optimizations. Both share much of the infrastructure in how
201 to match applicable basic block patterns. DO_STORE_ELIM is true
202 when we want to do conditional store replacement, false otherwise. */
203 static unsigned int
204 tree_ssa_phiopt_worker (bool do_store_elim)
206 basic_block bb;
207 basic_block *bb_order;
208 unsigned n, i;
209 bool cfgchanged = false;
210 struct pointer_set_t *nontrap = 0;
212 if (do_store_elim)
214 condstoretemp = NULL_TREE;
215 /* Calculate the set of non-trapping memory accesses. */
216 nontrap = get_non_trapping ();
219 /* Search every basic block for COND_EXPR we may be able to optimize.
221 We walk the blocks in order that guarantees that a block with
222 a single predecessor is processed before the predecessor.
223 This ensures that we collapse inner ifs before visiting the
224 outer ones, and also that we do not try to visit a removed
225 block. */
226 bb_order = blocks_in_phiopt_order ();
227 n = n_basic_blocks - NUM_FIXED_BLOCKS;
229 for (i = 0; i < n; i++)
231 gimple cond_stmt, phi;
232 basic_block bb1, bb2;
233 edge e1, e2;
234 tree arg0, arg1;
236 bb = bb_order[i];
238 cond_stmt = last_stmt (bb);
239 /* Check to see if the last statement is a GIMPLE_COND. */
240 if (!cond_stmt
241 || gimple_code (cond_stmt) != GIMPLE_COND)
242 continue;
244 e1 = EDGE_SUCC (bb, 0);
245 bb1 = e1->dest;
246 e2 = EDGE_SUCC (bb, 1);
247 bb2 = e2->dest;
249 /* We cannot do the optimization on abnormal edges. */
250 if ((e1->flags & EDGE_ABNORMAL) != 0
251 || (e2->flags & EDGE_ABNORMAL) != 0)
252 continue;
254 /* If either bb1's succ or bb2 or bb2's succ is non NULL. */
255 if (EDGE_COUNT (bb1->succs) == 0
256 || bb2 == NULL
257 || EDGE_COUNT (bb2->succs) == 0)
258 continue;
260 /* Find the bb which is the fall through to the other. */
261 if (EDGE_SUCC (bb1, 0)->dest == bb2)
263 else if (EDGE_SUCC (bb2, 0)->dest == bb1)
265 basic_block bb_tmp = bb1;
266 edge e_tmp = e1;
267 bb1 = bb2;
268 bb2 = bb_tmp;
269 e1 = e2;
270 e2 = e_tmp;
272 else if (do_store_elim
273 && EDGE_SUCC (bb1, 0)->dest == EDGE_SUCC (bb2, 0)->dest)
275 basic_block bb3 = EDGE_SUCC (bb1, 0)->dest;
277 if (!single_succ_p (bb1)
278 || (EDGE_SUCC (bb1, 0)->flags & EDGE_FALLTHRU) == 0
279 || !single_succ_p (bb2)
280 || (EDGE_SUCC (bb2, 0)->flags & EDGE_FALLTHRU) == 0
281 || EDGE_COUNT (bb3->preds) != 2)
282 continue;
283 if (cond_if_else_store_replacement (bb1, bb2, bb3))
284 cfgchanged = true;
285 continue;
287 else
288 continue;
290 e1 = EDGE_SUCC (bb1, 0);
292 /* Make sure that bb1 is just a fall through. */
293 if (!single_succ_p (bb1)
294 || (e1->flags & EDGE_FALLTHRU) == 0)
295 continue;
297 /* Also make sure that bb1 only have one predecessor and that it
298 is bb. */
299 if (!single_pred_p (bb1)
300 || single_pred (bb1) != bb)
301 continue;
303 if (do_store_elim)
305 /* bb1 is the middle block, bb2 the join block, bb the split block,
306 e1 the fallthrough edge from bb1 to bb2. We can't do the
307 optimization if the join block has more than two predecessors. */
308 if (EDGE_COUNT (bb2->preds) > 2)
309 continue;
310 if (cond_store_replacement (bb1, bb2, e1, e2, nontrap))
311 cfgchanged = true;
313 else
315 gimple_seq phis = phi_nodes (bb2);
316 gimple_stmt_iterator gsi;
318 /* Check to make sure that there is only one non-virtual PHI node.
319 TODO: we could do it with more than one iff the other PHI nodes
320 have the same elements for these two edges. */
321 phi = NULL;
322 for (gsi = gsi_start (phis); !gsi_end_p (gsi); gsi_next (&gsi))
324 if (!is_gimple_reg (gimple_phi_result (gsi_stmt (gsi))))
325 continue;
326 if (phi)
328 phi = NULL;
329 break;
331 phi = gsi_stmt (gsi);
333 if (!phi)
334 continue;
336 arg0 = gimple_phi_arg_def (phi, e1->dest_idx);
337 arg1 = gimple_phi_arg_def (phi, e2->dest_idx);
339 /* Something is wrong if we cannot find the arguments in the PHI
340 node. */
341 gcc_assert (arg0 != NULL && arg1 != NULL);
343 /* Do the replacement of conditional if it can be done. */
344 if (conditional_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
345 cfgchanged = true;
346 else if (value_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
347 cfgchanged = true;
348 else if (abs_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
349 cfgchanged = true;
350 else if (minmax_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
351 cfgchanged = true;
355 free (bb_order);
357 if (do_store_elim)
358 pointer_set_destroy (nontrap);
359 /* If the CFG has changed, we should cleanup the CFG. */
360 if (cfgchanged && do_store_elim)
362 /* In cond-store replacement we have added some loads on edges
363 and new VOPS (as we moved the store, and created a load). */
364 gsi_commit_edge_inserts ();
365 return TODO_cleanup_cfg | TODO_update_ssa_only_virtuals;
367 else if (cfgchanged)
368 return TODO_cleanup_cfg;
369 return 0;
372 /* Returns the list of basic blocks in the function in an order that guarantees
373 that if a block X has just a single predecessor Y, then Y is after X in the
374 ordering. */
376 basic_block *
377 blocks_in_phiopt_order (void)
379 basic_block x, y;
380 basic_block *order = XNEWVEC (basic_block, n_basic_blocks);
381 unsigned n = n_basic_blocks - NUM_FIXED_BLOCKS;
382 unsigned np, i;
383 sbitmap visited = sbitmap_alloc (last_basic_block);
385 #define MARK_VISITED(BB) (SET_BIT (visited, (BB)->index))
386 #define VISITED_P(BB) (TEST_BIT (visited, (BB)->index))
388 sbitmap_zero (visited);
390 MARK_VISITED (ENTRY_BLOCK_PTR);
391 FOR_EACH_BB (x)
393 if (VISITED_P (x))
394 continue;
396 /* Walk the predecessors of x as long as they have precisely one
397 predecessor and add them to the list, so that they get stored
398 after x. */
399 for (y = x, np = 1;
400 single_pred_p (y) && !VISITED_P (single_pred (y));
401 y = single_pred (y))
402 np++;
403 for (y = x, i = n - np;
404 single_pred_p (y) && !VISITED_P (single_pred (y));
405 y = single_pred (y), i++)
407 order[i] = y;
408 MARK_VISITED (y);
410 order[i] = y;
411 MARK_VISITED (y);
413 gcc_assert (i == n - 1);
414 n -= np;
417 sbitmap_free (visited);
418 gcc_assert (n == 0);
419 return order;
421 #undef MARK_VISITED
422 #undef VISITED_P
426 /* Return TRUE if block BB has no executable statements, otherwise return
427 FALSE. */
429 bool
430 empty_block_p (basic_block bb)
432 /* BB must have no executable statements. */
433 gimple_stmt_iterator gsi = gsi_after_labels (bb);
434 if (gsi_end_p (gsi))
435 return true;
436 if (is_gimple_debug (gsi_stmt (gsi)))
437 gsi_next_nondebug (&gsi);
438 return gsi_end_p (gsi);
441 /* Replace PHI node element whose edge is E in block BB with variable NEW.
442 Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK
443 is known to have two edges, one of which must reach BB). */
445 static void
446 replace_phi_edge_with_variable (basic_block cond_block,
447 edge e, gimple phi, tree new_tree)
449 basic_block bb = gimple_bb (phi);
450 basic_block block_to_remove;
451 gimple_stmt_iterator gsi;
453 /* Change the PHI argument to new. */
454 SET_USE (PHI_ARG_DEF_PTR (phi, e->dest_idx), new_tree);
456 /* Remove the empty basic block. */
457 if (EDGE_SUCC (cond_block, 0)->dest == bb)
459 EDGE_SUCC (cond_block, 0)->flags |= EDGE_FALLTHRU;
460 EDGE_SUCC (cond_block, 0)->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
461 EDGE_SUCC (cond_block, 0)->probability = REG_BR_PROB_BASE;
462 EDGE_SUCC (cond_block, 0)->count += EDGE_SUCC (cond_block, 1)->count;
464 block_to_remove = EDGE_SUCC (cond_block, 1)->dest;
466 else
468 EDGE_SUCC (cond_block, 1)->flags |= EDGE_FALLTHRU;
469 EDGE_SUCC (cond_block, 1)->flags
470 &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
471 EDGE_SUCC (cond_block, 1)->probability = REG_BR_PROB_BASE;
472 EDGE_SUCC (cond_block, 1)->count += EDGE_SUCC (cond_block, 0)->count;
474 block_to_remove = EDGE_SUCC (cond_block, 0)->dest;
476 delete_basic_block (block_to_remove);
478 /* Eliminate the COND_EXPR at the end of COND_BLOCK. */
479 gsi = gsi_last_bb (cond_block);
480 gsi_remove (&gsi, true);
482 if (dump_file && (dump_flags & TDF_DETAILS))
483 fprintf (dump_file,
484 "COND_EXPR in block %d and PHI in block %d converted to straightline code.\n",
485 cond_block->index,
486 bb->index);
489 /* The function conditional_replacement does the main work of doing the
490 conditional replacement. Return true if the replacement is done.
491 Otherwise return false.
492 BB is the basic block where the replacement is going to be done on. ARG0
493 is argument 0 from PHI. Likewise for ARG1. */
495 static bool
496 conditional_replacement (basic_block cond_bb, basic_block middle_bb,
497 edge e0, edge e1, gimple phi,
498 tree arg0, tree arg1)
500 tree result;
501 gimple stmt, new_stmt;
502 tree cond;
503 gimple_stmt_iterator gsi;
504 edge true_edge, false_edge;
505 tree new_var, new_var2;
507 /* FIXME: Gimplification of complex type is too hard for now. */
508 if (TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
509 || TREE_CODE (TREE_TYPE (arg1)) == COMPLEX_TYPE)
510 return false;
512 /* The PHI arguments have the constants 0 and 1, then convert
513 it to the conditional. */
514 if ((integer_zerop (arg0) && integer_onep (arg1))
515 || (integer_zerop (arg1) && integer_onep (arg0)))
517 else
518 return false;
520 if (!empty_block_p (middle_bb))
521 return false;
523 /* At this point we know we have a GIMPLE_COND with two successors.
524 One successor is BB, the other successor is an empty block which
525 falls through into BB.
527 There is a single PHI node at the join point (BB) and its arguments
528 are constants (0, 1).
530 So, given the condition COND, and the two PHI arguments, we can
531 rewrite this PHI into non-branching code:
533 dest = (COND) or dest = COND'
535 We use the condition as-is if the argument associated with the
536 true edge has the value one or the argument associated with the
537 false edge as the value zero. Note that those conditions are not
538 the same since only one of the outgoing edges from the GIMPLE_COND
539 will directly reach BB and thus be associated with an argument. */
541 stmt = last_stmt (cond_bb);
542 result = PHI_RESULT (phi);
544 /* To handle special cases like floating point comparison, it is easier and
545 less error-prone to build a tree and gimplify it on the fly though it is
546 less efficient. */
547 cond = fold_build2 (gimple_cond_code (stmt), boolean_type_node,
548 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
550 /* We need to know which is the true edge and which is the false
551 edge so that we know when to invert the condition below. */
552 extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
553 if ((e0 == true_edge && integer_zerop (arg0))
554 || (e0 == false_edge && integer_onep (arg0))
555 || (e1 == true_edge && integer_zerop (arg1))
556 || (e1 == false_edge && integer_onep (arg1)))
557 cond = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (cond), cond);
559 /* Insert our new statements at the end of conditional block before the
560 COND_STMT. */
561 gsi = gsi_for_stmt (stmt);
562 new_var = force_gimple_operand_gsi (&gsi, cond, true, NULL, true,
563 GSI_SAME_STMT);
565 if (!useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (new_var)))
567 source_location locus_0, locus_1;
569 new_var2 = create_tmp_var (TREE_TYPE (result), NULL);
570 add_referenced_var (new_var2);
571 new_stmt = gimple_build_assign_with_ops (CONVERT_EXPR, new_var2,
572 new_var, NULL);
573 new_var2 = make_ssa_name (new_var2, new_stmt);
574 gimple_assign_set_lhs (new_stmt, new_var2);
575 gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
576 new_var = new_var2;
578 /* Set the locus to the first argument, unless is doesn't have one. */
579 locus_0 = gimple_phi_arg_location (phi, 0);
580 locus_1 = gimple_phi_arg_location (phi, 1);
581 if (locus_0 == UNKNOWN_LOCATION)
582 locus_0 = locus_1;
583 gimple_set_location (new_stmt, locus_0);
586 replace_phi_edge_with_variable (cond_bb, e1, phi, new_var);
588 /* Note that we optimized this PHI. */
589 return true;
592 /* The function value_replacement does the main work of doing the value
593 replacement. Return true if the replacement is done. Otherwise return
594 false.
595 BB is the basic block where the replacement is going to be done on. ARG0
596 is argument 0 from the PHI. Likewise for ARG1. */
598 static bool
599 value_replacement (basic_block cond_bb, basic_block middle_bb,
600 edge e0, edge e1, gimple phi,
601 tree arg0, tree arg1)
603 gimple cond;
604 edge true_edge, false_edge;
605 enum tree_code code;
607 /* If the type says honor signed zeros we cannot do this
608 optimization. */
609 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
610 return false;
612 if (!empty_block_p (middle_bb))
613 return false;
615 cond = last_stmt (cond_bb);
616 code = gimple_cond_code (cond);
618 /* This transformation is only valid for equality comparisons. */
619 if (code != NE_EXPR && code != EQ_EXPR)
620 return false;
622 /* We need to know which is the true edge and which is the false
623 edge so that we know if have abs or negative abs. */
624 extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
626 /* At this point we know we have a COND_EXPR with two successors.
627 One successor is BB, the other successor is an empty block which
628 falls through into BB.
630 The condition for the COND_EXPR is known to be NE_EXPR or EQ_EXPR.
632 There is a single PHI node at the join point (BB) with two arguments.
634 We now need to verify that the two arguments in the PHI node match
635 the two arguments to the equality comparison. */
637 if ((operand_equal_for_phi_arg_p (arg0, gimple_cond_lhs (cond))
638 && operand_equal_for_phi_arg_p (arg1, gimple_cond_rhs (cond)))
639 || (operand_equal_for_phi_arg_p (arg1, gimple_cond_lhs (cond))
640 && operand_equal_for_phi_arg_p (arg0, gimple_cond_rhs (cond))))
642 edge e;
643 tree arg;
645 /* For NE_EXPR, we want to build an assignment result = arg where
646 arg is the PHI argument associated with the true edge. For
647 EQ_EXPR we want the PHI argument associated with the false edge. */
648 e = (code == NE_EXPR ? true_edge : false_edge);
650 /* Unfortunately, E may not reach BB (it may instead have gone to
651 OTHER_BLOCK). If that is the case, then we want the single outgoing
652 edge from OTHER_BLOCK which reaches BB and represents the desired
653 path from COND_BLOCK. */
654 if (e->dest == middle_bb)
655 e = single_succ_edge (e->dest);
657 /* Now we know the incoming edge to BB that has the argument for the
658 RHS of our new assignment statement. */
659 if (e0 == e)
660 arg = arg0;
661 else
662 arg = arg1;
664 replace_phi_edge_with_variable (cond_bb, e1, phi, arg);
666 /* Note that we optimized this PHI. */
667 return true;
669 return false;
672 /* The function minmax_replacement does the main work of doing the minmax
673 replacement. Return true if the replacement is done. Otherwise return
674 false.
675 BB is the basic block where the replacement is going to be done on. ARG0
676 is argument 0 from the PHI. Likewise for ARG1. */
678 static bool
679 minmax_replacement (basic_block cond_bb, basic_block middle_bb,
680 edge e0, edge e1, gimple phi,
681 tree arg0, tree arg1)
683 tree result, type;
684 gimple cond, new_stmt;
685 edge true_edge, false_edge;
686 enum tree_code cmp, minmax, ass_code;
687 tree smaller, larger, arg_true, arg_false;
688 gimple_stmt_iterator gsi, gsi_from;
690 type = TREE_TYPE (PHI_RESULT (phi));
692 /* The optimization may be unsafe due to NaNs. */
693 if (HONOR_NANS (TYPE_MODE (type)))
694 return false;
696 cond = last_stmt (cond_bb);
697 cmp = gimple_cond_code (cond);
698 result = PHI_RESULT (phi);
700 /* This transformation is only valid for order comparisons. Record which
701 operand is smaller/larger if the result of the comparison is true. */
702 if (cmp == LT_EXPR || cmp == LE_EXPR)
704 smaller = gimple_cond_lhs (cond);
705 larger = gimple_cond_rhs (cond);
707 else if (cmp == GT_EXPR || cmp == GE_EXPR)
709 smaller = gimple_cond_rhs (cond);
710 larger = gimple_cond_lhs (cond);
712 else
713 return false;
715 /* We need to know which is the true edge and which is the false
716 edge so that we know if have abs or negative abs. */
717 extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
719 /* Forward the edges over the middle basic block. */
720 if (true_edge->dest == middle_bb)
721 true_edge = EDGE_SUCC (true_edge->dest, 0);
722 if (false_edge->dest == middle_bb)
723 false_edge = EDGE_SUCC (false_edge->dest, 0);
725 if (true_edge == e0)
727 gcc_assert (false_edge == e1);
728 arg_true = arg0;
729 arg_false = arg1;
731 else
733 gcc_assert (false_edge == e0);
734 gcc_assert (true_edge == e1);
735 arg_true = arg1;
736 arg_false = arg0;
739 if (empty_block_p (middle_bb))
741 if (operand_equal_for_phi_arg_p (arg_true, smaller)
742 && operand_equal_for_phi_arg_p (arg_false, larger))
744 /* Case
746 if (smaller < larger)
747 rslt = smaller;
748 else
749 rslt = larger; */
750 minmax = MIN_EXPR;
752 else if (operand_equal_for_phi_arg_p (arg_false, smaller)
753 && operand_equal_for_phi_arg_p (arg_true, larger))
754 minmax = MAX_EXPR;
755 else
756 return false;
758 else
760 /* Recognize the following case, assuming d <= u:
762 if (a <= u)
763 b = MAX (a, d);
764 x = PHI <b, u>
766 This is equivalent to
768 b = MAX (a, d);
769 x = MIN (b, u); */
771 gimple assign = last_and_only_stmt (middle_bb);
772 tree lhs, op0, op1, bound;
774 if (!assign
775 || gimple_code (assign) != GIMPLE_ASSIGN)
776 return false;
778 lhs = gimple_assign_lhs (assign);
779 ass_code = gimple_assign_rhs_code (assign);
780 if (ass_code != MAX_EXPR && ass_code != MIN_EXPR)
781 return false;
782 op0 = gimple_assign_rhs1 (assign);
783 op1 = gimple_assign_rhs2 (assign);
785 if (true_edge->src == middle_bb)
787 /* We got here if the condition is true, i.e., SMALLER < LARGER. */
788 if (!operand_equal_for_phi_arg_p (lhs, arg_true))
789 return false;
791 if (operand_equal_for_phi_arg_p (arg_false, larger))
793 /* Case
795 if (smaller < larger)
797 r' = MAX_EXPR (smaller, bound)
799 r = PHI <r', larger> --> to be turned to MIN_EXPR. */
800 if (ass_code != MAX_EXPR)
801 return false;
803 minmax = MIN_EXPR;
804 if (operand_equal_for_phi_arg_p (op0, smaller))
805 bound = op1;
806 else if (operand_equal_for_phi_arg_p (op1, smaller))
807 bound = op0;
808 else
809 return false;
811 /* We need BOUND <= LARGER. */
812 if (!integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node,
813 bound, larger)))
814 return false;
816 else if (operand_equal_for_phi_arg_p (arg_false, smaller))
818 /* Case
820 if (smaller < larger)
822 r' = MIN_EXPR (larger, bound)
824 r = PHI <r', smaller> --> to be turned to MAX_EXPR. */
825 if (ass_code != MIN_EXPR)
826 return false;
828 minmax = MAX_EXPR;
829 if (operand_equal_for_phi_arg_p (op0, larger))
830 bound = op1;
831 else if (operand_equal_for_phi_arg_p (op1, larger))
832 bound = op0;
833 else
834 return false;
836 /* We need BOUND >= SMALLER. */
837 if (!integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node,
838 bound, smaller)))
839 return false;
841 else
842 return false;
844 else
846 /* We got here if the condition is false, i.e., SMALLER > LARGER. */
847 if (!operand_equal_for_phi_arg_p (lhs, arg_false))
848 return false;
850 if (operand_equal_for_phi_arg_p (arg_true, larger))
852 /* Case
854 if (smaller > larger)
856 r' = MIN_EXPR (smaller, bound)
858 r = PHI <r', larger> --> to be turned to MAX_EXPR. */
859 if (ass_code != MIN_EXPR)
860 return false;
862 minmax = MAX_EXPR;
863 if (operand_equal_for_phi_arg_p (op0, smaller))
864 bound = op1;
865 else if (operand_equal_for_phi_arg_p (op1, smaller))
866 bound = op0;
867 else
868 return false;
870 /* We need BOUND >= LARGER. */
871 if (!integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node,
872 bound, larger)))
873 return false;
875 else if (operand_equal_for_phi_arg_p (arg_true, smaller))
877 /* Case
879 if (smaller > larger)
881 r' = MAX_EXPR (larger, bound)
883 r = PHI <r', smaller> --> to be turned to MIN_EXPR. */
884 if (ass_code != MAX_EXPR)
885 return false;
887 minmax = MIN_EXPR;
888 if (operand_equal_for_phi_arg_p (op0, larger))
889 bound = op1;
890 else if (operand_equal_for_phi_arg_p (op1, larger))
891 bound = op0;
892 else
893 return false;
895 /* We need BOUND <= SMALLER. */
896 if (!integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node,
897 bound, smaller)))
898 return false;
900 else
901 return false;
904 /* Move the statement from the middle block. */
905 gsi = gsi_last_bb (cond_bb);
906 gsi_from = gsi_last_nondebug_bb (middle_bb);
907 gsi_move_before (&gsi_from, &gsi);
910 /* Emit the statement to compute min/max. */
911 result = duplicate_ssa_name (PHI_RESULT (phi), NULL);
912 new_stmt = gimple_build_assign_with_ops (minmax, result, arg0, arg1);
913 gsi = gsi_last_bb (cond_bb);
914 gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
916 replace_phi_edge_with_variable (cond_bb, e1, phi, result);
917 return true;
920 /* The function absolute_replacement does the main work of doing the absolute
921 replacement. Return true if the replacement is done. Otherwise return
922 false.
923 bb is the basic block where the replacement is going to be done on. arg0
924 is argument 0 from the phi. Likewise for arg1. */
926 static bool
927 abs_replacement (basic_block cond_bb, basic_block middle_bb,
928 edge e0 ATTRIBUTE_UNUSED, edge e1,
929 gimple phi, tree arg0, tree arg1)
931 tree result;
932 gimple new_stmt, cond;
933 gimple_stmt_iterator gsi;
934 edge true_edge, false_edge;
935 gimple assign;
936 edge e;
937 tree rhs, lhs;
938 bool negate;
939 enum tree_code cond_code;
941 /* If the type says honor signed zeros we cannot do this
942 optimization. */
943 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
944 return false;
946 /* OTHER_BLOCK must have only one executable statement which must have the
947 form arg0 = -arg1 or arg1 = -arg0. */
949 assign = last_and_only_stmt (middle_bb);
950 /* If we did not find the proper negation assignment, then we can not
951 optimize. */
952 if (assign == NULL)
953 return false;
955 /* If we got here, then we have found the only executable statement
956 in OTHER_BLOCK. If it is anything other than arg = -arg1 or
957 arg1 = -arg0, then we can not optimize. */
958 if (gimple_code (assign) != GIMPLE_ASSIGN)
959 return false;
961 lhs = gimple_assign_lhs (assign);
963 if (gimple_assign_rhs_code (assign) != NEGATE_EXPR)
964 return false;
966 rhs = gimple_assign_rhs1 (assign);
968 /* The assignment has to be arg0 = -arg1 or arg1 = -arg0. */
969 if (!(lhs == arg0 && rhs == arg1)
970 && !(lhs == arg1 && rhs == arg0))
971 return false;
973 cond = last_stmt (cond_bb);
974 result = PHI_RESULT (phi);
976 /* Only relationals comparing arg[01] against zero are interesting. */
977 cond_code = gimple_cond_code (cond);
978 if (cond_code != GT_EXPR && cond_code != GE_EXPR
979 && cond_code != LT_EXPR && cond_code != LE_EXPR)
980 return false;
982 /* Make sure the conditional is arg[01] OP y. */
983 if (gimple_cond_lhs (cond) != rhs)
984 return false;
986 if (FLOAT_TYPE_P (TREE_TYPE (gimple_cond_rhs (cond)))
987 ? real_zerop (gimple_cond_rhs (cond))
988 : integer_zerop (gimple_cond_rhs (cond)))
990 else
991 return false;
993 /* We need to know which is the true edge and which is the false
994 edge so that we know if have abs or negative abs. */
995 extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
997 /* For GT_EXPR/GE_EXPR, if the true edge goes to OTHER_BLOCK, then we
998 will need to negate the result. Similarly for LT_EXPR/LE_EXPR if
999 the false edge goes to OTHER_BLOCK. */
1000 if (cond_code == GT_EXPR || cond_code == GE_EXPR)
1001 e = true_edge;
1002 else
1003 e = false_edge;
1005 if (e->dest == middle_bb)
1006 negate = true;
1007 else
1008 negate = false;
1010 result = duplicate_ssa_name (result, NULL);
1012 if (negate)
1014 tree tmp = create_tmp_var (TREE_TYPE (result), NULL);
1015 add_referenced_var (tmp);
1016 lhs = make_ssa_name (tmp, NULL);
1018 else
1019 lhs = result;
1021 /* Build the modify expression with abs expression. */
1022 new_stmt = gimple_build_assign_with_ops (ABS_EXPR, lhs, rhs, NULL);
1024 gsi = gsi_last_bb (cond_bb);
1025 gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
1027 if (negate)
1029 /* Get the right GSI. We want to insert after the recently
1030 added ABS_EXPR statement (which we know is the first statement
1031 in the block. */
1032 new_stmt = gimple_build_assign_with_ops (NEGATE_EXPR, result, lhs, NULL);
1034 gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
1037 replace_phi_edge_with_variable (cond_bb, e1, phi, result);
1039 /* Note that we optimized this PHI. */
1040 return true;
1043 /* Auxiliary functions to determine the set of memory accesses which
1044 can't trap because they are preceded by accesses to the same memory
1045 portion. We do that for MEM_REFs, so we only need to track
1046 the SSA_NAME of the pointer indirectly referenced. The algorithm
1047 simply is a walk over all instructions in dominator order. When
1048 we see an MEM_REF we determine if we've already seen a same
1049 ref anywhere up to the root of the dominator tree. If we do the
1050 current access can't trap. If we don't see any dominating access
1051 the current access might trap, but might also make later accesses
1052 non-trapping, so we remember it. We need to be careful with loads
1053 or stores, for instance a load might not trap, while a store would,
1054 so if we see a dominating read access this doesn't mean that a later
1055 write access would not trap. Hence we also need to differentiate the
1056 type of access(es) seen.
1058 ??? We currently are very conservative and assume that a load might
1059 trap even if a store doesn't (write-only memory). This probably is
1060 overly conservative. */
1062 /* A hash-table of SSA_NAMEs, and in which basic block an MEM_REF
1063 through it was seen, which would constitute a no-trap region for
1064 same accesses. */
1065 struct name_to_bb
1067 tree ssa_name;
1068 basic_block bb;
1069 unsigned store : 1;
1072 /* The hash table for remembering what we've seen. */
1073 static htab_t seen_ssa_names;
1075 /* The set of MEM_REFs which can't trap. */
1076 static struct pointer_set_t *nontrap_set;
1078 /* The hash function, based on the pointer to the pointer SSA_NAME. */
1079 static hashval_t
1080 name_to_bb_hash (const void *p)
1082 const_tree n = ((const struct name_to_bb *)p)->ssa_name;
1083 return htab_hash_pointer (n) ^ ((const struct name_to_bb *)p)->store;
1086 /* The equality function of *P1 and *P2. SSA_NAMEs are shared, so
1087 it's enough to simply compare them for equality. */
1088 static int
1089 name_to_bb_eq (const void *p1, const void *p2)
1091 const struct name_to_bb *n1 = (const struct name_to_bb *)p1;
1092 const struct name_to_bb *n2 = (const struct name_to_bb *)p2;
1094 return n1->ssa_name == n2->ssa_name && n1->store == n2->store;
1097 /* We see the expression EXP in basic block BB. If it's an interesting
1098 expression (an MEM_REF through an SSA_NAME) possibly insert the
1099 expression into the set NONTRAP or the hash table of seen expressions.
1100 STORE is true if this expression is on the LHS, otherwise it's on
1101 the RHS. */
1102 static void
1103 add_or_mark_expr (basic_block bb, tree exp,
1104 struct pointer_set_t *nontrap, bool store)
1106 if (TREE_CODE (exp) == MEM_REF
1107 && TREE_CODE (TREE_OPERAND (exp, 0)) == SSA_NAME)
1109 tree name = TREE_OPERAND (exp, 0);
1110 struct name_to_bb map;
1111 void **slot;
1112 struct name_to_bb *n2bb;
1113 basic_block found_bb = 0;
1115 /* Try to find the last seen MEM_REF through the same
1116 SSA_NAME, which can trap. */
1117 map.ssa_name = name;
1118 map.bb = 0;
1119 map.store = store;
1120 slot = htab_find_slot (seen_ssa_names, &map, INSERT);
1121 n2bb = (struct name_to_bb *) *slot;
1122 if (n2bb)
1123 found_bb = n2bb->bb;
1125 /* If we've found a trapping MEM_REF, _and_ it dominates EXP
1126 (it's in a basic block on the path from us to the dominator root)
1127 then we can't trap. */
1128 if (found_bb && found_bb->aux == (void *)1)
1130 pointer_set_insert (nontrap, exp);
1132 else
1134 /* EXP might trap, so insert it into the hash table. */
1135 if (n2bb)
1137 n2bb->bb = bb;
1139 else
1141 n2bb = XNEW (struct name_to_bb);
1142 n2bb->ssa_name = name;
1143 n2bb->bb = bb;
1144 n2bb->store = store;
1145 *slot = n2bb;
1151 /* Called by walk_dominator_tree, when entering the block BB. */
1152 static void
1153 nt_init_block (struct dom_walk_data *data ATTRIBUTE_UNUSED, basic_block bb)
1155 gimple_stmt_iterator gsi;
1156 /* Mark this BB as being on the path to dominator root. */
1157 bb->aux = (void*)1;
1159 /* And walk the statements in order. */
1160 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1162 gimple stmt = gsi_stmt (gsi);
1164 if (is_gimple_assign (stmt))
1166 add_or_mark_expr (bb, gimple_assign_lhs (stmt), nontrap_set, true);
1167 add_or_mark_expr (bb, gimple_assign_rhs1 (stmt), nontrap_set, false);
1168 if (get_gimple_rhs_num_ops (gimple_assign_rhs_code (stmt)) > 1)
1169 add_or_mark_expr (bb, gimple_assign_rhs2 (stmt), nontrap_set,
1170 false);
1175 /* Called by walk_dominator_tree, when basic block BB is exited. */
1176 static void
1177 nt_fini_block (struct dom_walk_data *data ATTRIBUTE_UNUSED, basic_block bb)
1179 /* This BB isn't on the path to dominator root anymore. */
1180 bb->aux = NULL;
1183 /* This is the entry point of gathering non trapping memory accesses.
1184 It will do a dominator walk over the whole function, and it will
1185 make use of the bb->aux pointers. It returns a set of trees
1186 (the MEM_REFs itself) which can't trap. */
1187 static struct pointer_set_t *
1188 get_non_trapping (void)
1190 struct pointer_set_t *nontrap;
1191 struct dom_walk_data walk_data;
1193 nontrap = pointer_set_create ();
1194 seen_ssa_names = htab_create (128, name_to_bb_hash, name_to_bb_eq,
1195 free);
1196 /* We're going to do a dominator walk, so ensure that we have
1197 dominance information. */
1198 calculate_dominance_info (CDI_DOMINATORS);
1200 /* Setup callbacks for the generic dominator tree walker. */
1201 nontrap_set = nontrap;
1202 walk_data.dom_direction = CDI_DOMINATORS;
1203 walk_data.initialize_block_local_data = NULL;
1204 walk_data.before_dom_children = nt_init_block;
1205 walk_data.after_dom_children = nt_fini_block;
1206 walk_data.global_data = NULL;
1207 walk_data.block_local_data_size = 0;
1209 init_walk_dominator_tree (&walk_data);
1210 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1211 fini_walk_dominator_tree (&walk_data);
1212 htab_delete (seen_ssa_names);
1214 return nontrap;
1217 /* Do the main work of conditional store replacement. We already know
1218 that the recognized pattern looks like so:
1220 split:
1221 if (cond) goto MIDDLE_BB; else goto JOIN_BB (edge E1)
1222 MIDDLE_BB:
1223 something
1224 fallthrough (edge E0)
1225 JOIN_BB:
1226 some more
1228 We check that MIDDLE_BB contains only one store, that that store
1229 doesn't trap (not via NOTRAP, but via checking if an access to the same
1230 memory location dominates us) and that the store has a "simple" RHS. */
1232 static bool
1233 cond_store_replacement (basic_block middle_bb, basic_block join_bb,
1234 edge e0, edge e1, struct pointer_set_t *nontrap)
1236 gimple assign = last_and_only_stmt (middle_bb);
1237 tree lhs, rhs, name;
1238 gimple newphi, new_stmt;
1239 gimple_stmt_iterator gsi;
1240 source_location locus;
1242 /* Check if middle_bb contains of only one store. */
1243 if (!assign
1244 || !gimple_assign_single_p (assign))
1245 return false;
1247 locus = gimple_location (assign);
1248 lhs = gimple_assign_lhs (assign);
1249 rhs = gimple_assign_rhs1 (assign);
1250 if (TREE_CODE (lhs) != MEM_REF
1251 || TREE_CODE (TREE_OPERAND (lhs, 0)) != SSA_NAME
1252 || !is_gimple_reg_type (TREE_TYPE (lhs)))
1253 return false;
1255 /* Prove that we can move the store down. We could also check
1256 TREE_THIS_NOTRAP here, but in that case we also could move stores,
1257 whose value is not available readily, which we want to avoid. */
1258 if (!pointer_set_contains (nontrap, lhs))
1259 return false;
1261 /* Now we've checked the constraints, so do the transformation:
1262 1) Remove the single store. */
1263 gsi = gsi_for_stmt (assign);
1264 unlink_stmt_vdef (assign);
1265 gsi_remove (&gsi, true);
1266 release_defs (assign);
1268 /* 2) Create a temporary where we can store the old content
1269 of the memory touched by the store, if we need to. */
1270 if (!condstoretemp || TREE_TYPE (lhs) != TREE_TYPE (condstoretemp))
1272 condstoretemp = create_tmp_reg (TREE_TYPE (lhs), "cstore");
1273 get_var_ann (condstoretemp);
1275 add_referenced_var (condstoretemp);
1277 /* 3) Insert a load from the memory of the store to the temporary
1278 on the edge which did not contain the store. */
1279 lhs = unshare_expr (lhs);
1280 new_stmt = gimple_build_assign (condstoretemp, lhs);
1281 name = make_ssa_name (condstoretemp, new_stmt);
1282 gimple_assign_set_lhs (new_stmt, name);
1283 gimple_set_location (new_stmt, locus);
1284 gsi_insert_on_edge (e1, new_stmt);
1286 /* 4) Create a PHI node at the join block, with one argument
1287 holding the old RHS, and the other holding the temporary
1288 where we stored the old memory contents. */
1289 newphi = create_phi_node (condstoretemp, join_bb);
1290 add_phi_arg (newphi, rhs, e0, locus);
1291 add_phi_arg (newphi, name, e1, locus);
1293 lhs = unshare_expr (lhs);
1294 new_stmt = gimple_build_assign (lhs, PHI_RESULT (newphi));
1296 /* 5) Insert that PHI node. */
1297 gsi = gsi_after_labels (join_bb);
1298 if (gsi_end_p (gsi))
1300 gsi = gsi_last_bb (join_bb);
1301 gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
1303 else
1304 gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
1306 return true;
1309 /* Do the main work of conditional store replacement. */
1311 static bool
1312 cond_if_else_store_replacement_1 (basic_block then_bb, basic_block else_bb,
1313 basic_block join_bb, gimple then_assign,
1314 gimple else_assign)
1316 tree lhs_base, lhs, then_rhs, else_rhs;
1317 source_location then_locus, else_locus;
1318 gimple_stmt_iterator gsi;
1319 gimple newphi, new_stmt;
1321 if (then_assign == NULL
1322 || !gimple_assign_single_p (then_assign)
1323 || else_assign == NULL
1324 || !gimple_assign_single_p (else_assign))
1325 return false;
1327 lhs = gimple_assign_lhs (then_assign);
1328 if (!is_gimple_reg_type (TREE_TYPE (lhs))
1329 || !operand_equal_p (lhs, gimple_assign_lhs (else_assign), 0))
1330 return false;
1332 lhs_base = get_base_address (lhs);
1333 if (lhs_base == NULL_TREE
1334 || (!DECL_P (lhs_base) && TREE_CODE (lhs_base) != MEM_REF))
1335 return false;
1337 then_rhs = gimple_assign_rhs1 (then_assign);
1338 else_rhs = gimple_assign_rhs1 (else_assign);
1339 then_locus = gimple_location (then_assign);
1340 else_locus = gimple_location (else_assign);
1342 /* Now we've checked the constraints, so do the transformation:
1343 1) Remove the stores. */
1344 gsi = gsi_for_stmt (then_assign);
1345 unlink_stmt_vdef (then_assign);
1346 gsi_remove (&gsi, true);
1347 release_defs (then_assign);
1349 gsi = gsi_for_stmt (else_assign);
1350 unlink_stmt_vdef (else_assign);
1351 gsi_remove (&gsi, true);
1352 release_defs (else_assign);
1354 /* 2) Create a temporary where we can store the old content
1355 of the memory touched by the store, if we need to. */
1356 if (!condstoretemp || TREE_TYPE (lhs) != TREE_TYPE (condstoretemp))
1358 condstoretemp = create_tmp_reg (TREE_TYPE (lhs), "cstore");
1359 get_var_ann (condstoretemp);
1361 add_referenced_var (condstoretemp);
1363 /* 3) Create a PHI node at the join block, with one argument
1364 holding the old RHS, and the other holding the temporary
1365 where we stored the old memory contents. */
1366 newphi = create_phi_node (condstoretemp, join_bb);
1367 add_phi_arg (newphi, then_rhs, EDGE_SUCC (then_bb, 0), then_locus);
1368 add_phi_arg (newphi, else_rhs, EDGE_SUCC (else_bb, 0), else_locus);
1370 new_stmt = gimple_build_assign (lhs, PHI_RESULT (newphi));
1372 /* 4) Insert that PHI node. */
1373 gsi = gsi_after_labels (join_bb);
1374 if (gsi_end_p (gsi))
1376 gsi = gsi_last_bb (join_bb);
1377 gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
1379 else
1380 gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
1382 return true;
1385 /* Conditional store replacement. We already know
1386 that the recognized pattern looks like so:
1388 split:
1389 if (cond) goto THEN_BB; else goto ELSE_BB (edge E1)
1390 THEN_BB:
1392 X = Y;
1394 goto JOIN_BB;
1395 ELSE_BB:
1397 X = Z;
1399 fallthrough (edge E0)
1400 JOIN_BB:
1401 some more
1403 We check that it is safe to sink the store to JOIN_BB by verifying that
1404 there are no read-after-write or write-after-write dependencies in
1405 THEN_BB and ELSE_BB. */
1407 static bool
1408 cond_if_else_store_replacement (basic_block then_bb, basic_block else_bb,
1409 basic_block join_bb)
1411 gimple then_assign = last_and_only_stmt (then_bb);
1412 gimple else_assign = last_and_only_stmt (else_bb);
1413 VEC (data_reference_p, heap) *then_datarefs, *else_datarefs;
1414 VEC (ddr_p, heap) *then_ddrs, *else_ddrs;
1415 gimple then_store, else_store;
1416 bool found, ok = false, res;
1417 struct data_dependence_relation *ddr;
1418 data_reference_p then_dr, else_dr;
1419 int i, j;
1420 tree then_lhs, else_lhs;
1421 VEC (gimple, heap) *then_stores, *else_stores;
1422 basic_block blocks[3];
1424 if (MAX_STORES_TO_SINK == 0)
1425 return false;
1427 /* Handle the case with single statement in THEN_BB and ELSE_BB. */
1428 if (then_assign && else_assign)
1429 return cond_if_else_store_replacement_1 (then_bb, else_bb, join_bb,
1430 then_assign, else_assign);
1432 /* Find data references. */
1433 then_datarefs = VEC_alloc (data_reference_p, heap, 1);
1434 else_datarefs = VEC_alloc (data_reference_p, heap, 1);
1435 if ((find_data_references_in_bb (NULL, then_bb, &then_datarefs)
1436 == chrec_dont_know)
1437 || !VEC_length (data_reference_p, then_datarefs)
1438 || (find_data_references_in_bb (NULL, else_bb, &else_datarefs)
1439 == chrec_dont_know)
1440 || !VEC_length (data_reference_p, else_datarefs))
1442 free_data_refs (then_datarefs);
1443 free_data_refs (else_datarefs);
1444 return false;
1447 /* Find pairs of stores with equal LHS. */
1448 then_stores = VEC_alloc (gimple, heap, 1);
1449 else_stores = VEC_alloc (gimple, heap, 1);
1450 FOR_EACH_VEC_ELT (data_reference_p, then_datarefs, i, then_dr)
1452 if (DR_IS_READ (then_dr))
1453 continue;
1455 then_store = DR_STMT (then_dr);
1456 then_lhs = gimple_assign_lhs (then_store);
1457 found = false;
1459 FOR_EACH_VEC_ELT (data_reference_p, else_datarefs, j, else_dr)
1461 if (DR_IS_READ (else_dr))
1462 continue;
1464 else_store = DR_STMT (else_dr);
1465 else_lhs = gimple_assign_lhs (else_store);
1467 if (operand_equal_p (then_lhs, else_lhs, 0))
1469 found = true;
1470 break;
1474 if (!found)
1475 continue;
1477 VEC_safe_push (gimple, heap, then_stores, then_store);
1478 VEC_safe_push (gimple, heap, else_stores, else_store);
1481 /* No pairs of stores found. */
1482 if (!VEC_length (gimple, then_stores)
1483 || VEC_length (gimple, then_stores) > (unsigned) MAX_STORES_TO_SINK)
1485 free_data_refs (then_datarefs);
1486 free_data_refs (else_datarefs);
1487 VEC_free (gimple, heap, then_stores);
1488 VEC_free (gimple, heap, else_stores);
1489 return false;
1492 /* Compute and check data dependencies in both basic blocks. */
1493 then_ddrs = VEC_alloc (ddr_p, heap, 1);
1494 else_ddrs = VEC_alloc (ddr_p, heap, 1);
1495 compute_all_dependences (then_datarefs, &then_ddrs, NULL, false);
1496 compute_all_dependences (else_datarefs, &else_ddrs, NULL, false);
1497 blocks[0] = then_bb;
1498 blocks[1] = else_bb;
1499 blocks[2] = join_bb;
1500 renumber_gimple_stmt_uids_in_blocks (blocks, 3);
1502 /* Check that there are no read-after-write or write-after-write dependencies
1503 in THEN_BB. */
1504 FOR_EACH_VEC_ELT (ddr_p, then_ddrs, i, ddr)
1506 struct data_reference *dra = DDR_A (ddr);
1507 struct data_reference *drb = DDR_B (ddr);
1509 if (DDR_ARE_DEPENDENT (ddr) != chrec_known
1510 && ((DR_IS_READ (dra) && DR_IS_WRITE (drb)
1511 && gimple_uid (DR_STMT (dra)) > gimple_uid (DR_STMT (drb)))
1512 || (DR_IS_READ (drb) && DR_IS_WRITE (dra)
1513 && gimple_uid (DR_STMT (drb)) > gimple_uid (DR_STMT (dra)))
1514 || (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))))
1516 free_dependence_relations (then_ddrs);
1517 free_dependence_relations (else_ddrs);
1518 free_data_refs (then_datarefs);
1519 free_data_refs (else_datarefs);
1520 VEC_free (gimple, heap, then_stores);
1521 VEC_free (gimple, heap, else_stores);
1522 return false;
1526 /* Check that there are no read-after-write or write-after-write dependencies
1527 in ELSE_BB. */
1528 FOR_EACH_VEC_ELT (ddr_p, else_ddrs, i, ddr)
1530 struct data_reference *dra = DDR_A (ddr);
1531 struct data_reference *drb = DDR_B (ddr);
1533 if (DDR_ARE_DEPENDENT (ddr) != chrec_known
1534 && ((DR_IS_READ (dra) && DR_IS_WRITE (drb)
1535 && gimple_uid (DR_STMT (dra)) > gimple_uid (DR_STMT (drb)))
1536 || (DR_IS_READ (drb) && DR_IS_WRITE (dra)
1537 && gimple_uid (DR_STMT (drb)) > gimple_uid (DR_STMT (dra)))
1538 || (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))))
1540 free_dependence_relations (then_ddrs);
1541 free_dependence_relations (else_ddrs);
1542 free_data_refs (then_datarefs);
1543 free_data_refs (else_datarefs);
1544 VEC_free (gimple, heap, then_stores);
1545 VEC_free (gimple, heap, else_stores);
1546 return false;
1550 /* Sink stores with same LHS. */
1551 FOR_EACH_VEC_ELT (gimple, then_stores, i, then_store)
1553 else_store = VEC_index (gimple, else_stores, i);
1554 res = cond_if_else_store_replacement_1 (then_bb, else_bb, join_bb,
1555 then_store, else_store);
1556 ok = ok || res;
1559 free_dependence_relations (then_ddrs);
1560 free_dependence_relations (else_ddrs);
1561 free_data_refs (then_datarefs);
1562 free_data_refs (else_datarefs);
1563 VEC_free (gimple, heap, then_stores);
1564 VEC_free (gimple, heap, else_stores);
1566 return ok;
1569 /* Always do these optimizations if we have SSA
1570 trees to work on. */
1571 static bool
1572 gate_phiopt (void)
1574 return 1;
1577 struct gimple_opt_pass pass_phiopt =
1580 GIMPLE_PASS,
1581 "phiopt", /* name */
1582 gate_phiopt, /* gate */
1583 tree_ssa_phiopt, /* execute */
1584 NULL, /* sub */
1585 NULL, /* next */
1586 0, /* static_pass_number */
1587 TV_TREE_PHIOPT, /* tv_id */
1588 PROP_cfg | PROP_ssa, /* properties_required */
1589 0, /* properties_provided */
1590 0, /* properties_destroyed */
1591 0, /* todo_flags_start */
1592 TODO_dump_func
1593 | TODO_ggc_collect
1594 | TODO_verify_ssa
1595 | TODO_verify_flow
1596 | TODO_verify_stmts /* todo_flags_finish */
1600 static bool
1601 gate_cselim (void)
1603 return flag_tree_cselim;
1606 struct gimple_opt_pass pass_cselim =
1609 GIMPLE_PASS,
1610 "cselim", /* name */
1611 gate_cselim, /* gate */
1612 tree_ssa_cs_elim, /* execute */
1613 NULL, /* sub */
1614 NULL, /* next */
1615 0, /* static_pass_number */
1616 TV_TREE_PHIOPT, /* tv_id */
1617 PROP_cfg | PROP_ssa, /* properties_required */
1618 0, /* properties_provided */
1619 0, /* properties_destroyed */
1620 0, /* todo_flags_start */
1621 TODO_dump_func
1622 | TODO_ggc_collect
1623 | TODO_verify_ssa
1624 | TODO_verify_flow
1625 | TODO_verify_stmts /* todo_flags_finish */