OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-phiopt.c
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.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>.  */
20
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
38 static unsigned int tree_ssa_phiopt (void);
39 static unsigned int tree_ssa_phiopt_worker (bool);
40 static bool conditional_replacement (basic_block, basic_block,
41                                      edge, edge, gimple, tree, tree);
42 static bool value_replacement (basic_block, basic_block,
43                                edge, edge, gimple, tree, tree);
44 static bool minmax_replacement (basic_block, basic_block,
45                                 edge, edge, gimple, tree, tree);
46 static bool abs_replacement (basic_block, basic_block,
47                              edge, edge, gimple, tree, tree);
48 static bool cond_store_replacement (basic_block, basic_block, edge, edge,
49                                     struct pointer_set_t *);
50 static bool cond_if_else_store_replacement (basic_block, basic_block, basic_block);
51 static struct pointer_set_t * get_non_trapping (void);
52 static void replace_phi_edge_with_variable (basic_block, edge, gimple, tree);
53
54 /* This pass tries to replaces an if-then-else block with an
55    assignment.  We have four kinds of transformations.  Some of these
56    transformations are also performed by the ifcvt RTL optimizer.
57
58    Conditional Replacement
59    -----------------------
60
61    This transformation, implemented in conditional_replacement,
62    replaces
63
64      bb0:
65       if (cond) goto bb2; else goto bb1;
66      bb1:
67      bb2:
68       x = PHI <0 (bb1), 1 (bb0), ...>;
69
70    with
71
72      bb0:
73       x' = cond;
74       goto bb2;
75      bb2:
76       x = PHI <x' (bb0), ...>;
77
78    We remove bb1 as it becomes unreachable.  This occurs often due to
79    gimplification of conditionals.
80
81    Value Replacement
82    -----------------
83
84    This transformation, implemented in value_replacement, replaces
85
86      bb0:
87        if (a != b) goto bb2; else goto bb1;
88      bb1:
89      bb2:
90        x = PHI <a (bb1), b (bb0), ...>;
91
92    with
93
94      bb0:
95      bb2:
96        x = PHI <b (bb0), ...>;
97
98    This opportunity can sometimes occur as a result of other
99    optimizations.
100
101    ABS Replacement
102    ---------------
103
104    This transformation, implemented in abs_replacement, replaces
105
106      bb0:
107        if (a >= 0) goto bb2; else goto bb1;
108      bb1:
109        x = -a;
110      bb2:
111        x = PHI <x (bb1), a (bb0), ...>;
112
113    with
114
115      bb0:
116        x' = ABS_EXPR< a >;
117      bb2:
118        x = PHI <x' (bb0), ...>;
119
120    MIN/MAX Replacement
121    -------------------
122
123    This transformation, minmax_replacement replaces
124
125      bb0:
126        if (a <= b) goto bb2; else goto bb1;
127      bb1:
128      bb2:
129        x = PHI <b (bb1), a (bb0), ...>;
130
131    with
132
133      bb0:
134        x' = MIN_EXPR (a, b)
135      bb2:
136        x = PHI <x' (bb0), ...>;
137
138    A similar transformation is done for MAX_EXPR.  */
139
140 static unsigned int
141 tree_ssa_phiopt (void)
142 {
143   return tree_ssa_phiopt_worker (false);
144 }
145
146 /* This pass tries to transform conditional stores into unconditional
147    ones, enabling further simplifications with the simpler then and else
148    blocks.  In particular it replaces this:
149
150      bb0:
151        if (cond) goto bb2; else goto bb1;
152      bb1:
153        *p = RHS;
154      bb2:
155
156    with
157
158      bb0:
159        if (cond) goto bb1; else goto bb2;
160      bb1:
161        condtmp' = *p;
162      bb2:
163        condtmp = PHI <RHS, condtmp'>
164        *p = condtmp;
165
166    This transformation can only be done under several constraints,
167    documented below.  It also replaces:
168
169      bb0:
170        if (cond) goto bb2; else goto bb1;
171      bb1:
172        *p = RHS1;
173        goto bb3;
174      bb2:
175        *p = RHS2;
176      bb3:
177
178    with
179
180      bb0:
181        if (cond) goto bb3; else goto bb1;
182      bb1:
183      bb3:
184        condtmp = PHI <RHS1, RHS2>
185        *p = condtmp;  */
186
187 static unsigned int
188 tree_ssa_cs_elim (void)
189 {
190   return tree_ssa_phiopt_worker (true);
191 }
192
193 /* For conditional store replacement we need a temporary to
194    put the old contents of the memory in.  */
195 static tree condstoretemp;
196
197 /* The core routine of conditional store replacement and normal
198    phi optimizations.  Both share much of the infrastructure in how
199    to match applicable basic block patterns.  DO_STORE_ELIM is true
200    when we want to do conditional store replacement, false otherwise.  */
201 static unsigned int
202 tree_ssa_phiopt_worker (bool do_store_elim)
203 {
204   basic_block bb;
205   basic_block *bb_order;
206   unsigned n, i;
207   bool cfgchanged = false;
208   struct pointer_set_t *nontrap = 0;
209
210   if (do_store_elim)
211     {
212       condstoretemp = NULL_TREE;
213       /* Calculate the set of non-trapping memory accesses.  */
214       nontrap = get_non_trapping ();
215     }
216
217   /* Search every basic block for COND_EXPR we may be able to optimize.
218
219      We walk the blocks in order that guarantees that a block with
220      a single predecessor is processed before the predecessor.
221      This ensures that we collapse inner ifs before visiting the
222      outer ones, and also that we do not try to visit a removed
223      block.  */
224   bb_order = blocks_in_phiopt_order ();
225   n = n_basic_blocks - NUM_FIXED_BLOCKS;
226
227   for (i = 0; i < n; i++)
228     {
229       gimple cond_stmt, phi;
230       basic_block bb1, bb2;
231       edge e1, e2;
232       tree arg0, arg1;
233
234       bb = bb_order[i];
235
236       cond_stmt = last_stmt (bb);
237       /* Check to see if the last statement is a GIMPLE_COND.  */
238       if (!cond_stmt
239           || gimple_code (cond_stmt) != GIMPLE_COND)
240         continue;
241
242       e1 = EDGE_SUCC (bb, 0);
243       bb1 = e1->dest;
244       e2 = EDGE_SUCC (bb, 1);
245       bb2 = e2->dest;
246
247       /* We cannot do the optimization on abnormal edges.  */
248       if ((e1->flags & EDGE_ABNORMAL) != 0
249           || (e2->flags & EDGE_ABNORMAL) != 0)
250        continue;
251
252       /* If either bb1's succ or bb2 or bb2's succ is non NULL.  */
253       if (EDGE_COUNT (bb1->succs) == 0
254           || bb2 == NULL
255           || EDGE_COUNT (bb2->succs) == 0)
256         continue;
257
258       /* Find the bb which is the fall through to the other.  */
259       if (EDGE_SUCC (bb1, 0)->dest == bb2)
260         ;
261       else if (EDGE_SUCC (bb2, 0)->dest == bb1)
262         {
263           basic_block bb_tmp = bb1;
264           edge e_tmp = e1;
265           bb1 = bb2;
266           bb2 = bb_tmp;
267           e1 = e2;
268           e2 = e_tmp;
269         }
270       else if (do_store_elim
271                && EDGE_SUCC (bb1, 0)->dest == EDGE_SUCC (bb2, 0)->dest)
272         {
273           basic_block bb3 = EDGE_SUCC (bb1, 0)->dest;
274
275           if (!single_succ_p (bb1)
276               || (EDGE_SUCC (bb1, 0)->flags & EDGE_FALLTHRU) == 0
277               || !single_succ_p (bb2)
278               || (EDGE_SUCC (bb2, 0)->flags & EDGE_FALLTHRU) == 0
279               || EDGE_COUNT (bb3->preds) != 2)
280             continue;
281           if (cond_if_else_store_replacement (bb1, bb2, bb3))
282             cfgchanged = true;
283           continue;
284         }
285       else
286         continue;      
287
288       e1 = EDGE_SUCC (bb1, 0);
289
290       /* Make sure that bb1 is just a fall through.  */
291       if (!single_succ_p (bb1)
292           || (e1->flags & EDGE_FALLTHRU) == 0)
293         continue;
294
295       /* Also make sure that bb1 only have one predecessor and that it
296          is bb.  */
297       if (!single_pred_p (bb1)
298           || single_pred (bb1) != bb)
299         continue;
300
301       if (do_store_elim)
302         {
303           /* bb1 is the middle block, bb2 the join block, bb the split block,
304              e1 the fallthrough edge from bb1 to bb2.  We can't do the
305              optimization if the join block has more than two predecessors.  */
306           if (EDGE_COUNT (bb2->preds) > 2)
307             continue;
308           if (cond_store_replacement (bb1, bb2, e1, e2, nontrap))
309             cfgchanged = true;
310         }
311       else
312         {
313           gimple_seq phis = phi_nodes (bb2);
314
315           /* Check to make sure that there is only one PHI node.
316              TODO: we could do it with more than one iff the other PHI nodes
317              have the same elements for these two edges.  */
318           if (! gimple_seq_singleton_p (phis))
319             continue;
320
321           phi = gsi_stmt (gsi_start (phis));
322           arg0 = gimple_phi_arg_def (phi, e1->dest_idx);
323           arg1 = gimple_phi_arg_def (phi, e2->dest_idx);
324
325           /* Something is wrong if we cannot find the arguments in the PHI
326              node.  */
327           gcc_assert (arg0 != NULL && arg1 != NULL);
328
329           /* Do the replacement of conditional if it can be done.  */
330           if (conditional_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
331             cfgchanged = true;
332           else if (value_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
333             cfgchanged = true;
334           else if (abs_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
335             cfgchanged = true;
336           else if (minmax_replacement (bb, bb1, e1, e2, phi, arg0, arg1))
337             cfgchanged = true;
338         }
339     }
340
341   free (bb_order);
342
343   if (do_store_elim)
344     pointer_set_destroy (nontrap);
345   /* If the CFG has changed, we should cleanup the CFG.  */
346   if (cfgchanged && do_store_elim)
347     {
348       /* In cond-store replacement we have added some loads on edges
349          and new VOPS (as we moved the store, and created a load).  */
350       gsi_commit_edge_inserts ();
351       return TODO_cleanup_cfg | TODO_update_ssa_only_virtuals;
352     }
353   else if (cfgchanged)
354     return TODO_cleanup_cfg;
355   return 0;
356 }
357
358 /* Returns the list of basic blocks in the function in an order that guarantees
359    that if a block X has just a single predecessor Y, then Y is after X in the
360    ordering.  */
361
362 basic_block *
363 blocks_in_phiopt_order (void)
364 {
365   basic_block x, y;
366   basic_block *order = XNEWVEC (basic_block, n_basic_blocks);
367   unsigned n = n_basic_blocks - NUM_FIXED_BLOCKS;
368   unsigned np, i;
369   sbitmap visited = sbitmap_alloc (last_basic_block);
370
371 #define MARK_VISITED(BB) (SET_BIT (visited, (BB)->index))
372 #define VISITED_P(BB) (TEST_BIT (visited, (BB)->index))
373
374   sbitmap_zero (visited);
375
376   MARK_VISITED (ENTRY_BLOCK_PTR);
377   FOR_EACH_BB (x)
378     {
379       if (VISITED_P (x))
380         continue;
381
382       /* Walk the predecessors of x as long as they have precisely one
383          predecessor and add them to the list, so that they get stored
384          after x.  */
385       for (y = x, np = 1;
386            single_pred_p (y) && !VISITED_P (single_pred (y));
387            y = single_pred (y))
388         np++;
389       for (y = x, i = n - np;
390            single_pred_p (y) && !VISITED_P (single_pred (y));
391            y = single_pred (y), i++)
392         {
393           order[i] = y;
394           MARK_VISITED (y);
395         }
396       order[i] = y;
397       MARK_VISITED (y);
398
399       gcc_assert (i == n - 1);
400       n -= np;
401     }
402
403   sbitmap_free (visited);
404   gcc_assert (n == 0);
405   return order;
406
407 #undef MARK_VISITED
408 #undef VISITED_P
409 }
410
411
412 /* Return TRUE if block BB has no executable statements, otherwise return
413    FALSE.  */
414
415 bool
416 empty_block_p (basic_block bb)
417 {
418   /* BB must have no executable statements.  */
419   gimple_stmt_iterator gsi = gsi_after_labels (bb);
420   if (gsi_end_p (gsi))
421     return true;
422   if (is_gimple_debug (gsi_stmt (gsi)))
423     gsi_next_nondebug (&gsi);
424   return gsi_end_p (gsi);
425 }
426
427 /* Replace PHI node element whose edge is E in block BB with variable NEW.
428    Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK
429    is known to have two edges, one of which must reach BB).  */
430
431 static void
432 replace_phi_edge_with_variable (basic_block cond_block,
433                                 edge e, gimple phi, tree new_tree)
434 {
435   basic_block bb = gimple_bb (phi);
436   basic_block block_to_remove;
437   gimple_stmt_iterator gsi;
438
439   /* Change the PHI argument to new.  */
440   SET_USE (PHI_ARG_DEF_PTR (phi, e->dest_idx), new_tree);
441
442   /* Remove the empty basic block.  */
443   if (EDGE_SUCC (cond_block, 0)->dest == bb)
444     {
445       EDGE_SUCC (cond_block, 0)->flags |= EDGE_FALLTHRU;
446       EDGE_SUCC (cond_block, 0)->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
447       EDGE_SUCC (cond_block, 0)->probability = REG_BR_PROB_BASE;
448       EDGE_SUCC (cond_block, 0)->count += EDGE_SUCC (cond_block, 1)->count;
449
450       block_to_remove = EDGE_SUCC (cond_block, 1)->dest;
451     }
452   else
453     {
454       EDGE_SUCC (cond_block, 1)->flags |= EDGE_FALLTHRU;
455       EDGE_SUCC (cond_block, 1)->flags
456         &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
457       EDGE_SUCC (cond_block, 1)->probability = REG_BR_PROB_BASE;
458       EDGE_SUCC (cond_block, 1)->count += EDGE_SUCC (cond_block, 0)->count;
459
460       block_to_remove = EDGE_SUCC (cond_block, 0)->dest;
461     }
462   delete_basic_block (block_to_remove);
463
464   /* Eliminate the COND_EXPR at the end of COND_BLOCK.  */
465   gsi = gsi_last_bb (cond_block);
466   gsi_remove (&gsi, true);
467
468   if (dump_file && (dump_flags & TDF_DETAILS))
469     fprintf (dump_file,
470               "COND_EXPR in block %d and PHI in block %d converted to straightline code.\n",
471               cond_block->index,
472               bb->index);
473 }
474
475 /*  The function conditional_replacement does the main work of doing the
476     conditional replacement.  Return true if the replacement is done.
477     Otherwise return false.
478     BB is the basic block where the replacement is going to be done on.  ARG0
479     is argument 0 from PHI.  Likewise for ARG1.  */
480
481 static bool
482 conditional_replacement (basic_block cond_bb, basic_block middle_bb,
483                          edge e0, edge e1, gimple phi,
484                          tree arg0, tree arg1)
485 {
486   tree result;
487   gimple stmt, new_stmt;
488   tree cond;
489   gimple_stmt_iterator gsi;
490   edge true_edge, false_edge;
491   tree new_var, new_var2;
492
493   /* FIXME: Gimplification of complex type is too hard for now.  */
494   if (TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
495       || TREE_CODE (TREE_TYPE (arg1)) == COMPLEX_TYPE)
496     return false;
497
498   /* The PHI arguments have the constants 0 and 1, then convert
499      it to the conditional.  */
500   if ((integer_zerop (arg0) && integer_onep (arg1))
501       || (integer_zerop (arg1) && integer_onep (arg0)))
502     ;
503   else
504     return false;
505
506   if (!empty_block_p (middle_bb))
507     return false;
508
509   /* At this point we know we have a GIMPLE_COND with two successors.
510      One successor is BB, the other successor is an empty block which
511      falls through into BB.
512
513      There is a single PHI node at the join point (BB) and its arguments
514      are constants (0, 1).
515
516      So, given the condition COND, and the two PHI arguments, we can
517      rewrite this PHI into non-branching code:
518
519        dest = (COND) or dest = COND'
520
521      We use the condition as-is if the argument associated with the
522      true edge has the value one or the argument associated with the
523      false edge as the value zero.  Note that those conditions are not
524      the same since only one of the outgoing edges from the GIMPLE_COND
525      will directly reach BB and thus be associated with an argument.  */
526
527   stmt = last_stmt (cond_bb);
528   result = PHI_RESULT (phi);
529
530   /* To handle special cases like floating point comparison, it is easier and
531      less error-prone to build a tree and gimplify it on the fly though it is
532      less efficient.  */
533   cond = fold_build2 (gimple_cond_code (stmt), boolean_type_node,
534                       gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
535
536   /* We need to know which is the true edge and which is the false
537      edge so that we know when to invert the condition below.  */
538   extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
539   if ((e0 == true_edge && integer_zerop (arg0))
540       || (e0 == false_edge && integer_onep (arg0))
541       || (e1 == true_edge && integer_zerop (arg1))
542       || (e1 == false_edge && integer_onep (arg1)))
543     cond = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (cond), cond);
544
545   /* Insert our new statements at the end of conditional block before the
546      COND_STMT.  */
547   gsi = gsi_for_stmt (stmt);
548   new_var = force_gimple_operand_gsi (&gsi, cond, true, NULL, true,
549                                       GSI_SAME_STMT);
550
551   if (!useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (new_var)))
552     {
553       source_location locus_0, locus_1;
554
555       new_var2 = create_tmp_var (TREE_TYPE (result), NULL);
556       add_referenced_var (new_var2);
557       new_stmt = gimple_build_assign_with_ops (CONVERT_EXPR, new_var2,
558                                                new_var, NULL);
559       new_var2 = make_ssa_name (new_var2, new_stmt);
560       gimple_assign_set_lhs (new_stmt, new_var2);
561       gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
562       new_var = new_var2;
563
564       /* Set the locus to the first argument, unless is doesn't have one.  */
565       locus_0 = gimple_phi_arg_location (phi, 0);
566       locus_1 = gimple_phi_arg_location (phi, 1);
567       if (locus_0 == UNKNOWN_LOCATION)
568         locus_0 = locus_1;
569       gimple_set_location (new_stmt, locus_0);
570     }
571
572   replace_phi_edge_with_variable (cond_bb, e1, phi, new_var);
573
574   /* Note that we optimized this PHI.  */
575   return true;
576 }
577
578 /*  The function value_replacement does the main work of doing the value
579     replacement.  Return true if the replacement is done.  Otherwise return
580     false.
581     BB is the basic block where the replacement is going to be done on.  ARG0
582     is argument 0 from the PHI.  Likewise for ARG1.  */
583
584 static bool
585 value_replacement (basic_block cond_bb, basic_block middle_bb,
586                    edge e0, edge e1, gimple phi,
587                    tree arg0, tree arg1)
588 {
589   gimple cond;
590   edge true_edge, false_edge;
591   enum tree_code code;
592
593   /* If the type says honor signed zeros we cannot do this
594      optimization.  */
595   if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
596     return false;
597
598   if (!empty_block_p (middle_bb))
599     return false;
600
601   cond = last_stmt (cond_bb);
602   code = gimple_cond_code (cond);
603
604   /* This transformation is only valid for equality comparisons.  */
605   if (code != NE_EXPR && code != EQ_EXPR)
606     return false;
607
608   /* We need to know which is the true edge and which is the false
609       edge so that we know if have abs or negative abs.  */
610   extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
611
612   /* At this point we know we have a COND_EXPR with two successors.
613      One successor is BB, the other successor is an empty block which
614      falls through into BB.
615
616      The condition for the COND_EXPR is known to be NE_EXPR or EQ_EXPR.
617
618      There is a single PHI node at the join point (BB) with two arguments.
619
620      We now need to verify that the two arguments in the PHI node match
621      the two arguments to the equality comparison.  */
622
623   if ((operand_equal_for_phi_arg_p (arg0, gimple_cond_lhs (cond))
624        && operand_equal_for_phi_arg_p (arg1, gimple_cond_rhs (cond)))
625       || (operand_equal_for_phi_arg_p (arg1, gimple_cond_lhs (cond))
626           && operand_equal_for_phi_arg_p (arg0, gimple_cond_rhs (cond))))
627     {
628       edge e;
629       tree arg;
630
631       /* For NE_EXPR, we want to build an assignment result = arg where
632          arg is the PHI argument associated with the true edge.  For
633          EQ_EXPR we want the PHI argument associated with the false edge.  */
634       e = (code == NE_EXPR ? true_edge : false_edge);
635
636       /* Unfortunately, E may not reach BB (it may instead have gone to
637          OTHER_BLOCK).  If that is the case, then we want the single outgoing
638          edge from OTHER_BLOCK which reaches BB and represents the desired
639          path from COND_BLOCK.  */
640       if (e->dest == middle_bb)
641         e = single_succ_edge (e->dest);
642
643       /* Now we know the incoming edge to BB that has the argument for the
644          RHS of our new assignment statement.  */
645       if (e0 == e)
646         arg = arg0;
647       else
648         arg = arg1;
649
650       replace_phi_edge_with_variable (cond_bb, e1, phi, arg);
651
652       /* Note that we optimized this PHI.  */
653       return true;
654     }
655   return false;
656 }
657
658 /*  The function minmax_replacement does the main work of doing the minmax
659     replacement.  Return true if the replacement is done.  Otherwise return
660     false.
661     BB is the basic block where the replacement is going to be done on.  ARG0
662     is argument 0 from the PHI.  Likewise for ARG1.  */
663
664 static bool
665 minmax_replacement (basic_block cond_bb, basic_block middle_bb,
666                     edge e0, edge e1, gimple phi,
667                     tree arg0, tree arg1)
668 {
669   tree result, type;
670   gimple cond, new_stmt;
671   edge true_edge, false_edge;
672   enum tree_code cmp, minmax, ass_code;
673   tree smaller, larger, arg_true, arg_false;
674   gimple_stmt_iterator gsi, gsi_from;
675
676   type = TREE_TYPE (PHI_RESULT (phi));
677
678   /* The optimization may be unsafe due to NaNs.  */
679   if (HONOR_NANS (TYPE_MODE (type)))
680     return false;
681
682   cond = last_stmt (cond_bb);
683   cmp = gimple_cond_code (cond);
684   result = PHI_RESULT (phi);
685
686   /* This transformation is only valid for order comparisons.  Record which
687      operand is smaller/larger if the result of the comparison is true.  */
688   if (cmp == LT_EXPR || cmp == LE_EXPR)
689     {
690       smaller = gimple_cond_lhs (cond);
691       larger = gimple_cond_rhs (cond);
692     }
693   else if (cmp == GT_EXPR || cmp == GE_EXPR)
694     {
695       smaller = gimple_cond_rhs (cond);
696       larger = gimple_cond_lhs (cond);
697     }
698   else
699     return false;
700
701   /* We need to know which is the true edge and which is the false
702       edge so that we know if have abs or negative abs.  */
703   extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
704
705   /* Forward the edges over the middle basic block.  */
706   if (true_edge->dest == middle_bb)
707     true_edge = EDGE_SUCC (true_edge->dest, 0);
708   if (false_edge->dest == middle_bb)
709     false_edge = EDGE_SUCC (false_edge->dest, 0);
710
711   if (true_edge == e0)
712     {
713       gcc_assert (false_edge == e1);
714       arg_true = arg0;
715       arg_false = arg1;
716     }
717   else
718     {
719       gcc_assert (false_edge == e0);
720       gcc_assert (true_edge == e1);
721       arg_true = arg1;
722       arg_false = arg0;
723     }
724
725   if (empty_block_p (middle_bb))
726     {
727       if (operand_equal_for_phi_arg_p (arg_true, smaller)
728           && operand_equal_for_phi_arg_p (arg_false, larger))
729         {
730           /* Case
731
732              if (smaller < larger)
733              rslt = smaller;
734              else
735              rslt = larger;  */
736           minmax = MIN_EXPR;
737         }
738       else if (operand_equal_for_phi_arg_p (arg_false, smaller)
739                && operand_equal_for_phi_arg_p (arg_true, larger))
740         minmax = MAX_EXPR;
741       else
742         return false;
743     }
744   else
745     {
746       /* Recognize the following case, assuming d <= u:
747
748          if (a <= u)
749            b = MAX (a, d);
750          x = PHI <b, u>
751
752          This is equivalent to
753
754          b = MAX (a, d);
755          x = MIN (b, u);  */
756
757       gimple assign = last_and_only_stmt (middle_bb);
758       tree lhs, op0, op1, bound;
759
760       if (!assign
761           || gimple_code (assign) != GIMPLE_ASSIGN)
762         return false;
763
764       lhs = gimple_assign_lhs (assign);
765       ass_code = gimple_assign_rhs_code (assign);
766       if (ass_code != MAX_EXPR && ass_code != MIN_EXPR)
767         return false;
768       op0 = gimple_assign_rhs1 (assign);
769       op1 = gimple_assign_rhs2 (assign);
770
771       if (true_edge->src == middle_bb)
772         {
773           /* We got here if the condition is true, i.e., SMALLER < LARGER.  */
774           if (!operand_equal_for_phi_arg_p (lhs, arg_true))
775             return false;
776
777           if (operand_equal_for_phi_arg_p (arg_false, larger))
778             {
779               /* Case
780
781                  if (smaller < larger)
782                    {
783                      r' = MAX_EXPR (smaller, bound)
784                    }
785                  r = PHI <r', larger>  --> to be turned to MIN_EXPR.  */
786               if (ass_code != MAX_EXPR)
787                 return false;
788
789               minmax = MIN_EXPR;
790               if (operand_equal_for_phi_arg_p (op0, smaller))
791                 bound = op1;
792               else if (operand_equal_for_phi_arg_p (op1, smaller))
793                 bound = op0;
794               else
795                 return false;
796
797               /* We need BOUND <= LARGER.  */
798               if (!integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node,
799                                                   bound, larger)))
800                 return false;
801             }
802           else if (operand_equal_for_phi_arg_p (arg_false, smaller))
803             {
804               /* Case
805
806                  if (smaller < larger)
807                    {
808                      r' = MIN_EXPR (larger, bound)
809                    }
810                  r = PHI <r', smaller>  --> to be turned to MAX_EXPR.  */
811               if (ass_code != MIN_EXPR)
812                 return false;
813
814               minmax = MAX_EXPR;
815               if (operand_equal_for_phi_arg_p (op0, larger))
816                 bound = op1;
817               else if (operand_equal_for_phi_arg_p (op1, larger))
818                 bound = op0;
819               else
820                 return false;
821
822               /* We need BOUND >= SMALLER.  */
823               if (!integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node,
824                                                   bound, smaller)))
825                 return false;
826             }
827           else
828             return false;
829         }
830       else
831         {
832           /* We got here if the condition is false, i.e., SMALLER > LARGER.  */
833           if (!operand_equal_for_phi_arg_p (lhs, arg_false))
834             return false;
835
836           if (operand_equal_for_phi_arg_p (arg_true, larger))
837             {
838               /* Case
839
840                  if (smaller > larger)
841                    {
842                      r' = MIN_EXPR (smaller, bound)
843                    }
844                  r = PHI <r', larger>  --> to be turned to MAX_EXPR.  */
845               if (ass_code != MIN_EXPR)
846                 return false;
847
848               minmax = MAX_EXPR;
849               if (operand_equal_for_phi_arg_p (op0, smaller))
850                 bound = op1;
851               else if (operand_equal_for_phi_arg_p (op1, smaller))
852                 bound = op0;
853               else
854                 return false;
855
856               /* We need BOUND >= LARGER.  */
857               if (!integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node,
858                                                   bound, larger)))
859                 return false;
860             }
861           else if (operand_equal_for_phi_arg_p (arg_true, smaller))
862             {
863               /* Case
864
865                  if (smaller > larger)
866                    {
867                      r' = MAX_EXPR (larger, bound)
868                    }
869                  r = PHI <r', smaller>  --> to be turned to MIN_EXPR.  */
870               if (ass_code != MAX_EXPR)
871                 return false;
872
873               minmax = MIN_EXPR;
874               if (operand_equal_for_phi_arg_p (op0, larger))
875                 bound = op1;
876               else if (operand_equal_for_phi_arg_p (op1, larger))
877                 bound = op0;
878               else
879                 return false;
880
881               /* We need BOUND <= SMALLER.  */
882               if (!integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node,
883                                                   bound, smaller)))
884                 return false;
885             }
886           else
887             return false;
888         }
889
890       /* Move the statement from the middle block.  */
891       gsi = gsi_last_bb (cond_bb);
892       gsi_from = gsi_last_nondebug_bb (middle_bb);
893       gsi_move_before (&gsi_from, &gsi);
894     }
895
896   /* Emit the statement to compute min/max.  */
897   result = duplicate_ssa_name (PHI_RESULT (phi), NULL);
898   new_stmt = gimple_build_assign_with_ops (minmax, result, arg0, arg1);
899   gsi = gsi_last_bb (cond_bb);
900   gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
901
902   replace_phi_edge_with_variable (cond_bb, e1, phi, result);
903   return true;
904 }
905
906 /*  The function absolute_replacement does the main work of doing the absolute
907     replacement.  Return true if the replacement is done.  Otherwise return
908     false.
909     bb is the basic block where the replacement is going to be done on.  arg0
910     is argument 0 from the phi.  Likewise for arg1.  */
911
912 static bool
913 abs_replacement (basic_block cond_bb, basic_block middle_bb,
914                  edge e0 ATTRIBUTE_UNUSED, edge e1,
915                  gimple phi, tree arg0, tree arg1)
916 {
917   tree result;
918   gimple new_stmt, cond;
919   gimple_stmt_iterator gsi;
920   edge true_edge, false_edge;
921   gimple assign;
922   edge e;
923   tree rhs, lhs;
924   bool negate;
925   enum tree_code cond_code;
926
927   /* If the type says honor signed zeros we cannot do this
928      optimization.  */
929   if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
930     return false;
931
932   /* OTHER_BLOCK must have only one executable statement which must have the
933      form arg0 = -arg1 or arg1 = -arg0.  */
934
935   assign = last_and_only_stmt (middle_bb);
936   /* If we did not find the proper negation assignment, then we can not
937      optimize.  */
938   if (assign == NULL)
939     return false;
940
941   /* If we got here, then we have found the only executable statement
942      in OTHER_BLOCK.  If it is anything other than arg = -arg1 or
943      arg1 = -arg0, then we can not optimize.  */
944   if (gimple_code (assign) != GIMPLE_ASSIGN)
945     return false;
946
947   lhs = gimple_assign_lhs (assign);
948
949   if (gimple_assign_rhs_code (assign) != NEGATE_EXPR)
950     return false;
951
952   rhs = gimple_assign_rhs1 (assign);
953
954   /* The assignment has to be arg0 = -arg1 or arg1 = -arg0.  */
955   if (!(lhs == arg0 && rhs == arg1)
956       && !(lhs == arg1 && rhs == arg0))
957     return false;
958
959   cond = last_stmt (cond_bb);
960   result = PHI_RESULT (phi);
961
962   /* Only relationals comparing arg[01] against zero are interesting.  */
963   cond_code = gimple_cond_code (cond);
964   if (cond_code != GT_EXPR && cond_code != GE_EXPR
965       && cond_code != LT_EXPR && cond_code != LE_EXPR)
966     return false;
967
968   /* Make sure the conditional is arg[01] OP y.  */
969   if (gimple_cond_lhs (cond) != rhs)
970     return false;
971
972   if (FLOAT_TYPE_P (TREE_TYPE (gimple_cond_rhs (cond)))
973                ? real_zerop (gimple_cond_rhs (cond))
974                : integer_zerop (gimple_cond_rhs (cond)))
975     ;
976   else
977     return false;
978
979   /* We need to know which is the true edge and which is the false
980      edge so that we know if have abs or negative abs.  */
981   extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
982
983   /* For GT_EXPR/GE_EXPR, if the true edge goes to OTHER_BLOCK, then we
984      will need to negate the result.  Similarly for LT_EXPR/LE_EXPR if
985      the false edge goes to OTHER_BLOCK.  */
986   if (cond_code == GT_EXPR || cond_code == GE_EXPR)
987     e = true_edge;
988   else
989     e = false_edge;
990
991   if (e->dest == middle_bb)
992     negate = true;
993   else
994     negate = false;
995
996   result = duplicate_ssa_name (result, NULL);
997
998   if (negate)
999     {
1000       tree tmp = create_tmp_var (TREE_TYPE (result), NULL);
1001       add_referenced_var (tmp);
1002       lhs = make_ssa_name (tmp, NULL);
1003     }
1004   else
1005     lhs = result;
1006
1007   /* Build the modify expression with abs expression.  */
1008   new_stmt = gimple_build_assign_with_ops (ABS_EXPR, lhs, rhs, NULL);
1009
1010   gsi = gsi_last_bb (cond_bb);
1011   gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
1012
1013   if (negate)
1014     {
1015       /* Get the right GSI.  We want to insert after the recently
1016          added ABS_EXPR statement (which we know is the first statement
1017          in the block.  */
1018       new_stmt = gimple_build_assign_with_ops (NEGATE_EXPR, result, lhs, NULL);
1019
1020       gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
1021     }
1022
1023   replace_phi_edge_with_variable (cond_bb, e1, phi, result);
1024
1025   /* Note that we optimized this PHI.  */
1026   return true;
1027 }
1028
1029 /* Auxiliary functions to determine the set of memory accesses which
1030    can't trap because they are preceded by accesses to the same memory
1031    portion.  We do that for MEM_REFs, so we only need to track
1032    the SSA_NAME of the pointer indirectly referenced.  The algorithm
1033    simply is a walk over all instructions in dominator order.  When
1034    we see an MEM_REF we determine if we've already seen a same
1035    ref anywhere up to the root of the dominator tree.  If we do the
1036    current access can't trap.  If we don't see any dominating access
1037    the current access might trap, but might also make later accesses
1038    non-trapping, so we remember it.  We need to be careful with loads
1039    or stores, for instance a load might not trap, while a store would,
1040    so if we see a dominating read access this doesn't mean that a later
1041    write access would not trap.  Hence we also need to differentiate the
1042    type of access(es) seen.
1043
1044    ??? We currently are very conservative and assume that a load might
1045    trap even if a store doesn't (write-only memory).  This probably is
1046    overly conservative.  */
1047
1048 /* A hash-table of SSA_NAMEs, and in which basic block an MEM_REF
1049    through it was seen, which would constitute a no-trap region for
1050    same accesses.  */
1051 struct name_to_bb
1052 {
1053   tree ssa_name;
1054   basic_block bb;
1055   unsigned store : 1;
1056 };
1057
1058 /* The hash table for remembering what we've seen.  */
1059 static htab_t seen_ssa_names;
1060
1061 /* The set of MEM_REFs which can't trap.  */
1062 static struct pointer_set_t *nontrap_set;
1063
1064 /* The hash function, based on the pointer to the pointer SSA_NAME.  */
1065 static hashval_t
1066 name_to_bb_hash (const void *p)
1067 {
1068   const_tree n = ((const struct name_to_bb *)p)->ssa_name;
1069   return htab_hash_pointer (n) ^ ((const struct name_to_bb *)p)->store;
1070 }
1071
1072 /* The equality function of *P1 and *P2.  SSA_NAMEs are shared, so
1073    it's enough to simply compare them for equality.  */
1074 static int
1075 name_to_bb_eq (const void *p1, const void *p2)
1076 {
1077   const struct name_to_bb *n1 = (const struct name_to_bb *)p1;
1078   const struct name_to_bb *n2 = (const struct name_to_bb *)p2;
1079
1080   return n1->ssa_name == n2->ssa_name && n1->store == n2->store;
1081 }
1082
1083 /* We see the expression EXP in basic block BB.  If it's an interesting
1084    expression (an MEM_REF through an SSA_NAME) possibly insert the
1085    expression into the set NONTRAP or the hash table of seen expressions.
1086    STORE is true if this expression is on the LHS, otherwise it's on
1087    the RHS.  */
1088 static void
1089 add_or_mark_expr (basic_block bb, tree exp,
1090                   struct pointer_set_t *nontrap, bool store)
1091 {
1092   if (TREE_CODE (exp) == MEM_REF
1093       && TREE_CODE (TREE_OPERAND (exp, 0)) == SSA_NAME)
1094     {
1095       tree name = TREE_OPERAND (exp, 0);
1096       struct name_to_bb map;
1097       void **slot;
1098       struct name_to_bb *n2bb;
1099       basic_block found_bb = 0;
1100
1101       /* Try to find the last seen MEM_REF through the same
1102          SSA_NAME, which can trap.  */
1103       map.ssa_name = name;
1104       map.bb = 0;
1105       map.store = store;
1106       slot = htab_find_slot (seen_ssa_names, &map, INSERT);
1107       n2bb = (struct name_to_bb *) *slot;
1108       if (n2bb)
1109         found_bb = n2bb->bb;
1110
1111       /* If we've found a trapping MEM_REF, _and_ it dominates EXP
1112          (it's in a basic block on the path from us to the dominator root)
1113          then we can't trap.  */
1114       if (found_bb && found_bb->aux == (void *)1)
1115         {
1116           pointer_set_insert (nontrap, exp);
1117         }
1118       else
1119         {
1120           /* EXP might trap, so insert it into the hash table.  */
1121           if (n2bb)
1122             {
1123               n2bb->bb = bb;
1124             }
1125           else
1126             {
1127               n2bb = XNEW (struct name_to_bb);
1128               n2bb->ssa_name = name;
1129               n2bb->bb = bb;
1130               n2bb->store = store;
1131               *slot = n2bb;
1132             }
1133         }
1134     }
1135 }
1136
1137 /* Called by walk_dominator_tree, when entering the block BB.  */
1138 static void
1139 nt_init_block (struct dom_walk_data *data ATTRIBUTE_UNUSED, basic_block bb)
1140 {
1141   gimple_stmt_iterator gsi;
1142   /* Mark this BB as being on the path to dominator root.  */
1143   bb->aux = (void*)1;
1144
1145   /* And walk the statements in order.  */
1146   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1147     {
1148       gimple stmt = gsi_stmt (gsi);
1149
1150       if (is_gimple_assign (stmt))
1151         {
1152           add_or_mark_expr (bb, gimple_assign_lhs (stmt), nontrap_set, true);
1153           add_or_mark_expr (bb, gimple_assign_rhs1 (stmt), nontrap_set, false);
1154           if (get_gimple_rhs_num_ops (gimple_assign_rhs_code (stmt)) > 1)
1155             add_or_mark_expr (bb, gimple_assign_rhs2 (stmt), nontrap_set,
1156                               false);
1157         }
1158     }
1159 }
1160
1161 /* Called by walk_dominator_tree, when basic block BB is exited.  */
1162 static void
1163 nt_fini_block (struct dom_walk_data *data ATTRIBUTE_UNUSED, basic_block bb)
1164 {
1165   /* This BB isn't on the path to dominator root anymore.  */
1166   bb->aux = NULL;
1167 }
1168
1169 /* This is the entry point of gathering non trapping memory accesses.
1170    It will do a dominator walk over the whole function, and it will
1171    make use of the bb->aux pointers.  It returns a set of trees
1172    (the MEM_REFs itself) which can't trap.  */
1173 static struct pointer_set_t *
1174 get_non_trapping (void)
1175 {
1176   struct pointer_set_t *nontrap;
1177   struct dom_walk_data walk_data;
1178
1179   nontrap = pointer_set_create ();
1180   seen_ssa_names = htab_create (128, name_to_bb_hash, name_to_bb_eq,
1181                                 free);
1182   /* We're going to do a dominator walk, so ensure that we have
1183      dominance information.  */
1184   calculate_dominance_info (CDI_DOMINATORS);
1185
1186   /* Setup callbacks for the generic dominator tree walker.  */
1187   nontrap_set = nontrap;
1188   walk_data.dom_direction = CDI_DOMINATORS;
1189   walk_data.initialize_block_local_data = NULL;
1190   walk_data.before_dom_children = nt_init_block;
1191   walk_data.after_dom_children = nt_fini_block;
1192   walk_data.global_data = NULL;
1193   walk_data.block_local_data_size = 0;
1194
1195   init_walk_dominator_tree (&walk_data);
1196   walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1197   fini_walk_dominator_tree (&walk_data);
1198   htab_delete (seen_ssa_names);
1199
1200   return nontrap;
1201 }
1202
1203 /* Do the main work of conditional store replacement.  We already know
1204    that the recognized pattern looks like so:
1205
1206    split:
1207      if (cond) goto MIDDLE_BB; else goto JOIN_BB (edge E1)
1208    MIDDLE_BB:
1209      something
1210      fallthrough (edge E0)
1211    JOIN_BB:
1212      some more
1213
1214    We check that MIDDLE_BB contains only one store, that that store
1215    doesn't trap (not via NOTRAP, but via checking if an access to the same
1216    memory location dominates us) and that the store has a "simple" RHS.  */
1217
1218 static bool
1219 cond_store_replacement (basic_block middle_bb, basic_block join_bb,
1220                         edge e0, edge e1, struct pointer_set_t *nontrap)
1221 {
1222   gimple assign = last_and_only_stmt (middle_bb);
1223   tree lhs, rhs, name;
1224   gimple newphi, new_stmt;
1225   gimple_stmt_iterator gsi;
1226   source_location locus;
1227
1228   /* Check if middle_bb contains of only one store.  */
1229   if (!assign
1230       || !gimple_assign_single_p (assign))
1231     return false;
1232
1233   locus = gimple_location (assign);
1234   lhs = gimple_assign_lhs (assign);
1235   rhs = gimple_assign_rhs1 (assign);
1236   if (TREE_CODE (lhs) != MEM_REF
1237       || TREE_CODE (TREE_OPERAND (lhs, 0)) != SSA_NAME
1238       || !is_gimple_reg_type (TREE_TYPE (lhs)))
1239     return false;
1240
1241   /* Prove that we can move the store down.  We could also check
1242      TREE_THIS_NOTRAP here, but in that case we also could move stores,
1243      whose value is not available readily, which we want to avoid.  */
1244   if (!pointer_set_contains (nontrap, lhs))
1245     return false;
1246
1247   /* Now we've checked the constraints, so do the transformation:
1248      1) Remove the single store.  */
1249   gsi = gsi_for_stmt (assign);
1250   unlink_stmt_vdef (assign);
1251   gsi_remove (&gsi, true);
1252   release_defs (assign);
1253
1254   /* 2) Create a temporary where we can store the old content
1255         of the memory touched by the store, if we need to.  */
1256   if (!condstoretemp || TREE_TYPE (lhs) != TREE_TYPE (condstoretemp))
1257     {
1258       condstoretemp = create_tmp_reg (TREE_TYPE (lhs), "cstore");
1259       get_var_ann (condstoretemp);
1260     }
1261   add_referenced_var (condstoretemp);
1262
1263   /* 3) Insert a load from the memory of the store to the temporary
1264         on the edge which did not contain the store.  */
1265   lhs = unshare_expr (lhs);
1266   new_stmt = gimple_build_assign (condstoretemp, lhs);
1267   name = make_ssa_name (condstoretemp, new_stmt);
1268   gimple_assign_set_lhs (new_stmt, name);
1269   gimple_set_location (new_stmt, locus);
1270   gsi_insert_on_edge (e1, new_stmt);
1271
1272   /* 4) Create a PHI node at the join block, with one argument
1273         holding the old RHS, and the other holding the temporary
1274         where we stored the old memory contents.  */
1275   newphi = create_phi_node (condstoretemp, join_bb);
1276   add_phi_arg (newphi, rhs, e0, locus);
1277   add_phi_arg (newphi, name, e1, locus);
1278
1279   lhs = unshare_expr (lhs);
1280   new_stmt = gimple_build_assign (lhs, PHI_RESULT (newphi));
1281
1282   /* 5) Insert that PHI node.  */
1283   gsi = gsi_after_labels (join_bb);
1284   if (gsi_end_p (gsi))
1285     {
1286       gsi = gsi_last_bb (join_bb);
1287       gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
1288     }
1289   else
1290     gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
1291
1292   return true;
1293 }
1294
1295 /* Do the main work of conditional store replacement.  We already know
1296    that the recognized pattern looks like so:
1297
1298    split:
1299      if (cond) goto THEN_BB; else goto ELSE_BB (edge E1)
1300    THEN_BB:
1301      X = Y;
1302      goto JOIN_BB;
1303    ELSE_BB:
1304      X = Z;
1305      fallthrough (edge E0)
1306    JOIN_BB:
1307      some more
1308
1309    We check that THEN_BB and ELSE_BB contain only one store
1310    that the stores have a "simple" RHS.  */
1311
1312 static bool
1313 cond_if_else_store_replacement (basic_block then_bb, basic_block else_bb,
1314                                 basic_block join_bb)
1315 {
1316   gimple then_assign = last_and_only_stmt (then_bb);
1317   gimple else_assign = last_and_only_stmt (else_bb);
1318   tree lhs_base, lhs, then_rhs, else_rhs;
1319   source_location then_locus, else_locus;
1320   gimple_stmt_iterator gsi;
1321   gimple newphi, new_stmt;
1322
1323   /* Check if then_bb and else_bb contain only one store each.  */
1324   if (then_assign == NULL
1325       || !gimple_assign_single_p (then_assign)
1326       || else_assign == NULL
1327       || !gimple_assign_single_p (else_assign))
1328     return false;
1329
1330   lhs = gimple_assign_lhs (then_assign);
1331   if (!is_gimple_reg_type (TREE_TYPE (lhs))
1332       || !operand_equal_p (lhs, gimple_assign_lhs (else_assign), 0))
1333     return false;
1334
1335   lhs_base = get_base_address (lhs);
1336   if (lhs_base == NULL_TREE
1337       || (!DECL_P (lhs_base) && TREE_CODE (lhs_base) != MEM_REF))
1338     return false;
1339
1340   then_rhs = gimple_assign_rhs1 (then_assign);
1341   else_rhs = gimple_assign_rhs1 (else_assign);
1342   then_locus = gimple_location (then_assign);
1343   else_locus = gimple_location (else_assign);
1344
1345   /* Now we've checked the constraints, so do the transformation:
1346      1) Remove the stores.  */
1347   gsi = gsi_for_stmt (then_assign);
1348   unlink_stmt_vdef (then_assign);
1349   gsi_remove (&gsi, true);
1350   release_defs (then_assign);
1351
1352   gsi = gsi_for_stmt (else_assign);
1353   unlink_stmt_vdef (else_assign);
1354   gsi_remove (&gsi, true);
1355   release_defs (else_assign);
1356
1357   /* 2) Create a temporary where we can store the old content
1358         of the memory touched by the store, if we need to.  */
1359   if (!condstoretemp || TREE_TYPE (lhs) != TREE_TYPE (condstoretemp))
1360     {
1361       condstoretemp = create_tmp_reg (TREE_TYPE (lhs), "cstore");
1362       get_var_ann (condstoretemp);
1363     }
1364   add_referenced_var (condstoretemp);
1365
1366   /* 3) Create a PHI node at the join block, with one argument
1367         holding the old RHS, and the other holding the temporary
1368         where we stored the old memory contents.  */
1369   newphi = create_phi_node (condstoretemp, join_bb);
1370   add_phi_arg (newphi, then_rhs, EDGE_SUCC (then_bb, 0), then_locus);
1371   add_phi_arg (newphi, else_rhs, EDGE_SUCC (else_bb, 0), else_locus);
1372
1373   new_stmt = gimple_build_assign (lhs, PHI_RESULT (newphi));
1374
1375   /* 4) Insert that PHI node.  */
1376   gsi = gsi_after_labels (join_bb);
1377   if (gsi_end_p (gsi))
1378     {
1379       gsi = gsi_last_bb (join_bb);
1380       gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
1381     }
1382   else
1383     gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
1384
1385   return true;
1386 }
1387
1388 /* Always do these optimizations if we have SSA
1389    trees to work on.  */
1390 static bool
1391 gate_phiopt (void)
1392 {
1393   return 1;
1394 }
1395
1396 struct gimple_opt_pass pass_phiopt =
1397 {
1398  {
1399   GIMPLE_PASS,
1400   "phiopt",                             /* name */
1401   gate_phiopt,                          /* gate */
1402   tree_ssa_phiopt,                      /* execute */
1403   NULL,                                 /* sub */
1404   NULL,                                 /* next */
1405   0,                                    /* static_pass_number */
1406   TV_TREE_PHIOPT,                       /* tv_id */
1407   PROP_cfg | PROP_ssa,                  /* properties_required */
1408   0,                                    /* properties_provided */
1409   0,                                    /* properties_destroyed */
1410   0,                                    /* todo_flags_start */
1411   TODO_dump_func
1412     | TODO_ggc_collect
1413     | TODO_verify_ssa
1414     | TODO_verify_flow
1415     | TODO_verify_stmts                 /* todo_flags_finish */
1416  }
1417 };
1418
1419 static bool
1420 gate_cselim (void)
1421 {
1422   return flag_tree_cselim;
1423 }
1424
1425 struct gimple_opt_pass pass_cselim =
1426 {
1427  {
1428   GIMPLE_PASS,
1429   "cselim",                             /* name */
1430   gate_cselim,                          /* gate */
1431   tree_ssa_cs_elim,                     /* execute */
1432   NULL,                                 /* sub */
1433   NULL,                                 /* next */
1434   0,                                    /* static_pass_number */
1435   TV_TREE_PHIOPT,                       /* tv_id */
1436   PROP_cfg | PROP_ssa,                  /* properties_required */
1437   0,                                    /* properties_provided */
1438   0,                                    /* properties_destroyed */
1439   0,                                    /* todo_flags_start */
1440   TODO_dump_func
1441     | TODO_ggc_collect
1442     | TODO_verify_ssa
1443     | TODO_verify_flow
1444     | TODO_verify_stmts                 /* todo_flags_finish */
1445  }
1446 };